<?php

namespace Modules\Canteen\Controllers\Admin;

/** Ajax Request */

use App\Services\Response\ResponseService;
use Exception;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use Modules\Canteen\Requests\Admin\CanteenRequest;
use Modules\Canteen\Services\CanteenService;
use Modules\PaymentLog\Enums\PaymentLogAccStatusEnum;
use Modules\School\Services\SchoolService;
use Modules\User\Enums\UserRoleEnum;
use Modules\User\Services\UserService;
use Modules\PaymentLog\Services\PaymentLogService;

class CanteenController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/admin/canteens';
    /** View folder */
    private $viewDirectory= 'admin.canteens.';
    //

    public function __construct(
        private CanteenService $canteenService,
        private SchoolService $schoolService,
        private UserService $userService,
        private PaymentLogService $paymentLogService,
    )
    {}

    /**
     * Display a listing of the canteens.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $title= __('canteens.head');
        $schools = $this->schoolService->get();
        return view($this->viewDirectory.'index', get_defined_vars());
    }

    /**
     * return data of the canteens.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) :View
    {
        $request->merge(['paginate'=>true]);
        $canteens= $this->canteenService->get($request->all());
        $canteen_status = PaymentLogAccStatusEnum::CANTEEN->value;
        return view($this->viewDirectory.'grid',get_defined_vars());
    }

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

    /**
     * Store a newly created Canteen in storage.
     * @param CanteenRequest $request
     * @return JsonResponse
     */
    public function store(CanteenRequest $request) :JsonResponse
    {
        $canteen = $this->canteenService->create($request->validated());
        $data = $request->validated();
        $data['role'] = UserRoleEnum::CANTEEN->value;
        $data['canteen_id'] = $canteen->id;
        $this->userService->create($data);

        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('canteens.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Show the form for editing the specified Canteen.
     *
     * @param  int $id
     * @return View
     */
    public function edit(int $id) :View
    {
        $canteen = $this->canteenService->getItem($id);
        if (!$canteen)
        {
            abort(404);
        }
        $action= route('canteens.update', $canteen->id);
        $head= __('canteens.edit');
        $schools = $this->schoolService->get();
        $user = $this->userService->canteenUser($id);
        $total =$this->paymentLogService->getTotal($id) ?? 0;

        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Update the specified Canteen in storage.
     * @param CanteenRequest $request
     * @param int $id
     * @return JsonResponse
     */
    public function update(CanteenRequest $request, int $id) :JsonResponse
    {
        $canteen = $this->canteenService->checkItem($id);
        if (!$canteen)
        {
            abort(404);
        }
        $this->canteenService->update($request->validated(), $id);
        $user = $this->userService->canteenUser($id);
        $data = $request->validated();
        if($request->photo??null){
            $data['photo']=$request->photo;
        }
        $data['role'] = UserRoleEnum::CANTEEN->value;
        if($user)
        {
        $this->userService->update($data,$user->id);
        }
        else
        {
            $data['canteen_id'] = $canteen->id;
            $this->userService->create($data);
        }
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('canteens.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Remove the specified Canteen from storage.
     * @param int $id
     * @return JsonResponse
     * @throws Exception
     */
    public function destroy(int $id) :JsonResponse
    {
        $this->canteenService->destroy($id);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('canteens.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Remove several canteens by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function destroyAll(Request $request) :JsonResponse
    {
        $this->canteenService->destroyAll([
            'ids' => $request->ids,
            'status' => 1
        ]);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('canteens.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Restore the specified Canteen from storage.
     *
     * @param int $id
     * @return JsonResponse
     */
    public function restore(int $id) :JsonResponse
    {
        $this->canteenService->destroy($id);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('canteens.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Restore several canteens by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function restoreAll(Request $request) :JsonResponse
    {
        $this->canteenService->destroyAll([
            'ids' => $request->ids,
            'status' => 0
        ]);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('canteens.index'),
        ];
        return ResponseService::response($response);
    }
}
