<?php

namespace Modules\Attribute\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\Attribute\Requests\Admin\AttributeRequest;
use Modules\Attribute\Services\AttributeService;
use Modules\Option\Services\OptionService;

class AttributeController extends Controller
{

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

    public function __construct(
        private AttributeService $attributeService,
        private OptionService $optionService
    )
    {}

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

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

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

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

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

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

    /**
     * Show the form for editing the specified Attribute.
     *
     * @param  int $id
     * @return View
     */
    public function edit(int $id) :View
    {
        $attribute = $this->attributeService->getItem($id);
        if (!$attribute)
        {
            abort(404);
        }
        $action= route('attributes.update', $attribute->id);
        $head= __('attributes.edit');
        $choices = $this->optionService->get();
        $attr_choices = $this->attributeService->choices($id);
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    public function choices(Request $request)
    {
        $choices = $this->optionService->get();
        return view( $this->viewDirectory.'choices', get_defined_vars() );
    }

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

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

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

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

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