<?php

namespace App\Config;

use App\Drivers\DatabaseDriver;

/**
 * Auth Microservice Configuration
 * Database: emmanylcode_auth
 */
class AuthServiceConfig
{
    private static $initialized = false;
    private static string $basePath = '';

    public static function setBasePath(string $basePath): void
    {
        self::$basePath = rtrim($basePath, '/');
    }

    public static function initialize()
    {
        if (self::$initialized) {
            return;
        }

        // Initialize database connection for auth service using $_ENV
        $driver = new DatabaseDriver(
            host: $_ENV['DB_HOST'] ?? 'localhost',
            port: (int)($_ENV['DB_PORT'] ?? 3306),
            database: $_ENV['DB_NAME'] ?? 'emmanylcode_auth',
            username: $_ENV['DB_USER'] ?? 'root',
            password: $_ENV['DB_PASS'] ?? ''
        );

        DatabaseDriver::registerInstance('auth-service', $driver);
        self::$initialized = true;
    }

    public static function getDatabase(): DatabaseDriver
    {
        self::initialize();
        return DatabaseDriver::getInstance('auth-service');
    }

    public static function getConfig(): array
    {
        return [
            'app' => [
                'name' => $_ENV['APP_NAME'] ?? 'auth-service',
                'env' => $_ENV['APP_ENV'] ?? 'production',
                'debug' => ($_ENV['APP_DEBUG'] ?? 'false') === 'true',
                'url' => $_ENV['APP_URL'] ?? 'http://localhost/projects/SMS-MICROSERVICES/auth-service/public/'
            ],
            'database' => [
                'host' => $_ENV['DB_HOST'] ?? 'localhost',
                'port' => (int)($_ENV['DB_PORT'] ?? 3306),
                'name' => $_ENV['DB_NAME'] ?? 'emmanylcode_auth',
                'username' => $_ENV['DB_USER'] ?? 'root',
                'password' => $_ENV['DB_PASS'] ?? ''
            ],
                'jwt' => [
                    'secret' => $_ENV['JWT_SECRET'] ?? 'auth-service-secret-key',
                    'issuer' => $_ENV['JWT_ISSUER'] ?? 'emmanylcode-auth',
                    'expiration' => (int)($_ENV['JWT_EXPIRATION'] ?? 3600),
                    'refresh_expiration' => (int)($_ENV['JWT_REFRESH_EXPIRATION'] ?? 604800),
                    'alg' => 'RS256',
                    'private_key_path' => self::path($_ENV['JWT_PRIVATE_KEY_PATH'] ?? 'app/Keys/private.pem'),
                    'public_key_path' => self::path($_ENV['JWT_PUBLIC_KEY_PATH'] ?? 'app/Keys/public.pem'),
                    'private_key' => (is_file(self::path($_ENV['JWT_PRIVATE_KEY_PATH'] ?? 'app/Keys/private.pem')) ? file_get_contents(self::path($_ENV['JWT_PRIVATE_KEY_PATH'] ?? 'app/Keys/private.pem')) : null),
                    'public_key' => (is_file(self::path($_ENV['JWT_PUBLIC_KEY_PATH'] ?? 'app/Keys/public.pem')) ? file_get_contents(self::path($_ENV['JWT_PUBLIC_KEY_PATH'] ?? 'app/Keys/public.pem')) : null),
                ],
            'cache' => [
                'driver' => $_ENV['CACHE_DRIVER'] ?? 'file',
                'path' => self::path('storage/cache'),
                'ttl' => (int)($_ENV['CACHE_TTL'] ?? 3600),
            ],
            'security' => [
                'max_login_attempts' => (int)($_ENV['MAX_LOGIN_ATTEMPTS'] ?? 5),
                'lockout_duration' => (int)($_ENV['LOGIN_LOCKOUT_DURATION'] ?? 900),
                'password_hash_algorithm' => $_ENV['PASSWORD_HASH_ALGORITHM'] ?? 'bcrypt'
            ],
            'mail' => [
                'host' => $_ENV['MAIL_HOST'] ?? 'localhost',
                'port' => (int)($_ENV['MAIL_PORT'] ?? 1025),
                'from' => $_ENV['MAIL_FROM'] ?? 'noreply@emmanylcode.local',
            ],
            'api' => [
                'key' => $_ENV['API_KEY'] ?? 'auth-service-api-key',
                'cors_origins' => explode(',', $_ENV['CORS_ORIGINS'] ?? 'http://localhost')
            ]
        ];
    }

    private static function path(string $path): string
    {
        if (str_starts_with($path, '/')) {
            return $path;
        }

        return (self::$basePath ?: dirname(__DIR__, 2)) . '/' . ltrim($path, '/');
    }
}
