<?php

namespace App\Http\Controllers\Admin\APIs\Customer_API;

use App\Http\Controllers\Controller;
use App\Services\APIs\CustomerAPIs\RestaurantApiServices;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class RestaurantControllerApi extends Controller
{
    protected $RestaurantApiServices;

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

    /**
     * Created By : Sayli Raut
     * Created at : 30 May 2024
     * Use : To get restaurant.
     */
    public function getCoordinates()
    {
        try {
            $token = readHeaderToken();

            if ($token) {
                $customerIamId = $token['sub'];
                $response = $this->RestaurantApiServices->getCoordinates($customerIamId);
                return $response;
            } else {
                return jsonResponseWithErrorMessageApi(__('auth.user_deleted'), 409);
            }
        } catch (\Exception $e) {
            Log::error('Passport function failed: ' . $e->getMessage());
            return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
        }
    }

    /**
     * Created By : Sayli Raut
     * Created at : 30 May 2024
     * Use : To add favourite.
     */
    public function addToFavourite(Request $request)
    {
        try {
            $token = readHeaderToken();

            if ($token) {
                $customerIamId = $token['sub'];
                $validator = Validator::make($request->all(), [
                    'id' => 'required',
                ]);

                if ($validator->fails()) {
                    return jsonResponseWithErrorMessageApi($validator->errors()->first(), 400);
                }
                $response = $this->RestaurantApiServices->addToFavourite($customerIamId, $request);
                return $response;
            } else {
                return jsonResponseWithErrorMessageApi(__('auth.user_deleted'), 409);
            }
        } catch (\Exception $e) {
            Log::error('Passport function failed: ' . $e->getMessage());
            return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
        }
    }

    /**
     * Created By : Sayli Raut
     * Created at : 30 May 2024
     * Use : To list of fav restaurant.
     */
    public function listFavRestaurant()
    {
        try {
            $token = readHeaderToken();

            if ($token) {
                $customerIamId = $token['sub'];
                $response = $this->RestaurantApiServices->listFavRestaurant($customerIamId);
                return $response;
            } else {
                return jsonResponseWithErrorMessageApi(__('auth.user_deleted'), 409);
            }
        } catch (\Exception $e) {
            Log::error('Passport function failed: ' . $e->getMessage());
            return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
        }
    }

    /**
     * Created By : Sayli Raut
     * Created at : 31 May 2024
     * Use : To detail of specific restaurant.
     */
    public function DetailRestaurant($id)
    {
        try {
            $token = readHeaderToken();

            if ($token) {
                $customerIamId = $token['sub'];
                $response = $this->RestaurantApiServices->DetailRestaurant($customerIamId, $id);
                return $response;
            } else {
                return jsonResponseWithErrorMessageApi(__('auth.user_deleted'), 409);
            }
        } catch (\Exception $e) {
            Log::error('Passport function failed: ' . $e->getMessage());
            return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
        }
    }




    /**
     * Created By : Sayli Raut
     * Created at : 04 June 2024
     * Use : To remove from favourite.
     */
    public function removeFromFavourite(Request $request)
    {
        try {
            $token = readHeaderToken();

            if ($token) {
                $customerIamId = $token['sub'];
                $validator = Validator::make($request->all(), [
                    'id' => 'required',
                ]);

                if ($validator->fails()) {
                    return jsonResponseWithErrorMessageApi($validator->errors()->first(), 400);
                }
                $response = $this->RestaurantApiServices->removeFromFavourite($customerIamId, $request);
                return $response;
            } else {
                return jsonResponseWithErrorMessageApi(__('auth.user_deleted'), 409);
            }
        } catch (\Exception $e) {
            Log::error('Passport function failed: ' . $e->getMessage());
            return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
        }
    }


    /**
     * Created By : Sayli Raut
     * Created at : 04 June 2024
     * Use : To search from favourite.
     */
    public function searchFromFavourite(Request $request)
    {
        try {
            $token = readHeaderToken();
            if ($token) {
                $customerIamId = $token['sub'];
                return $this->RestaurantApiServices->searchFromFavourite($customerIamId, $request);
            } else {
                return jsonResponseWithErrorMessageApi(__('auth.user_deleted'), 409);
            }
        } catch (\Exception $e) {
            Log::error("An error occurred in " . __METHOD__ . ": " . $e->getMessage(), ['exception' => $e]);
            return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
        }
    }

     /**
     * Created By : Sayli Raut
     * Created at : 04 June 2024
     * Use : To redeem restaurant.
     */
    public function redeemRestaurant(Request $request)
    {
        try {
            $token = readHeaderToken();
            if ($token) {
                $customerIamId = $token['sub'];
                $validator = Validator::make($request->all(), [
                    'id' => 'required',
                ]);

                if ($validator->fails()) {
                    return jsonResponseWithErrorMessageApi($validator->errors()->first(), 400);
                }
                return $this->RestaurantApiServices->redeemRestaurant($customerIamId, $request);
            } else {
                return jsonResponseWithErrorMessageApi(__('auth.user_deleted'), 409);
            }
        } catch (\Exception $e) {
            Log::error('Passport function failed: ' . $e->getMessage());
            return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
        }
    }
}
