<?php

namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller; 

use Illuminate\Http\Request;
use App\Models\Tag;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Validator;

class TagController extends Controller
{
    public function List()
    {
        $tags = Tag::get();
        return view('admin.tag.list', compact('tags'));
    }
    public function Add()
    {
        $title = "Create Tag";
        $url = route('tag.store');
        return view('admin.tag.create', compact('title', 'url'));
    }

    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required|unique:tags,name',
            'slug' => 'nullable|unique:tags,slug',
            'description' => 'nullable|max:255',
            'meta_description' => 'nullable|max:255',
            'meta_keywords' => 'nullable|max:255',
            'status' => 'required|in:0,1',

        ]);
        $data = new Tag();
        $data->name = $request->name;
        $data->slug = $request->slug ? Str::slug($request->slug, '-') : Str::slug($request->name, '-');
        $data->description = $request->description;
        $data->meta_description = $request->meta_description ?: Str::limit(strip_tags($request->description), 150);
        $data->meta_keywords = $request->meta_keywords ?: implode(',', array_slice(explode(' ', $request->description), 0, 5));
        $data->status = $request->status;
        $data->created_at = now();
        $data->updated_at = now();

        $data->save();

        return redirect()->route('tag.list')->with('success', 'Tag created successfully!');
    }


    public function Edit($id)
    {
        $tag = Tag::findOrFail($id);
        $title = "Edit Tag";
        $url = route('update.tag', $id);
        return view('admin.tag.create', compact('title', 'url', 'tag'));
    }
    public function Update(Request $request, $id)
    {

        $request->validate([
            'name' => 'required|unique:tags,name,' . $id,
            'slug' => 'nullable|unique:tags,slug' . $id,
            'description' => 'nullable|max:255',
            'meta_description' => 'nullable|max:255',
            'meta_keywords' => 'nullable|max:255',
            'status' => 'required|in:0,1',
        ]);


        $data = Tag::findOrFail($id);
        $data->name = $request->name;
        $data->slug = $request->slug ? Str::slug($request->slug, '-') : Str::slug($request->name, '-');
        $data->description = $request->description;
        $data->meta_description = $request->meta_description ?: Str::limit(strip_tags($request->description), 150);
        $data->meta_keywords = $request->meta_keywords ?: implode(',', array_slice(explode(' ', $request->description), 0, 5));
        $data->status = $request->status;
        $data->updated_at = now();
        $data->save();
        return redirect()->route('tag.list')->with('success', 'tag updated successfully!');
    }

    public function tagstore(Request $request)
    {
        $request->validate([
            'name' => 'required|unique:tags,name',
        ]);

        $data = new Tag();
        $data->name = $request->name;
        $data->created_at = now();
        $data->updated_at = now();
        $data->slug = Str::slug($request->name, '-');
        $data->save();

        return response()->json([
            'success' => true,
            'tag' => [
                'id' => $data->id,
                'name' => $data->name,
            ]
        ]);
    }

    public function changeStatusAjax(Request $request)
    {
        $tag = Tag::findOrFail($request->id);
        $tag->status = $tag->status == 1 ? 0 : 1;
        $tag->save();

        return response()->json([
            'success' => true,
            'status' => $tag->status,
            'message' => 'Status updated successfully.',
        ]);
    }

    public function destroy($id)
    {
        $tag = Tag::findOrFail($id);
        $tag->delete();

        return redirect()->route('tag.list')->with('success', 'Tag deleted successfully!');
    }

}
