<?php

namespace App\Http\Controllers;

use App\Models\Career;
use App\Models\CareerCategory;
use Illuminate\Http\Request;

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

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

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

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

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