<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Genre;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class GenreController extends Controller
{
    public function index()
    {
        $genres = Genre::withCount('animes')->get();
        return view('admin.genres.index', compact('genres'));
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'name'  => 'required|string|max:100',
            'color' => 'nullable|string|max:7',
        ]);
        $data['slug'] = Str::slug($data['name']);
        Genre::create($data);
        return back()->with('success', 'Tür eklendi.');
    }

    public function update(Request $request, Genre $genre)
    {
        $data = $request->validate([
            'name'      => 'required|string|max:100',
            'color'     => 'nullable|string|max:7',
            'is_active' => 'boolean',
        ]);
        $data['is_active'] = $request->boolean('is_active');
        $genre->update($data);
        return back()->with('success', 'Tür güncellendi.');
    }

    public function destroy(Genre $genre)
    {
        $genre->delete();
        return back()->with('success', 'Tür silindi.');
    }
}
