<?php

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

use App\Models\Category;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Auth;

class CategoryController extends Controller
{
    public function list()
    {
        $categories = Category::with('createdBy')->get();
        return view('admin.Category.list', compact('categories'));
    }

    public function Add()
    {
        $title = "Create Category";
        $url = route('category.store');
        return view('admin.Category.create', compact('title', 'url'));
    }
    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required|unique:categories,name',
            'slug' => 'nullable|unique:categories,slug',
            'description' => 'nullable|max:255',
            'meta_description' => 'nullable|max:255',
            'meta_keywords' => 'nullable|max:255',
            'status' => 'required|in:0,1',
        ]);

        $data = new Category();
        $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_by = Auth::id();
        $data->created_at = now();
        $data->updated_at = now();

        $data->save();

        return redirect()->route('category.list')->with('success', 'Category created successfully!');
    }
    public function Edit($id)
    {
        $category = Category::findOrFail($id);
        $title = "Edit Category";
        $url = route('update.category', $id);
        return view('admin.Category.create', compact('title', 'url', 'category'));
    }
    public function update(Request $request, $id)
    {
        $request->validate([
            'name' => 'required|unique:categories,name,' . $id,
            'slug' => 'nullable|unique:categories,slug,' . $id,
            'description' => 'nullable|max:255',
            'meta_description' => 'nullable|max:255',
            'meta_keywords' => 'nullable|max:255',
            'status' => 'required|in:0,1',
        ]);

        $category = Category::findOrFail($id);

        $category->name = $request->name;
        $category->slug = $request->slug ? Str::slug($request->slug, '-') : Str::slug($request->name, '-');
        $category->description = $request->description;
        $category->meta_description = $request->meta_description;
        $category->meta_keywords = $request->meta_keywords;
        $category->status = $request->status;
        $category->updated_at = now();

        $category->save();

        return redirect()->route('category.list')->with('success', 'Category updated successfully!');
    }

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

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

    public function destroy($id)
    {
        // Find the category and delete it
        $category = Category::findOrFail($id);
        $category->delete();

        // Redirect with a success message after deletion
        return redirect()->route('category.list')->with('success', 'Category deleted successfully!');
    }
}
