<?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 Illuminate\Routing\Redirector;
use Illuminate\Http\RedirectResponse;
use App\Http\Controllers\Controller;
use Modules\Attribute\Enums\AttributeStatusEnum;
use Modules\Attribute\Models\Attribute;
use Modules\Attribute\Requests\Admin\AttributeRequest;
use Modules\Attribute\Services\AttributeService;

class AttributeController extends Controller
{

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

    public function __construct(AttributeService $attributeService)
    {
        $this->attributeService = $attributeService;
    }

    /**
     * Display a listing of the attributes.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $title= __('attributes.head');
        $statuses = AttributeStatusEnum::values();
        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);

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

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

    /**
     * Store a newly created Attribute in storage.
     * @param AttributeRequest $request
     * @return RedirectResponse|Redirector
     */
    public function store(AttributeRequest $request) :JsonResponse
    {
        $attribute= $this->attributeService->save($request, null);

        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  Attribute  $attribute
     * @return View
     */
    public function edit(Attribute $attribute) :View
    {
        // dd($attribute->id);
        $action= route('attributes.update', $attribute->id);
        $data= $this->attributeService->getItem($attribute);
        $head= __('attributes.edit');
        $statuses = AttributeStatusEnum::values();
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Update the specified Attribute in storage.
     * @param AttributeRequest $request
     * @param Attribute $attribute
     * @return RedirectResponse|Redirector
     */
    public function update(AttributeRequest $request, Attribute $attribute) :JsonResponse
    {
        $attribute= $this->attributeService->save($request, $attribute);
        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 Request $request
     * @param int $id
     * @return JsonResponse
     * @throws Exception
     */
    public function destroy(Request $request, int $id) :JsonResponse
    {
        $return= $this->attributeService->destroy($request, $id);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Remove several attributes by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function destroyAll(Request $request) :JsonResponse
    {
        $return= $this->attributeService->destroyAll($request);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Restore the specified Attribute from storage.
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function restore(Request $request, int $id) :JsonResponse
    {
        $return= $this->attributeService->restore($request, $id);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Restore several attributes by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function restoreAll(Request $request) :JsonResponse
    {
        $return= $this->attributeService->restoreAll($request);
        return response()->json($return[0], $return[1] ?? 200);
    }
}
