<?php

namespace Modules\Tag\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\Tag\Requests\Admin\TagRequest;
use Modules\Tag\Services\TagService;
use Modules\Currency\Services\CurrencyService;

class TagController extends Controller
{

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

    public function __construct(
        private TagService $tagService,
    )
    {}

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

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

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

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

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

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

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

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

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

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

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

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