<?php

namespace Modules\Supplier\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\Supplier\Requests\Admin\SupplierRequest;
use Modules\Supplier\Services\SupplierService;
use Modules\Country\Services\CountryService;

class SupplierController extends Controller
{

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

    public function __construct(
        private SupplierService $supplierService,
        private CountryService $countryService,
    )
    {}

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

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

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

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

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

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

    /**
     * Show the form for editing the specified Supplier.
     *
     * @param  int $id
     * @return View
     */
    public function edit(int $id) :View
    {
        $supplier = $this->supplierService->getItem($id);
        if (!$supplier)
        {
            abort(404);
        }
        $action= route('suppliers.update', $supplier->id);
        $head= __('suppliers.edit');
        $countries = $this->countryService->get([]);
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

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

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

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

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

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