<?php

namespace Modules\Complaint\Controllers\Admin;

/** Ajax Request */

use App\Services\Response\ResponseService;
use Exception;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Redirector;
use Illuminate\Http\RedirectResponse;
use App\Http\Controllers\Controller;
use Modules\Complaint\Enums\ComplaintStatusEnum;
use Modules\Complaint\Models\Complaint;
use Modules\Complaint\Requests\Admin\ComplaintRequest;
use Modules\Complaint\Services\ComplaintService;
use Modules\Option\Enums\OptionStatusEnum;
use Modules\Option\Services\OptionService;

class ComplaintController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/admin/complaints';
    /** View folder */
    private $viewDirectory= 'admin.complaints.';
    //
    private ComplaintService $complaintService;
    private OptionService $optionService;

    public function __construct(ComplaintService $complaintService,OptionService $optionService)
    {
        $this->complaintService = $complaintService;
        $this->optionService = $optionService;
    }

    /**
     * Display a listing of the complaints.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $title= __('complaints.head');
        $statuses = ComplaintStatusEnum::values();
        $request->merge(['type'=>OptionStatusEnum::COMPLAINT->value]);
        $reasons = $this->optionService->get($request);
        return view($this->viewDirectory.'index', get_defined_vars());
    }

    /**
     * return data of the complaints.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) //:View
    {
        $request->merge(['paginate'=> true]);
        $complaints= $this->complaintService->get($request);

        return view($this->viewDirectory.'grid',compact('complaints'));
    }

    /**
     * Show the form for creating a new Complaint.
     *
     * @return View
     */
    public function create() :View
    {
        $action= route('complaints.store');
        $head= __('complaints.new');
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Show the form for editing the specified Complaint.
     *
     * @param  Complaint  $complaint
     * @return View
     */
    public function edit(Complaint $complaint) :View
    {
        // dd($complaint->id);
        $action= route('complaints.update', $complaint->id);
        $data= $this->complaintService->getItem($complaint);
        $head= __('complaints.edit');
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }


    /**
     * Remove the specified Complaint from storage.
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws Exception
     */
    public function destroy(Request $request, int $id) :JsonResponse
    {
        $return= $this->complaintService->destroy($request, $id);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Remove several complaints by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function destroyAll(Request $request) :JsonResponse
    {
        $return= $this->complaintService->destroyAll($request);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Restore the specified Complaint from storage.
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function restore(Request $request, int $id) :JsonResponse
    {
        $return= $this->complaintService->restore($request, $id);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Restore several complaints by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function restoreAll(Request $request) :JsonResponse
    {
        $return= $this->complaintService->restoreAll($request);
        return response()->json($return[0], $return[1] ?? 200);
    }
}
