<?php
// api/config/database.php
// MUST CHANGE FOR HOSTING OR LOCAL DEVELOPMENT
require_once dirname(__DIR__, 2) . '/config/padel.php';

class Database {

    // =====================================
    // 0 = LOCAL DEVELOPMENT
    // 1 = HOSTING / PRODUCTION
    // =====================================
    public static int $hosting = 0;

    private static ?PDO $instance = null;

    private function __construct() {}

    public static function getConnection(): PDO {

        if (self::$instance === null) {
            $config = padel_db_config();

            self::$hosting = padel_is_hosting() ? 1 : 0;

            $host = $config['host'];
            $db = $config['name'];
            $user = $config['user'];
            $pass = $config['pass'];
            $charset = 'utf8mb4';

            $dsn = "mysql:host=$host;dbname=$db;charset=$charset";

            $options = [
                PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
                PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
                PDO::ATTR_EMULATE_PREPARES   => false,
            ];

            self::$instance = new PDO($dsn, $user, $pass, $options);
        }

        return self::$instance;
    }
}