<?php
    require_once __DIR__ . '/../vendor/autoload.php';

    $dotenv = new Dotenv\Dotenv(__DIR__ . '/../');
    $dotenv->load();

    require_once __DIR__ . '/../app/configs/application.php';    
    
    use Phalcon\Loader;
    use Phalcon\Di\FactoryDefault\Cli as CliDI;
    use Phalcon\Cli\Console as ConsoleApp;
    use Phalcon\Db\Adapter\Pdo\Postgresql as PdoPg;
    use Phalcon\Logger\Adapter\File as FileAdapter;
    use Phalcon\Db\Adapter\Pdo\Mysql as PdoMysql;

    // Load Application Files
    $loader = new Loader();
    $loader->registerDirs(
        [
            __DIR__ . '/../app/models/',
            __DIR__ . '/../app/middlewares/',
            __DIR__ . '/../app/controllers/',
            __DIR__ . '/../app/middlewares/',
            __DIR__ . '/../app/libraries/',
            __DIR__ . '/../app/exceptions/',
            __DIR__ . '/../app/responses/',
            __DIR__ . '/../app/tools/',
            __DIR__ . '/../cli/tasks/',
            __DIR__ . '/../app/traits/'
        ]
    );
    $loader->registerNamespaces(
        [
           'Jaga\Traits' => __DIR__ . '/../app/traits',
           'Jaga\Models' => __DIR__ . '/../app/models',
           //'Jaga\Controllers' => __DIR__ . '/../app/controllers',
        ]
    );
    $loader->register();

    $di = new CliDI();

    $di->set('config', function() use ($applicationConfig) {
        return $applicationConfig;
    });

    $di->set('db',
        function() use ($applicationConfig) {
            $connection = new PdoPg($applicationConfig['database']);

            return $connection;
        }
    );

    $di->set(
        'databaseElhkpnMart',
        function () use ($applicationConfig) {
            $connection = new PdoMysql($applicationConfig['databaseElhkpnMart']);

            return $connection;
        }
    );

    $di->set(
        'databaseNewStranas2024',
        function () use ($applicationConfig) {
            $connection = new PdoPg($applicationConfig['databaseNewStranas2024']);

            return $connection;
        }
    );
    
    $di->set(
        'security',
        function () {
            $security = new Security();
    
            // Set the password hashing factor to 12 rounds
            $security->setWorkFactor(12);
    
            return $security;
        },
        true
    );
    
    $di->set("transaction", function(){
        $txManager = new TxManager();
        $transaction = $txManager->get();
        return $transaction;
    });

    $di->set('logger',
        function() use ($applicationConfig) {
            $date = date('Y-m-d');
            $filename = $applicationConfig['log']['prefix'] . '-' . $date . '.log';
            $basicLogger = new FileAdapter("." . DIRECTORY_SEPARATOR . "logs" . DIRECTORY_SEPARATOR . $filename);

            return new BaseKvLogger($basicLogger, "");
        }
    );

    // Create a console application
    $console = new ConsoleApp();

    $console->setDI($di);

    /**
     * Process the console arguments
     */
    $arguments = [];

    foreach ($argv as $k => $arg) {
        if ($k === 1) {
            $arguments['task'] = $arg;
        } elseif ($k === 2) {
            $arguments['action'] = $arg;
        } elseif ($k >= 3) {
            $arguments['params'][] = $arg;
        }
    }

    try {
        // Handle incoming arguments
        $console->handle($arguments);
    } catch (\Phalcon\Exception $e) {
        // Do Phalcon related stuff here
        // ..
        fwrite(STDERR, $e->getMessage() . PHP_EOL);
        exit(1);
    } catch (\Throwable $throwable) {
        fwrite(STDERR, $throwable->getMessage() . PHP_EOL);
        exit(1);
    }
