<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Repositories\Interfaces\ArtTagInterface;
use Illuminate\Http\Request;

class ArtTagController extends Controller
{
    protected $tagRepo;

    public function __construct(ArtTagInterface $tagRepo)
    {
        $this->tagRepo = $tagRepo;
    }

    public function index(Request $request)
    {
        $tags = $this->tagRepo->list($request);
        return view('Admin.ArtTag.list', compact('tags'));
    }

    public function store(Request $request)
    {
        $request->validate(['name_en' => 'required|string|max:255']);
        $tag = $this->tagRepo->store(['name_en' => $request->name_en, 'status' => 1]);
        return response()->json(['success' => true, 'tag' => $tag]);
    }

    public function edit($id)
    {
        $tag = $this->tagRepo->find($id);
        return response()->json(['success' => true, 'tag' => $tag]);
    }

    public function update(Request $request, $id)
    {
        $request->validate(['name_en' => 'required|string|max:255']);
        $tag = $this->tagRepo->update($id, ['name_en' => $request->name_en]);
        return response()->json(['success' => true, 'tag' => $tag]);
    }

    public function destroy($id)
    {
        $this->tagRepo->delete($id);
        return response()->json(['success' => true]);
    }

    // For select2 ajax
    public function ajaxList(Request $request)
    {
        $tags = $this->tagRepo->getAllActive();
        return response()->json($tags);
    }
}
