<?php

namespace App\Http\Controllers;

use App\Models\Genre;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class GenreController extends Controller
{
    public function index(): JsonResponse
    {
        return response()->json(Genre::all());
    }

    public function show(Genre $genre): JsonResponse
    {
        return response()->json($genre);
    }

    public function store(Request $request): JsonResponse
    {
        $data = $request->validate(['name' => 'required|string|max:100']);
        $genre = Genre::create($data);

        return response()->json($genre, 201);
    }

    public function update(Request $request, Genre $genre): JsonResponse
    {
        $data = $request->validate(['name' => 'sometimes|required|string|max:100']);
        $genre->update($data);

        return response()->json($genre);
    }

    public function destroy(Genre $genre): JsonResponse
    {
        $genre->delete();

        return response()->json(null, 204);
    }
}
