<?php

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

use App\Services\APIs\RestaurantService\RestAuthApiService;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use App\Models\IamPrincipal;


use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class RestAuthApiController extends Controller
{
    protected $RestAuthApiService;

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

    /**
     * Created By : sayali parab
     * Created at : 30 May 2024
     * Use : Restaurant Details.
     */
    public function viewresyaurant()
    {
        try {
            $response = $this->RestAuthApiService->viewresyaurant();
            return jsonResponseWithSuccessMessageApi(__('success.data_fetched_successfully'), $response, 200);
        } 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 : 30 May 2024
     * Use : Restaurant State.
     */
    public function viewstates()
    {
        try {
            $response = $this->RestAuthApiService->viewstates();
            return jsonResponseWithSuccessMessageApi(__('success.data_fetched_successfully'), $response, 200);
        } 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 : 30 May 2024
     * Use : Restaurant Registration.
     */
    public function restRegister(Request $request)
    {
        try {
            $validator = Validator::make($request->all(), [
                'first_name' => 'required|string|min:2|max:100',
                'last_name' => 'required|string|min:2|max:100',
                'role' => 'required|string|min:2|max:100',
                'restaurant_xid' => 'required',
                // 'state_xid'=>'required',
                'date_of_birth' => 'required|date',
                'email_address' => [
                    'required',
                    'string',
                    'email',
                    'max:100',
                    Rule::unique('iam_principal')->where(function ($query) {
                        return $query->where('principal_type_xid', 4)->whereNull('deleted_at');
                    }),
                ],
                'phone_number' => 'required|min:10',
            ]);

            if ($validator->fails()) {
                $validationErrors = $validator->errors()->all();
                Log::error("Registration validation error: " . implode(", ", $validationErrors));
                return jsonResponseWithErrorMessageApi($validationErrors, 403);
            }
            return $this->RestAuthApiService->restRegister($request);
        } catch (\Exception $ex) {
            Log::error("Registration API Failed: " . $ex->getMessage());
            return jsonResponseWithErrorMessage(__('error_message.something_went_wrong'), 500);
        }
    }
    /**
     * Created By : sayali parab
     * Created at : 31 May 2024
     * Use : Restaurant login.
     */
    public function login(Request $request)
    {
        try {
            $validator = Validator::make($request->all(), [
                'email_address' => 'required|string|email',
                'password' => 'required|string|min:6',
            ]);

            if ($validator->fails()) {
                $validationErrors = $validator->errors()->all();
                Log::error("Login validation error: " . implode(", ", $validationErrors));
                return jsonResponseWithErrorMessageApi($validationErrors, 403);
            }
            return $this->RestAuthApiService->login($request);
        } catch (\Exception $ex) {
            Log::error("Login API Failed: " . $ex->getMessage());
            return jsonResponseWithErrorMessage(__('error_message.something_went_wrong'), 500);
        }
    }
    /**
     * Created By : sayali parab
     * Created at : 31 May 2024
     * Use : Forgot password for restaurant.
     */
    public function restForgotPassword(Request $request)
    {
        try {
            $validator = Validator::make($request->all(), [
                'email_address' => [
                    'required',
                    'string',
                    'email',
                    function ($attribute, $value, $fail) {
                        $existingUser = IamPrincipal::where('email_address', $value)->where('principal_type_xid', 4)->whereNull('deleted_at')->exists();
                        if (!$existingUser) {
                            $fail('The selected email address is invalid.');
                        }
                    },
                ],
            ]);

            if ($validator->fails()) {
                $validationErrors = $validator->errors()->all();
                Log::error("Forgot password validation error: " . implode(", ", $validationErrors));

                return jsonResponseWithErrorMessageApi($validationErrors, 403);
            }
            return  $this->RestAuthApiService->restForgotPassword($request);
        } catch (\Exception $ex) {
            Log::error("Forgot password API Failed: " . $ex->getMessage());
            return jsonResponseWithErrorMessage(__('error_message.something_went_wrong'), 500);
        }
    }
    /**
     * Created By : sayali parab
     * Created at : 31 May 2024
     * Use : verify otp for restaurant.
     */
    public function restVerifyOTP(Request $request)
    {
        try {
            $validator = Validator::make($request->all(), [
                'email_address' => [
                    'required',
                    'string',
                    'email',
                    function ($attribute, $value, $fail) {
                        $existingUser = IamPrincipal::where('email_address', $value)->where('principal_type_xid', 4)->whereNull('deleted_at')->exists();
                        if (!$existingUser) {
                            $fail('The selected email address is invalid.');
                        }
                    },
                ],
                'otp' => 'required',
            ]);

            if ($validator->fails()) {
                $validationErrors = $validator->errors()->all();
                Log::error("Forgot password validation error: " . implode(", ", $validationErrors));
                return jsonResponseWithErrorMessageApi($validationErrors, 403);
            }
            return $this->RestAuthApiService->restVerifyOTP($request);
        } catch (\Exception $ex) {
            Log::error("Verify password API Failed: " . $ex->getMessage());
            return jsonResponseWithErrorMessage(__('error_message.something_went_wrong'), 500);
        }
    }

    /**
     * Created By : sayali parab
     * Created at : 31 May 2024
     * Use : Change password for restaurant.
     */
    public function restChangePassword(Request $request)
    {
        try {
            $validator = Validator::make($request->all(), [
                'iam_principal_xid' => 'required|exists:iam_principal,id',
                'password' => 'required|confirmed',
            ]);
            if ($validator->fails()) {
                $validationErrors = $validator->errors()->all();
                Log::error("Forgot password validation error: " . implode(", ", $validationErrors));
                return jsonResponseWithErrorMessageApi($validationErrors, 403);
            }
            $response = $this->RestAuthApiService->restChangePassword($request);
            return jsonResponseWithSuccessMessage(__('auth.data_fetched_successfully'), $response, 200);
        } catch (\Exception $ex) {
            Log::error("Change password API Failed: " . $ex->getMessage());
            return jsonResponseWithErrorMessage(__('error_message.something_went_wrong'), 500);
        }
    }

    /**
     * Created By : sayali parab
     * Created at : 31 May 2024
     * Use : Resend otp for restaurant.
     */
    public function restResendOtp(Request $request)
    {
        try {
            $validator = Validator::make($request->all(), [
                'iam_principal_xid' => 'required|exists:iam_principal,id',
                'otp_purpose' => 'required'
            ]);
            if ($validator->fails()) {
                $validationErrors = $validator->errors()->all();
                Log::error("Forgot password validation error: " . implode(", ", $validationErrors));
                return jsonResponseWithErrorMessageApi($validationErrors, 403);
            }
            return $this->RestAuthApiService->restResendOtp($request);
        } catch (\Exception $ex) {
            Log::error("Resend otp API Failed: " . $ex->getMessage());
            return jsonResponseWithErrorMessage(__('error_message.something_went_wrong'), 500);
        }
    }


    /**
     * Created By : Sayli Raut
     * Created at : 12 June 2024
     * Use : Search Restaurant .
     */
    public function searchRest(Request $request)
    {
        try {
            return $this->RestAuthApiService->searchRest($request);
        } catch (\Exception $ex) {
            Log::error("Login API Failed: " . $ex->getMessage());
            return jsonResponseWithErrorMessage(__('error_message.something_went_wrong'), 500);
        }
    }



}
