<?php

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

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



class FeedbackApiController extends Controller
{

    protected $FeedbackApiServices;

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

    /**
     * Created By : Sayli Raut
     * Created at : 07 June 2024
     * Use : Storing feedback.
     */

    public function getFeedbackReaction(Request $request)
    {
        try {
            $token = readHeaderToken();
            if ($token) {

                $feedbackReactions = FeedbackReaction::select('id', 'feedback_reaction_title')->get();

                if ($feedbackReactions->isEmpty()) {
                    Log::info('Reactions not found.');
                    return jsonResponseWithSuccessMessageApi(__('success.data_not_found'), [], 200);
                }

                $responseData['result'] = $feedbackReactions;
                return jsonResponseWithSuccessMessageApi(__('success.data_fetched_successfully'), $feedbackReactions, 200);
            } else {
                return jsonResponseWithErrorMessageApi(__('auth.user_deleted'), 409);
            }
        } catch (Exception $e) {
            Log::error('Feedback Reaction API failed: ' . $e);
            return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500);
        }
    }


    public function storeFeedback(Request $request)
    {
        try {
            $token = readHeaderToken();
            $validator = Validator::make($request->all(), [
                'reaction' => 'required|numeric',
                'is_app_feedback' => 'required|boolean',
                'is_restaurant_feedback' => 'required|boolean',
                'restaurant_id' => $request->input('is_restaurant_feedback') == 1 ? 'required|integer' : '',
                // 'comment' => 'required',
            ]);

            if ($validator->fails()) {
                $validationErrors = $validator->errors()->all();
                Log::error("Validation error: " . implode(", ", $validationErrors));
                return jsonResponseWithErrorMessageApi($validationErrors, 403);
            }

            if ($token) {
                $customerIamId = $token['sub'];
                $response = $this->FeedbackApiServices->storeFeedback($customerIamId, $request);
                return $response;
            } 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);
        }
    }
}
