<?php
// public/migrate_message_limits.php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require_once __DIR__ . '/../src/Utils/Env.php';
require_once __DIR__ . '/../src/Config/Database.php';

use App\Utils\Env;
use App\Config\Database;

try {
    $envPath = __DIR__ . '/../../.env';
    if (!file_exists($envPath)) {
        die("Error: .env file not found at " . realpath($envPath) . "\n");
    }
    Env::load($envPath);
    echo "Loaded .env from: " . realpath($envPath) . "\n";
    $db = Database::getInstance()->getConnection();
    
    echo "Adding message limit columns to users table...\n";
    
    $queries = [
        "ALTER TABLE users ADD COLUMN daily_message_count INT DEFAULT 0",
        "ALTER TABLE users ADD COLUMN last_message_date DATE DEFAULT NULL",
        "ALTER TABLE users ADD COLUMN message_ad_credits INT DEFAULT 0"
    ];

    foreach ($queries as $sql) {
        try {
            $db->exec($sql);
            echo "SUCCESS: $sql\n";
        } catch (PDOException $e) {
            echo "INFO: Column might exist - " . $e->getMessage() . "\n";
        }
    }

    echo "Migration complete.\n";

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