<?php

namespace Modules\Type\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\Type\Enums\TypeBoolEnum;
use Modules\Type\Requests\Admin\TypeRequest;
use Modules\Type\Services\TypeService;
use Modules\Currency\Services\CurrencyService;

class TypeController extends Controller
{

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

    public function __construct(
        private TypeService $typeService,
    )
    {}

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

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

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

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

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

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

    /**
     * Show the form for editing the specified Type.
     *
     * @param  int $id
     * @return View
     */
    public function edit(int $id) :View
    {
        $type = $this->typeService->getItem($id);
        if (!$type)
        {
            abort(404);
        }
        $statuses = TypeBoolEnum::values();
        $action= route('types.update', $type->id);
        $head= __('types.edit');
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

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

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

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

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

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