<?php

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

$app = require_once __DIR__.'/bootstrap/app.php';
$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();

echo "=== FIX ORDER TIMESTAMPS ===\n\n";

echo "Finding orders with future timestamps...\n\n";

$futureOrders = \App\Models\Order::where('created_at', '>', now())->get();

if ($futureOrders->isEmpty()) {
    echo "No orders with future timestamps found!\n";
    exit;
}

echo "Found " . $futureOrders->count() . " orders with future timestamps:\n";
echo "==========================================================\n";

foreach ($futureOrders as $order) {
    echo sprintf(
        "ID: %d | Name: %s | Current time: %s (Should be: %s)\n",
        $order->id,
        $order->student_name,
        $order->created_at->format('Y-m-d H:i:s'),
        now()->format('Y-m-d H:i:s')
    );
}

echo "\n";
echo "Do you want to fix these timestamps to current time? (yes/no): ";
$handle = fopen("php://stdin", "r");
$line = trim(fgets($handle));

if (strtolower($line) !== 'yes') {
    echo "Aborted. No changes made.\n";
    exit;
}

echo "\nUpdating timestamps...\n";

foreach ($futureOrders as $order) {
    $oldTime = $order->created_at->format('Y-m-d H:i:s');
    $order->created_at = now();
    $order->save();
    echo "✓ Updated Order #{$order->id} ({$order->student_name}): {$oldTime} → " . $order->created_at->format('Y-m-d H:i:s') . "\n";
}

echo "\n✅ All timestamps fixed!\n";
echo "\nPlease refresh your browser to see the correct order.\n";

