<?php

namespace App\Http\Controllers;

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

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

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

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

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

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

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

    public function destroy(Label $label): JsonResponse
    {
        $label->delete();

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