<?php
/**
 * license-php/config/config.php
 * Configuration loader for PHP License Server and Client Database.
 * Environment-driven with secure fallbacks.
 * Never stores real secrets in repository.
 */

// Allow override from environment variables or custom config file
$envFile = dirname(__DIR__) . '/.env';
if (file_exists($envFile)) {
    $lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    foreach ($lines as $line) {
        $line = trim($line);
        if ($line === '' || strpos($line, '#') === 0) continue;
        if (strpos($line, '=') !== false) {
            list($key, $val) = explode('=', $line, 2);
            $key = trim($key);
            $val = trim(trim($val), '"\'');
            if (!getenv($key)) {
                putenv("$key=$val");
                $_ENV[$key] = $val;
            }
        }
    }
}

return [
    'environment'  => getenv('APP_ENV') ?: 'development',
    'product_name' => getenv('LICENSE_PRODUCT') ?: 'presensi-smansa-pro',
    'admin_api_key' => getenv('ADMIN_API_KEY') ?: 'adm_sec_smansa_master_2026_superkey',

    // =========================================================================
    // DATABASE PRESENSI CLIENT (Satu-satunya database MySQL yang perlu diisi)
    // =========================================================================
    'client_db' => (function() {
        $dbConfigFile = __DIR__ . '/db_config.php';
        if (file_exists($dbConfigFile)) {
            $customDb = require $dbConfigFile;
            if (is_array($customDb) && !empty($customDb['name'])) {
                return $customDb;
            }
        }
        return [
            'driver'   => 'mysql',
            'host'     => getenv('DB_HOST') ?: (getenv('CLIENT_DB_HOST') ?: '127.0.0.1'),
            'port'     => getenv('DB_PORT') ?: (getenv('CLIENT_DB_PORT') ?: '3306'),
            'name'     => getenv('DB_NAME') ?: (getenv('CLIENT_DB_NAME') ?: 'presensi_client_db'),
            'user'     => getenv('DB_USER') ?: (getenv('CLIENT_DB_USER') ?: 'root'),
            'password' => getenv('DB_PASSWORD') !== false ? getenv('DB_PASSWORD') : (getenv('CLIENT_DB_PASSWORD') !== false ? getenv('CLIENT_DB_PASSWORD') : ''),
            'charset'  => 'utf8mb4',
        ];
    })(),

    // Penyimpanan Lisensi Lokal (Otomatis SQLite internal, tidak memerlukan database MySQL)
    'db' => [
        'driver'      => 'sqlite',
        'sqlite_path' => dirname(__DIR__) . '/data/license.sqlite',
    ],

    // Key file paths (Strictly outside public_html in production)
    'keys' => [
        'private' => (function() {
            $candidates = [
                getenv('LICENSE_PRIVATE_KEY_PATH'),
                dirname(__DIR__, 2) . '/keys/license_private.key',
                dirname(__DIR__, 3) . '/keys/license_private.key',
                dirname(__DIR__, 4) . '/keys/license_private.key',
                dirname(__DIR__, 5) . '/keys/license_private.key',
                (isset($_SERVER['DOCUMENT_ROOT']) ? dirname($_SERVER['DOCUMENT_ROOT']) . '/keys/license_private.key' : null),
                dirname(__DIR__, 3) . '/private/license-private.pem',
                dirname(__DIR__, 2) . '/private/license-private.pem'
            ];
            foreach ($candidates as $c) {
                if ($c && file_exists($c)) return $c;
            }
            return getenv('LICENSE_PRIVATE_KEY_PATH') ?: (dirname(__DIR__, 5) . '/keys/license_private.key');
        })(),
        'public' => (function() {
            $candidates = [
                getenv('LICENSE_PUBLIC_KEY_PATH'),
                dirname(__DIR__, 2) . '/keys/license_public.key',
                dirname(__DIR__, 3) . '/keys/license_public.key',
                dirname(__DIR__, 4) . '/keys/license_public.key',
                dirname(__DIR__, 5) . '/keys/license_public.key',
                (isset($_SERVER['DOCUMENT_ROOT']) ? $_SERVER['DOCUMENT_ROOT'] . '/keys/license_public.key' : null)
            ];
            foreach ($candidates as $c) {
                if ($c && file_exists($c)) return $c;
            }
            return getenv('LICENSE_PUBLIC_KEY_PATH') ?: (dirname(__DIR__, 2) . '/keys/license_public.key');
        })(),
    ],

    'rate_limit' => [
        'activate_max' => 10,
        'activate_window' => 300, // 5 min
        'verify_max' => 60,
        'verify_window' => 60,   // 1 min
    ]
];
