<?php
// model/api/admin.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/AdminService.php';

$service = new AdminService();

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

try {
    switch ($method) {
        case 'GET':
            if ($action === 'stats') {
                // Get dashboard statistics
                $result = $service->getDashboardStats();
                echo json_encode(['success' => true, 'data' => $result]);
            } elseif ($action === 'inscriptions') {
                // Get inscriptions data for chart
                $result = $service->getInscriptionsData();
                echo json_encode(['success' => true, 'data' => $result]);
            } elseif ($action === 'demandes') {
                // Get demandes data for chart
                $result = $service->getDemandesData();
                echo json_encode(['success' => true, 'data' => $result]);
            } elseif ($action === 'avancements') {
                // Get avancements data for chart
                $result = $service->getAvancementsData();
                echo json_encode(['success' => true, 'data' => $result]);
            } elseif ($action === 'concours') {
                // Get concours data
                $result = $service->getConcoursData();
                echo json_encode(['success' => true, 'data' => $result]);
            } else {
                // Get all admins or specific admin
                if (isset($_GET['id'])) {
                    $data = $service->getAdmin($_GET['id']);
                    if ($data) {
                        echo json_encode(['success' => true, 'data' => $data]);
                    } else {
                        echo json_encode(['success' => false, 'message' => 'Admin not found']);
                    }
                } else {
                    $data = $service->findAll('id DESC');
                    echo json_encode(['success' => true, 'data' => $data]);
                }
            }
            break;

        case 'POST':
            // Create new admin
            $input = json_decode(file_get_contents('php://input'), true);
            if ($input) {
                $id = $service->createAdmin($input);
                echo json_encode(['success' => true, 'id' => $id, 'message' => 'Admin created successfully']);
            } else {
                echo json_encode(['success' => false, 'message' => 'Invalid input data']);
            }
            break;

        case 'PUT':
            // Update admin
            if (isset($_GET['id'])) {
                $input = json_decode(file_get_contents('php://input'), true);
                if ($input) {
                    $service->updateAdmin($_GET['id'], $input);
                    echo json_encode(['success' => true, 'message' => 'Admin updated successfully']);
                } else {
                    echo json_encode(['success' => false, 'message' => 'Invalid input data']);
                }
            } else {
                echo json_encode(['success' => false, 'message' => 'ID required']);
            }
            break;

        case 'DELETE':
            // Delete admin
            if (isset($_GET['id'])) {
                $service->deleteAdmin($_GET['id']);
                echo json_encode(['success' => true, 'message' => 'Admin deleted successfully']);
            } else {
                echo json_encode(['success' => false, 'message' => 'ID required']);
            }
            break;

        default:
            echo json_encode(['success' => false, 'message' => 'Method not allowed']);
    }
} catch (Exception $e) {
    echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}
?>