<?php

namespace Modules\Brand\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\Brand\Requests\Admin\BrandRequest;
use Modules\Brand\Services\BrandService;
use Modules\Currency\Services\CurrencyService;

class BrandController extends Controller
{

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

    public function __construct(
        private BrandService $brandService,
    )
    {}

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

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

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

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

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

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

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

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

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

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

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

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