<?php

require 'vendor/autoload.php';

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();

$pdo = new PDO(
    'mysql:host=' . $_ENV['DB_HOST'] . ';dbname=' . $_ENV['DB_DATABASE'],
    $_ENV['DB_USERNAME'],
    $_ENV['DB_PASSWORD']
);

echo "=== Fixing Subscription #35 Dates ===\n\n";

// Get subscription
$stmt = $pdo->prepare('SELECT * FROM subscriptions WHERE id = 35');
$stmt->execute();
$sub = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$sub) {
    die("Subscription #35 not found\n");
}

echo "Current data:\n";
echo "  Period Start: {$sub['current_period_start']}\n";
echo "  Period End: {$sub['current_period_end']}\n";
echo "  Expires At: {$sub['expires_at']}\n\n";

// Fix: Set period end to 1 month after start
$periodStart = new DateTime($sub['activated_at']);
$periodEnd = clone $periodStart;
$periodEnd->modify('+1 month');

$updateStmt = $pdo->prepare('
    UPDATE subscriptions 
    SET current_period_start = ?,
        current_period_end = ?,
        expires_at = ?
    WHERE id = 35
');

$updateStmt->execute([
    $periodStart->format('Y-m-d H:i:s'),
    $periodEnd->format('Y-m-d H:i:s'),
    $periodEnd->format('Y-m-d H:i:s'),
]);

echo "Fixed dates:\n";
echo "  Period Start: " . $periodStart->format('Y-m-d H:i:s') . "\n";
echo "  Period End: " . $periodEnd->format('Y-m-d H:i:s') . "\n";
echo "  Expires At: " . $periodEnd->format('Y-m-d H:i:s') . "\n";
echo "\n✅ Subscription #35 dates fixed!\n";
