<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Departement;
use Illuminate\Http\JsonResponse;

class DepartementController extends Controller
{
    /**
     * Liste tous les départements
     * GET /api/v1/departements
     */
    public function index(): JsonResponse
    {
        $departements = Departement::with(['filieres'])
            ->valide()
            ->orderBy('sigledept', 'asc')
            ->get();

        return response()->json([
            'success' => true,
            'data' => $departements,
            'count' => $departements->count()
        ]);
    }

    /**
     * Afficher un département spécifique avec ses filières
     * GET /api/v1/departements/{id}
     */
    public function show(int $id): JsonResponse
    {
        $departement = Departement::with(['filieres.ecus'])
            ->find($id);

        if (!$departement) {
            return response()->json([
                'success' => false,
                'message' => 'Département non trouvé'
            ], 404);
        }

        return response()->json([
            'success' => true,
            'data' => $departement
        ]);
    }
}
