<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST');
header('Access-Control-Allow-Headers: Content-Type');

// Log tudo que chega
$logFile = dirname(dirname(__FILE__)) . '/logs/debug_endpoint.log';
$timestamp = date('Y-m-d H:i:s');

// Capturar dados
$method = $_SERVER['REQUEST_METHOD'];
$headers = getallheaders();
$rawInput = file_get_contents('php://input');
$postData = $_POST;

// Log completo
$logData = [
    'timestamp' => $timestamp,
    'method' => $method,
    'headers' => $headers,
    'raw_input' => $rawInput,
    'post_data' => $postData,
    'get_data' => $_GET,
    'server' => $_SERVER
];

file_put_contents($logFile, json_encode($logData, JSON_PRETTY_PRINT) . "\n\n", FILE_APPEND);

// Resposta simples
if ($method === 'POST') {
    $input = json_decode($rawInput, true) ?: $postData;
    
    echo json_encode([
        'success' => true,
        'message' => 'Debug endpoint funcionando!',
        'received_data' => $input,
        'timestamp' => $timestamp
    ]);
} else {
    echo json_encode([
        'success' => false,
        'message' => 'Apenas POST permitido',
        'method_received' => $method
    ]);
}
?>
