<?php
header('Content-Type: application/json; charset=utf-8');
require_once 'config/database.php';

try {
    // Définition des lots (identique à get_classement.php)
    $lots = [
        1 => ["nom" => "Lot 1: Local Cafeterie", "prix_ouverture" => 100000],
        2 => ["nom" => "Lot 2: Local FastFood", "prix_ouverture" => 100000],
        3 => ["nom" => "Lot 3: Local Superette", "prix_ouverture" => 70000],
        4 => ["nom" => "Lot 4: Local Kiosque Multi Service", "prix_ouverture" => 50000]
    ];

    // Formater les lots pour l'application Android (format attendu: [{id: {data}}])
    $lotsFormatted = [];
    foreach ($lots as $id => $lot) {
        $lotsFormatted[] = [
            $id => [
                "nom" => $lot["nom"],
                "prix_ouverture" => $lot["prix_ouverture"]
            ]
        ];
    }

    // 1. Total Retraits
    $stmtRetrait = $pdo->query("SELECT COUNT(*) as count, SUM(Montant) as total_montant FROM cahier_des_charges");
    $rowRetrait = $stmtRetrait->fetch(PDO::FETCH_ASSOC);
    $totalRetraits = $rowRetrait['count'];
    $totalMontant = $rowRetrait['total_montant'] ? $rowRetrait['total_montant'] : 0;

    // 2. Total Depots
    $stmtDepot = $pdo->query("SELECT COUNT(*) as count FROM cahier_des_charges WHERE Date_Depot IS NOT NULL AND Date_Depot != '' AND Date_Depot != '0000-00-00'");
    $rowDepot = $stmtDepot->fetch(PDO::FETCH_ASSOC);
    $totalDepots = $rowDepot['count'];

    echo json_encode([
        'status' => 'OK',
        'total_retraits' => $totalRetraits,
        'total_montant' => $totalMontant,
        'total_depots' => $totalDepots,
        'lots' => $lotsFormatted
    ]);

} catch (PDOException $e) {
    echo json_encode(['status' => 'ERR', 'message' => $e->getMessage()]);
}
?>