<?php
/**
 * API: Vérification des doublons de doléances
 * POST /apipcc/doleances_check.php
 * Paramètres: flotte, chauffeur, probleme
 */
require_once __DIR__ . '/config.php';

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    sendError('Méthode non autorisée', 405);
}

$data = getPostData();
$flotte = $data['flotte'] ?? '';
$chauffeur = $data['chauffeur'] ?? '';
$probleme = $data['probleme'] ?? '';

if (empty($flotte) || empty($chauffeur) || empty($probleme)) {
    sendError('flotte, chauffeur et probleme sont requis');
}

$conn = getConnection(DB_NAME_PARCS);

$query = "SELECT COUNT(*) as count FROM doleances 
          WHERE flotte = ? AND chauffeur = ? AND probleme = ? 
          AND DATE(date_creation) = CURDATE()";
$stmt = $conn->prepare($query);
$stmt->bind_param("sss", $flotte, $chauffeur, $probleme);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();

sendSuccess(['count' => (int)$row['count']], 'Vérification effectuée');
$stmt->close();
$conn->close();