<?php

require_once __DIR__ . '/../vendor/autoload.php';

use App\Config\Database;

// Configuração: Dias para manter os logs (Default: 30)
$daysToKeep = getenv('LOG_RETENTION_DAYS') ?: 30;

try {
    $conn = Database::getInstance()->getConnection();

    echo "Iniciando limpeza de logs antigos (> $daysToKeep dias)...\n";

    // Calcular data de corte
    $cutoffDate = date('Y-m-d H:i:s', strtotime("-$daysToKeep days"));

    // Executar limpeza
    $stmt = $conn->prepare("DELETE FROM api_logs WHERE created_at < :cutoff");
    $stmt->execute(['cutoff' => $cutoffDate]);

    $deletedCount = $stmt->rowCount();

    echo "Limpeza concluída. $deletedCount registros removidos.\n";

} catch (Exception $e) {
    echo "Erro: " . $e->getMessage() . "\n";
    exit(1);
}
