<?php
/**
 * API Dashboard pour desktop app - Version autonome
 */

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type");
header("Content-Type: application/json; charset=UTF-8");

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    http_response_code(200);
    exit;
}

// Connexion directe
$host = '127.0.0.1';
$dbname = 'transubd_GGR';
$user = 'transubd_transub_user';
$pass = 'Tr@nsub2@24';

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $user, $pass);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo json_encode(['success' => false, 'message' => 'Erreur BD: ' . $e->getMessage()]);
    exit;
}

$action = $_GET['action'] ?? 'stats';

switch ($action) {
    case 'stats':
        // Statistiques du dashboard
        $stats = [];
        
        // Ventes du jour
        $stmt = $pdo->query("SELECT COUNT(*) as total FROM Tickets WHERE DATE(DateAchat) = CURDATE()");
        $stats['ventes'] = $stmt->fetch(PDO::FETCH_ASSOC)['total'] ?? 0;
        
        // Départs ouverts
        $stmt = $pdo->query("SELECT COUNT(*) as total FROM Departs WHERE Etat_Depart = 'O'");
        $stats['departs_ouverts'] = $stmt->fetch(PDO::FETCH_ASSOC)['total'] ?? 0;
        
        // Recettes du jour
        $stmt = $pdo->query("SELECT COALESCE(SUM(PrixTotal), 0) as total FROM Tickets WHERE DATE(DateAchat) = CURDATE()");
        $stats['recettes'] = floatval($stmt->fetch(PDO::FETCH_ASSOC)['total'] ?? 0);
        
        // Consignes actives
        $stmt = $pdo->query("SELECT COUNT(*) as total FROM Consignes_Bagages WHERE DateRetrait IS NULL");
        $stats['consignes'] = $stmt->fetch(PDO::FETCH_ASSOC)['total'] ?? 0;
        
        echo json_encode(['success' => true, 'data' => $stats]);
        break;
        
    case 'ventes':
        $stmt = $pdo->query("SELECT * FROM Tickets ORDER BY DateAchat DESC LIMIT 50");
        echo json_encode(['success' => true, 'data' => $stmt->fetchAll(PDO::FETCH_ASSOC)]);
        break;
        
    case 'departements':
        $stmt = $pdo->query("SELECT d.*, b.Modele_Bus, b.Ligne_Bus FROM Departs d LEFT JOIN BusGares b ON d.BusID = b.BusID WHERE d.Etat_Depart = 'O' ORDER BY d.DateDepart DESC");
        echo json_encode(['success' => true, 'data' => $stmt->fetchAll(PDO::FETCH_ASSOC)]);
        break;
        
    case 'consignes':
        try {
            $stmt = $pdo->query("SELECT * FROM Consignes ORDER BY DateDepot DESC LIMIT 50");
            $data = $stmt->fetchAll(PDO::FETCH_ASSOC);
            echo json_encode(['success' => true, 'data' => $data]);
        } catch (Exception $e) {
            // Table might not exist, try alternate names
            try {
                $stmt = $pdo->query("SELECT * FROM ConsignesBagages ORDER BY DateDepot DESC LIMIT 50");
                echo json_encode(['success' => true, 'data' => $stmt->fetchAll(PDO::FETCH_ASSOC)]);
            } catch (Exception $e2) {
                echo json_encode(['success' => true, 'data' => []]);
            }
        }
        break;
        
    default:
        echo json_encode(['success' => false, 'message' => 'Action non reconnue']);
}