<?php

namespace App\Http\Controllers;

use App\Models\Blog;
use App\Models\BlogCategory;
use Illuminate\Http\Request;

class BlogCategoryController extends Controller
{
    public function index()
    {
        $categories = BlogCategory::latest()->get();
        return view('admin.blog.category', compact('categories'));
    }

    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:255|unique:blog_categories,name',
        ]);

        BlogCategory::create(['name' => $request->name]);

        return response()->json([
            'success' => true,
            'message' => 'Category created successfully'
        ]);
    }

    public function destroy(BlogCategory $category)
    {
        $existgallery = Blog::where('category_id', $category->id)->first();
        if ($existgallery) {
            return response()->json([
                'success' => false,
                'message' => 'Cannot delete category with associated blogs.'
            ], 400);
        }
        $category->delete();
        return response()->json([
            'success' => true,
            'message' => 'Category deleted successfully'
        ]);
    }
}
