<?php
// Generated by setup_mariadb_project.sh — edit as needed.
define('DB_HOST', '127.0.0.1');
define('DB_NAME', 'app_38458');
define('DB_USER', 'app_38458');
define('DB_PASS', 'c217529c-a428-4a97-8f31-773c420377a7');

// Supabase Configuration - Provide your project URL and Service Role Key
define('SUPABASE_URL', getenv('SUPABASE_URL') ?: 'https://siqeqnizegizxemrfgkf.supabase.co');
define('SUPABASE_SERVICE_ROLE_KEY', getenv('SUPABASE_SERVICE_ROLE_KEY') ?: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InNpcWVxbml6ZWdpenhlbXJmZ2tmIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImlhdCI6MTc3MDEyMjgzMywiZXhwIjoyMDg1Njk4ODMzfQ.8K66Y3hSfSq5mRDeU8YT6pH9tqPVngxA9dEwCgQUCl0');
define('SUPABASE_DB_PASS', getenv('SUPABASE_DB_PASS') ?: 'gA82h8K80T5QUAwi'); // Set your DB password here for PostgreSQL migration

function db() {
  static $pdo;
  if (!$pdo) {
    $useSupabase = defined('SUPABASE_DB_PASS') && !empty(SUPABASE_DB_PASS);
    
    if ($useSupabase && extension_loaded('pdo_pgsql')) {
        // Use Supabase PostgreSQL
        $host = 'aws-1-ap-southeast-1.pooler.supabase.com';
        $port = '6543';
        $dbname = 'postgres';
        $user = 'postgres.siqeqnizegizxemrfgkf';
        $pass = SUPABASE_DB_PASS;
        try {
            $pdo = new PDO("pgsql:host=$host;port=$port;dbname=$dbname", $user, $pass, [
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
                PDO::ATTR_TIMEOUT => 5, // Short timeout for faster fallback
            ]);
            return $pdo;
        } catch (PDOException $pgException) {
            // Keep the error to report it if the fallback also fails
        }
    }

    // Fallback to local MariaDB/MySQL
    try {
        $pdo = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset=utf8mb4', DB_USER, DB_PASS, [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        ]);
    } catch (PDOException $mysqlException) {
        if (!extension_loaded('pdo_pgsql')) {
            die("Connection failed: The 'pdo_pgsql' extension is not enabled in your PHP configuration, and the local MySQL connection also failed.<br>MySQL Error: " . $mysqlException->getMessage());
        }
        
        $errorMsg = "<h3>Database Connection Failed</h3>";
        if (isset($pgException)) {
            $errorMsg .= "<strong>PostgreSQL (Supabase) Error:</strong> " . $pgException->getMessage() . "<br><br>";
        }
        $errorMsg .= "<strong>Local MySQL Error:</strong> " . $mysqlException->getMessage() . "<br><br>";
        $errorMsg .= "<strong>Possible Solutions:</strong><br>";
        $errorMsg .= "1. <strong>Supabase:</strong> Ensure your firewall/ISP allows outgoing connections on <strong>port 6543</strong>. Try disabling your antivirus temporarily.<br>";
        $errorMsg .= "2. <strong>Local MySQL:</strong> If you want to use local MySQL, update <code>DB_USER</code> and <code>DB_PASS</code> in <code>db/config.php</code> to match your XAMPP settings (usually 'root' and empty password).<br>";
        $errorMsg .= "3. <strong>Verify extension:</strong> Make sure <code>extension=pdo_pgsql</code> is uncommented in <code>php.ini</code> AND you have restarted Apache.<br>";
        
        die($errorMsg);
    }
  }
  return $pdo;
}
