<?php

namespace App\Http\Controllers\Backend;

use App\DataTables\ProductManagement\Tags\TagDataTable;
use App\DataTables\ProductManagement\Tags\TagDataTableEditor;
use App\Http\Controllers\Controller;
use App\Http\Requests\Backend\TagRequest;
use App\Models\ProductManagement\Tag;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;

class TagController extends Controller{

    public function __construct(){

        $this->middleware('permission:tag-list')
            ->only(["index"]);

        $this->middleware('permission:tag-show')
            ->only(["show"]);

        $this->middleware('permission:tag-create')
            ->only(["store"]);

        $this->middleware('permission:tag-update')
            ->only(['update',"edit"]);

        $this->middleware('permission:tag-delete')
            ->only(['destroy']);

    }


    public function index(TagDataTable $dataTable){
        $title = __("Manage Tags");
        return $dataTable->render('backend.tags.index',compact("title"));
    }


    public function create(): View{
        return view('backend.tags.create');
    }

    public function store1(TagRequest $request): RedirectResponse{
        Tag::create($request->validated());

        return redirect()->route('admin.tags.index')->with([
            'message' => action_message("create"),
            'alert-type' => 'success'
        ]);
    }

    public function show(Tag $tag): View{
        return view('backend.tags.show', compact('tag'));
    }

    public function edit(Tag $tag): View{
        return view('backend.tags.edit', compact('tag'));
    }

    public function store(TagDataTableEditor $editor){
        return $editor->process(request());
    }

    public function destroy(Tag $tag): RedirectResponse{
        $tag->delete();

        return redirect()->route('admin.tags.index')->with([
            'message' => action_message("delete"),
            'alert-type' => 'success'
        ]);
    }
}
