<?php

namespace Modules\School\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\City\Services\CityService;
use Modules\School\Requests\Admin\SchoolRequest;
use Modules\School\Services\SchoolService;

class SchoolController extends Controller
{

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

    public function __construct(
        private SchoolService $schoolService,
        private CityService $cityService,
    )
    {}

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

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

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

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

    /**
     * Store a newly created School in storage.
     * @param SchoolRequest $request
     * @return JsonResponse
     */
    public function store(SchoolRequest $request) :JsonResponse
    {
        $this->schoolService->create($request->validated());

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

    /**
     * Show the form for editing the specified School.
     *
     * @param  int $id
     * @return View
     */
    public function edit(int $id) :View
    {
        $school = $this->schoolService->getItem($id);
        if (!$school)
        {
            abort(404);
        }
        $action= route('schools.update', $school->id);
        $head= __('schools.edit');
        $cities = $this->cityService->get();

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

    /**
     * Update the specified School in storage.
     * @param SchoolRequest $request
     * @param int $id
     * @return JsonResponse
     */
    public function update(SchoolRequest $request, int $id) :JsonResponse
    {
        $school = $this->schoolService->checkItem($id);
        if (!$school)
        {
            abort(404);
        }
        $this->schoolService->update($request->validated(), $id);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('schools.index'),
        ];
        return ResponseService::response($response);
    }

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

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

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

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