<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\InterviewCategory;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;

class InterviewCategoryController extends Controller
{
    public function index(): View
    {
        $categories = InterviewCategory::withCount('questions', 'flashcards')
            ->orderBy('name')
            ->paginate(15);

        return view('admin.interview-categories.index', compact('categories'));
    }

    public function create(): View
    {
        return view('admin.interview-categories.create');
    }

    public function store(Request $request): RedirectResponse
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'slug' => 'required|string|max:255|unique:interview_categories,slug',
            'description' => 'nullable|string',
            'icon' => 'nullable|string|max:50',
            'color' => 'nullable|string|max:20',
            'time_limit' => 'nullable|integer|min:1|max:180',
        ]);

        InterviewCategory::create($validated);

        return redirect()->route('admin.interview-categories.index')
            ->with('success', 'Kategori başarıyla oluşturuldu.');
    }

    public function edit(InterviewCategory $category): View
    {
        return view('admin.interview-categories.edit', compact('category'));
    }

    public function update(Request $request, InterviewCategory $category): RedirectResponse
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'slug' => 'required|string|max:255|unique:interview_categories,slug,' . $category->id,
            'description' => 'nullable|string',
            'icon' => 'nullable|string|max:50',
            'color' => 'nullable|string|max:20',
            'time_limit' => 'nullable|integer|min:1|max:180',
        ]);

        $category->update($validated);

        return redirect()->route('admin.interview-categories.index')
            ->with('success', 'Kategori başarıyla güncellendi.');
    }

    public function destroy(InterviewCategory $category): RedirectResponse
    {
        $category->delete();

        return redirect()->route('admin.interview-categories.index')
            ->with('success', 'Kategori başarıyla silindi.');
    }
}
