<?php

namespace App\Http\Controllers;

use App\Models\Club;
use Illuminate\Http\Request;

class ClubController extends Controller
{
    public function index()
    {
        $clubes = Club::with('equipos')->get();
        return response()->json($clubes);
    }

    public function show($id)
    {
        $club = Club::with('equipos', 'jugadores')->findOrFail($id);
        return response()->json($club);
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'nombre' => 'required|string|max:100',
        ]);
        $club = Club::create($data);
        return response()->json($club, 201);
    }

    public function update(Request $request, $id)
    {
        $club = Club::findOrFail($id);
        $data = $request->validate([
            'nombre' => 'sometimes|string|max:100',
        ]);
        $club->update($data);
        return response()->json($club);
    }

    public function destroy($id)
    {
        $club = Club::findOrFail($id);
        $club->delete();
        return response()->json(null, 204);
    }
}
