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

require_once __DIR__ . '/../service/ConcoursService.php';

$service = new ConcoursService();

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

try {
    switch ($method) {
        case 'GET':
            if ($action === 'search') {
                $searchData = [
                    'code' => isset($_GET['code']) ? $_GET['code'] : '',
                    'label' => isset($_GET['label']) ? $_GET['label'] : '',
                    'filliere' => isset($_GET['filliere']) ? $_GET['filliere'] : '',
                    'etat_concours' => isset($_GET['etat_concours']) ? $_GET['etat_concours'] : '',
                    'active' => isset($_GET['active']) ? $_GET['active'] : ''
                ];
                $page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
                $perPage = isset($_GET['per_page']) ? (int)$_GET['per_page'] : 10;

                $result = $service->search($searchData, $page, $perPage);
                echo json_encode(['success' => true, 'data' => $result]);
            } elseif ($action === 'get' && isset($_GET['id'])) {
                $data = $service->getConcoursWithDetails($_GET['id']);
                if ($data) {
                    echo json_encode(['success' => true, 'data' => $data]);
                } else {
                    echo json_encode(['success' => false, 'message' => 'Concours not found']);
                }
            } elseif ($action === 'professors') {
                $data = $service->getProfessors();
                echo json_encode(['success' => true, 'data' => $data]);
            } elseif ($action === 'types_diplome') {
                $data = $service->getAllTypeDiplome();
                echo json_encode(['success' => true, 'data' => $data]);
            } elseif ($action === 'stats') {
                $data = $service->getStats();
                echo json_encode(['success' => true, 'data' => $data]);
            } else {
                $data = $service->findAll('id DESC');
                echo json_encode(['success' => true, 'data' => $data]);
            }
            break;

        case 'POST':
            $input = json_decode(file_get_contents('php://input'), true);
            if ($input) {
                $id = $service->createConcours($input);
                echo json_encode(['success' => true, 'id' => $id, 'message' => 'Concours 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->updateConcours($_GET['id'], $input);
                    echo json_encode(['success' => true, 'message' => 'Concours 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->deleteConcours($_GET['id']);
                echo json_encode(['success' => true, 'message' => 'Concours 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']);
    }
ini_set('display_errors', 1);
error_reporting(E_ALL);

} catch (Throwable $e) {

    echo "<pre>";
    print_r($e);
    exit;

}
?>
