<?php
require_once __DIR__ . '/../service/TypeAvancementService.php';

header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') exit(0);

$service = new TypeAvancementService();
$action  = $_GET['action'] ?? '';
$method  = $_SERVER['REQUEST_METHOD'];

try {
    switch ($method) {
        case 'GET':
            if ($action === 'all') {
                echo json_encode(['success' => true, 'data' => $service->findAll()]);

            } elseif ($action === 'get' && isset($_GET['id'])) {
                $data = $service->getTypeAvancement($_GET['id']);
                echo json_encode($data
                    ? ['success' => true, 'data' => $data]
                    : ['success' => false, 'message' => 'Type avancement non trouvé']);

            } elseif ($action === 'search') {
                $searchData = [
                    'code'    => $_GET['code']    ?? '',
                    'libelle' => $_GET['libelle'] ?? '',
                ];
                $page    = (int)($_GET['page']     ?? 1);
                $perPage = (int)($_GET['per_page'] ?? 10);
                echo json_encode(['success' => true, 'data' => $service->search($searchData, $page, $perPage)]);

            } elseif ($action === 'stats') {
                $all   = $service->findAll();
                $total = count($all);
                echo json_encode(['success' => true, 'data' => ['total' => $total]]);

            } else {
                echo json_encode(['success' => true, 'data' => $service->findAll()]);
            }
            break;

        case 'POST':
            $input = json_decode(file_get_contents('php://input'), true);
            if ($input) {
                $id = $service->createTypeAvancement($input);
                echo json_encode(['success' => true, 'id' => $id, 'message' => 'Type avancement créé avec succès']);
            } else {
                echo json_encode(['success' => false, 'message' => 'Données invalides']);
            }
            break;

        case 'PUT':
            if (isset($_GET['id'])) {
                $input = json_decode(file_get_contents('php://input'), true);
                if ($input) {
                    $service->updateTypeAvancement($_GET['id'], $input);
                    echo json_encode(['success' => true, 'message' => 'Type avancement mis à jour avec succès']);
                } else {
                    echo json_encode(['success' => false, 'message' => 'Données invalides']);
                }
            } else {
                echo json_encode(['success' => false, 'message' => 'ID requis']);
            }
            break;

        case 'DELETE':
            if (isset($_GET['id'])) {
                $service->deleteTypeAvancement($_GET['id']);
                echo json_encode(['success' => true, 'message' => 'Type avancement supprimé avec succès']);
            } else {
                echo json_encode(['success' => false, 'message' => 'ID requis']);
            }
            break;

        default:
            echo json_encode(['success' => false, 'message' => 'Méthode non autorisée']);
    }
} catch (Exception $e) {
    echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}
?>