<?php

namespace App\Http\Controllers\Admin\APIs\RestaurantApi;
use App\Services\APIs\RestaurantService\RestCMSService;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Exception;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;



class RestCMSController extends Controller
{
    protected $RestCMSService;

    public function __construct(RestCMSService $RestCMSService)
    {
        $this->RestCMSService = $RestCMSService;
    }

     /**
     * Created By : sayali parab
     * Created at : 31 May 2024
     * Use : To get faq detail of restaurant.
     */
    public function RestGetFaq()
    {
        try {
            $token = readRestHeaderToken();

            if ($token) {
                $restIamId = $token['sub'];
                $response = $this->RestCMSService->RestGetFaq();
                return jsonResponseWithSuccessMessageApi(__('success.data_fetched_successfully'), $response, 200);
            } else {
                return jsonResponseWithErrorMessageApi(__('auth.user_deleted'), 409);
            }
        } catch (\Exception $e) {
            Log::error('FAW get data controller function failed: ' . $e->getMessage());
            return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500);
        }
    }

      /**
      * Created By : sayali parab
     * Created at : 31 May 2024
     * Use : To get about us detail of restaurant.
     */
    public function RestAboutUs()
    {
        try {
            $token = readRestHeaderToken();
            if ($token) {
                $restIamId = $token['sub'];
                $response = $this->RestCMSService->RestAboutUs();
                return jsonResponseWithSuccessMessageApi(__('success.data_fetched_successfully'), $response, 200);
            } else {
                return jsonResponseWithErrorMessageApi(__('auth.user_deleted'), 409);
            }
        } catch (\Exception $e) {
            Log::error('FAW get data controller function failed: ' . $e->getMessage());
            return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500);
        }
    }

   /**
      * Created By : sayali parab
     * Created at : 31 May 2024
     * Use : To get privacy policy detail of restaurant.
     */
    public function RestPrivacyPolicy()
    {
        try {
            $token = readRestHeaderToken();
            if ($token) {
                $restIamId = $token['sub'];
                $response = $this->RestCMSService->RestPrivacyPolicy();
                return jsonResponseWithSuccessMessageApi(__('success.data_fetched_successfully'), $response, 200);
            } else {
                return jsonResponseWithErrorMessageApi(__('auth.user_deleted'), 409);
            }
        } catch (\Exception $e) {
            Log::error('FAW get data controller function failed: ' . $e->getMessage());
            return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500);
        }
    }


      /**
       * Created By :sayali parab
     * Created at : 31 May 2024
     * Use : To get News and Articles detail of restaurant.
     */
    public function RestNewsArticles()
    {
        try {
            $token = readRestHeaderToken();
            if ($token) {
                $restIamId = $token['sub'];
                $response = $this->RestCMSService->RestNewsArticles();
                // dd($response = $this->RestCMSService->RestNewsArticles());
                return jsonResponseWithSuccessMessageApi(__('success.data_fetched_successfully'), $response, 200);
            } else {
                return jsonResponseWithErrorMessageApi(__('auth.user_deleted'), 409);
            }
        } catch (\Exception $e) {
            Log::error('FAW get data controller function failed: ' . $e->getMessage());
            return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500);
        }
    }

    
    /**
      * Created By :sayali parab
     * Created at : 31 May 2024
     * Use : To store Contact Form for restaurant
     */
    public function RestContactUs(Request $request)
    {

        try {
            $token = readRestHeaderToken();
            if ($token) {
                $iam_principal_id = $token['sub'];
                $validator = $this->validateformdata($request);
                if ($validator->fails()) {

                    $validationErrors = $validator->errors()->all();
                    Log::error("Contact form validation error: " . implode(", ", $validationErrors));

                    return jsonResponseWithErrorMessageApi($validationErrors, 403);
                }
                $request['iam_principal_id'] = $iam_principal_id;

                return $this->RestCMSService->RestContactUs($request);
            } else {
                return jsonResponseWithErrorMessageApi(__('auth.user_deleted'), 409);
            }
        } catch (Exception $e) {

            Log::error('Contact form controller function failed: ' . $e);
            return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500);
        }
    }

    
    /**
    * Created By :sayali parab
     * Created at : 31 May 2024
     * Use : To validate Restaurant Contact form data
     */
    public function validateformdata(Request $request)
    {
        return Validator::make(
            $request->all(),
            [

                'name' => 'required|string|max:50',
                'email' => 'required|email|max:50',
                'message' => 'required',
                // 'iam_principal_xid'=>'required|integer'


            ]
        );
    }
}
