<?php

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

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


class ContactUsApiController extends Controller
{
    protected $ContactUsApiServices;

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

     /**
     * Created By : Sayli Raut
     * Created at : 24 May 2024
     * Use : To store Contact Form for customer & restaurant
     */
    public function addContactForm(Request $request)
    {

        try {
            $token = readHeaderToken();
            if ($token) {
                $iam_principal_id = $token['sub'];
                $validator = $this->validateContactForm($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->ContactUsApiServices->addCustomerRestaurantContactForm($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 : Sayli Raut
     * Created at :  27 May 2024
     * Use : To validate Customer and Restaurant Contact form data
     */
    public function validateContactForm(Request $request)
    {
        return Validator::make(
            $request->all(),
            [

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


            ]
        );
    }




}
