<?php

namespace Config;

use CodeIgniter\Database\Config;

class Database extends Config
{
    public string $filesPath = APPPATH . 'Database' . DIRECTORY_SEPARATOR;
    public string $defaultGroup = 'default';

    public array $default = [];
    public array $tests = [];

    public function __construct()
    {
        parent::__construct();

        if (ENVIRONMENT === 'testing') {
            $this->defaultGroup = 'tests';
        }

        $this->default = [
            'DSN'      => '',
            'hostname' => env('database.default.hostname'),
            'username' => env('database.default.username'),
            'password' => env('database.default.password'),
            'database' => env('database.default.database'),
            'DBDriver' => env('database.default.DBDriver', 'MySQLi'),
            'DBPrefix' => '',
            'pConnect' => false,
            'DBDebug'  => (ENVIRONMENT !== 'development'),
            'charset'  => 'utf8',
            'DBCollat' => 'utf8_general_ci',
            'swapPre'  => '',
            'encrypt'  => false,
            'compress' => false,
            'strictOn' => false,
            'failover' => [],
            'port'     => (int) env('database.default.port', 3306),
        ];

        $this->tests = [
            'DSN'      => '',
            'hostname' => env('TEST_DB_HOST', env('database.tests.hostname', env('database.default.hostname'))),
            'username' => env('TEST_DB_USER', env('database.tests.username', env('database.default.username'))),
            'password' => env('TEST_DB_PASSWORD', env('database.tests.password', env('database.default.password'))),
            'database' => env('TEST_DB_NAME', env('database.tests.database', 'alrahma_test')),
            'DBDriver' => env('database.tests.DBDriver', env('database.default.DBDriver', 'MySQLi')),
            'DBPrefix' => env('database.tests.DBPrefix', ''),
            'pConnect' => false,
            'DBDebug'  => true,
            'charset'  => 'utf8mb4',
            'DBCollat' => 'utf8mb4_general_ci',
            'swapPre'  => '',
            'encrypt'  => false,
            'compress' => false,
            'strictOn' => false,
            'failover' => [],
            'port'     => (int) env('TEST_DB_PORT', env('database.tests.port', env('database.default.port', 3306))),
        ];
    }
}
