<?php
// ============================================================
// Elegant Work — Database Configuration
// ============================================================

define('DB_HOST', 'ewg.dedicated.co.za');
define('DB_PORT', '3306');
define('DB_NAME', 'elegaysv_elegantwork');
define('DB_USER', 'elegaysv_Code2');        // change this
define('DB_PASS', 'EWG2Cod!@#');            // change this
define('DB_CHARSET', 'utf8mb4');

function getDB(): PDO {
    static $pdo = null;
    if ($pdo === null) {
        $dsn = sprintf(
            'mysql:host=%s;port=%s;dbname=%s;charset=%s',
            DB_HOST, DB_PORT, DB_NAME, DB_CHARSET
        );
        $options = [
            PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
            PDO::ATTR_EMULATE_PREPARES   => false,
        ];
        try {
            $pdo = new PDO($dsn, DB_USER, DB_PASS, $options);
        } catch (PDOException $e) {
            http_response_code(500);
            die(json_encode([
                'success' => false,
                'message' => 'Database connection failed.',
                'errors'  => [$e->getMessage()]
            ]));
        }
    }
    return $pdo;
}
