<?php

namespace App\Http\Controllers\Admin\APIs\RestaurantApi;
use Tymon\JWTAuth\Facades\JWTAuth;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Log;
use Exception;
use App\Models\IamPrincipal;
use Illuminate\Validation\Rule;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Services\APIs\RestaurantService\RestaurantApi_Service;
// use App\Services\APIs\RestaurantService\RestaurantApiService;
class RestaurantControllerApi extends Controller
{
    protected $RestaurantApiService;

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

     /**
     * Created By : sayali parab
     * Created at : 31 May 2024
     * Use : To get restaurant profile detail.
     */

    public function getRestProfileDetail()
    {
        try {
            $token = readRestHeaderToken();
            if ($token) {
                $restIamId = $token['sub'];
                return $this->RestaurantApiService->getRestProfileDetail($restIamId);
            } 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 : sayali parab
     * Created at : 31 May 2024
     * Use : To update restaurant user profile detail.
     */
    public function updateRestProfileDetail(Request $request)
    {
        try {
            $token = readRestHeaderToken();
            if ($token) {
                $restIamId = $token['sub'];
                $existingEmails = IamPrincipal::where('id', $restIamId)->first();
                $validator = Validator::make($request->all(), [
                    // 'email_address' => [
                    //     'required',
                    //     'email',
                    //     Rule::unique('iam_principal', 'email_address')
                    //         ->ignore($existingEmails->id)
                    //         ->whereNull('deleted_at')
                    //         ->where('principal_type_xid',4),
                    // ],
                    'email_address' => 'required|email',
                    'phone_number' => 'required|min:10',
                    'date_of_birth' => 'required|date',
                    'first_name' => 'required',
                    'last_name' => 'required',
                ]);
                if ($validator->fails()) {
                    return jsonResponseWithErrorMessageApi($validator->errors()->first(), 400);
                }
                $response =  $this->RestaurantApiService->updateRestProfileDetail($restIamId, $request);
                return response()->json(['status' => 'success', 'message' => 'Data updated successfully']);
            } 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 : sayali parab
     * Created at : 31 May 2024
     * Use : To update restaurant detail.
     */
    public function updateRestaurantDetail(Request $request)
    {
        try {
            $token = readRestHeaderToken();
            if ($token) {
                $restIamId = $token['sub'];
                $validator = Validator::make($request->all(), [
                    'name' => 'required',
                    'phone_number' => 'required',
                    'address' => 'required',
                    'bio' => 'required',
                    'try_on_1' => 'required',
                    'try_on_2' => 'required',
                    'try_on_3' => 'required',
                    'try_on_4' => 'required',



                ]);

                if ($validator->fails()) {
                    return jsonResponseWithErrorMessageApi($validator->errors()->first(), 400);
                }

                return $this->RestaurantApiService->updateRestaurantDetail($restIamId, $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 : sayali parab
     * Created at : 31 May 2024
     * Use : Reset password of restaurant profile detail.
     */
    public function resetRestPassword(Request $request)
    {
        try {
            $token = readRestHeaderToken();
            if ($token) {
                $restIamId = $token['sub'];
                $validator = Validator::make($request->all(), [
                    'current_password' => 'required',
                    'new_password' => 'required|string|min:6',
                    'confirm_password' => 'required|same:new_password',
                ]);

                if ($validator->fails()) {
                    return jsonResponseWithErrorMessageApi($validator->errors()->first(), 400);
                }
                return $this->RestaurantApiService->resetRestPassword($restIamId, $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 : sayali parab
     * Created at : 31 May 2024
     * Use : Logout Restaurant Account .
     */
    public function restaurantLogout(Request $request)
    {
        try {
            $jwtToken = $request->header('access-token');

            if ($jwtToken) {
                $iamPrincipalId = $request->user()->id;
                $user = IamPrincipal::find($iamPrincipalId);
                $user->one_signal_player_id = null;
                $user->save();

                JWTAuth::invalidate($jwtToken);

                Session::forget('RestToken');

                return jsonResponseWithSuccessMessageApi(__('auth.logout'), 200);
            } else {
                return jsonResponseWithErrorMessage(__('auth.invalid_token'), 401);
            }
        } catch (Exception $e) {
            // Log any errors
            Log::error('Account Logout failed: ' . $e->getMessage());
            return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
        }
    }
      /**
    * Created By : sayali parab
     * Created at : 31 May 2024
     * Use : Delete Restaurant Account .
     */
    public function restDeleteAccount(Request $request)
    {
        try {
            $token = readRestHeaderToken();

            if ($token) {
                $iamPrincipalId = $token['sub'];
                $deleteUser = IamPrincipal::find($iamPrincipalId);
                $deleteUser->one_signal_player_id = null;
                $deleteUser->save();

                if (!$deleteUser) {
                    return response()->json(['error' => 'User not found'], 404);
                }

                $deleteUser->delete();

                return jsonResponseWithSuccessMessageApi(__('auth.rest_delete_user'), 200);
            } else {
                return jsonResponseWithErrorMessageApi(__('auth.user_deleted'), 409);
            }
        } catch (\Exception $e) {
            Log::error('Account deletion failed: ' . $e->getMessage());
            return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
        }
    }
}
