<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../../../config/database.php';

// Désactiver complètement l'authentification en local pour éviter les problèmes
$host = $_SERVER['HTTP_HOST'] ?? '';
$isLocal = in_array($host, ['localhost', '127.0.0.1']);

if ($isLocal || php_sapi_name() === 'cli') {
    // En local ou en CLI, aucune authentification requise
    // Ne rien faire, passer directement à la logique de l'API
} else {
    // En production, vérifier l'authentification
    session_start();
    if (!isset($_SESSION['user_code'])) {
        header('Content-Type: application/json');
        echo json_encode(['success' => false, 'message' => 'Non autorisé']);
        exit;
    }
}

$db = Database::getInstance();
$action = $_GET['action'] ?? '';

switch ($action) {
    case 'list':
        try {
            $sql = "SELECT * FROM Tarifications_Parking ORDER BY date_validite DESC";
            $result = $db->select($sql);
            echo json_encode(['success' => true, 'data' => $result]);
        } catch (Exception $e) {
            echo json_encode(['success' => false, 'message' => $e->getMessage()]);
        }
        break;
        
    case 'create':
        $data = json_decode(file_get_contents('php://input'), true);
        try {
            $sql = "INSERT INTO Tarifications_Parking (SiteID, designation, designationAR, prix, created_at) 
                    VALUES (?, ?, ?, ?, NOW())";
            $db->query($sql, [
                $data['SiteID'],
                $data['designation'],
                $data['designationAR'],
                $data['prix']
            ]);
            echo json_encode(['success' => true, 'message' => 'Tarification ajoutée']);
        } catch (Exception $e) {
            echo json_encode(['success' => false, 'message' => $e->getMessage()]);
        }
        break;
        
    case 'update':
        $id = $_GET['id'] ?? 0;
        $data = json_decode(file_get_contents('php://input'), true);
        try {
            $sql = "UPDATE Tarifications_Parking SET SiteID = ?, designation = ?, designationAR = ?, prix = ? WHERE id_tarif = ?";
            $db->query($sql, [
                $data['SiteID'],
                $data['designation'],
                $data['designationAR'],
                $data['prix'],
                $id
            ]);
            echo json_encode(['success' => true, 'message' => 'Tarification modifiée']);
        } catch (Exception $e) {
            echo json_encode(['success' => false, 'message' => $e->getMessage()]);
        }
        break;
        
    case 'delete':
        $id = $_GET['id'] ?? 0;
        try {
            $sql = "DELETE FROM Tarifications_Parking WHERE id_tarif = ?";
            $db->query($sql, [$id]);
            echo json_encode(['success' => true, 'message' => 'Tarification supprimée']);
        } catch (Exception $e) {
            echo json_encode(['success' => false, 'message' => $e->getMessage()]);
        }
        break;
        
    default:
        echo json_encode(['success' => false, 'message' => 'Action non reconnue']);
}
