<?php

declare(strict_types=1);

$appEnv = getenv('APP_ENV') ?: 'production';

if ($appEnv === 'development' && file_exists(__DIR__ . '/../.env')) {
    require_once __DIR__ . '/../vendor/autoload.php';
    $dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/..');
    $dotenv->safeLoad();
}

$driver = getenv('DB_DRIVER') ?: 'sqlite';

$envConfig = $driver === 'mysql'
    ? [
        'adapter' => 'mysql',
        'host' => getenv('DB_MYSQL_HOST') ?: 'mysql',
        'port' => (int) (getenv('DB_MYSQL_PORT') ?: 3306),
        'name' => getenv('DB_MYSQL_DATABASE') ?: '',
        'user' => getenv('DB_MYSQL_USERNAME') ?: '',
        'pass' => getenv('DB_MYSQL_PASSWORD') ?: '',
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
    ]
    : [
        'adapter' => 'sqlite',
        // Pass the full path through verbatim (incl. extension or `:memory:`).
        // Phinx appends `suffix` to `name` when computing the DSN; an empty
        // suffix avoids the trim-the-extension dance that bit M01.
        'name' => (string) (getenv('DB_SQLITE_PATH') ?: '/data/irdb.sqlite'),
        'suffix' => '',
    ];

return [
    'paths' => [
        'migrations' => __DIR__ . '/../db/migrations',
        'seeds' => __DIR__ . '/../db/seeds',
    ],
    'environments' => [
        'default_migration_table' => 'phinxlog',
        'default_environment' => 'app',
        'app' => $envConfig,
    ],
    'version_order' => 'creation',
];
