<?php
// app/model/api/type_diplome.php
ini_set('display_errors', 1);
error_reporting(E_ALL);
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);
}
require_once __DIR__ . '/../service/TypeDiplomeService.php';

$service = new TypeDiplomeService();

$action = isset($_GET['action']) ? $_GET['action'] : '';
$method = $_SERVER['REQUEST_METHOD'];

try {
    switch ($method) {
        case 'GET':
            if ($action === 'list') {
                $data = $service->getAll();
                echo json_encode(['success' => true, 'data' => $data]);
            } elseif ($action === 'get' && isset($_GET['id'])) {
                $data = $service->getById($_GET['id']);
                if ($data) {
                    echo json_encode(['success' => true, 'data' => $data]);
                } else {
                    echo json_encode(['success' => false, 'message' => 'Type de diplôme non trouvé']);
                }
            } elseif ($action === 'search') {
                $keyword = isset($_GET['q']) ? $_GET['q'] : '';
                $data = $service->search($keyword);
                echo json_encode(['success' => true, 'data' => $data]);
            } elseif ($action === 'with_usage') {
                $data = $service->getWithUsageCount();
                echo json_encode(['success' => true, 'data' => $data]);
            } else {
                $data = $service->getAll();
                echo json_encode(['success' => true, 'data' => $data]);
            }
            break;

        case 'POST':
            $input = json_decode(file_get_contents('php://input'), true);
            if ($input) {
                $id = $service->createTypeDiplome($input);
                echo json_encode(['success' => true, 'id' => $id, 'message' => 'Type de diplôme 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->updateTypeDiplome($_GET['id'], $input);
                    echo json_encode(['success' => true, 'message' => 'Type de diplôme 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->deleteTypeDiplome($_GET['id']);
                echo json_encode(['success' => true, 'message' => 'Type de diplôme 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()]);
}
?>

