<?php

namespace Openguru\OpenCartFramework\QueryBuilder;

use Openguru\OpenCartFramework\Container\Container;
use Openguru\OpenCartFramework\Container\ServiceProvider;
use Openguru\OpenCartFramework\QueryBuilder\Connections\ConnectionInterface;
use Openguru\OpenCartFramework\QueryBuilder\Connections\MySqlConnection;
use Openguru\OpenCartFramework\QueryBuilder\Grammars\MySqlGrammar;
use PDO;

class QueryBuilderServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->container->bind(ConnectionInterface::class, function (Container $container) {
            $host = $container->getConfigValue('database.host');
            $username = $container->getConfigValue('database.username');
            $password = $container->getConfigValue('database.password');
            $port = (int) $container->getConfigValue('database.port');
            $dbName = $container->getConfigValue('database.database');

            $dsn = "mysql:host=$host;port=$port;dbname=$dbName";

            return new MySqlConnection(
                new PDO($dsn, $username, $password)
            );
        });

        $this->container->bind(Builder::class, function (Container $container) {
            return new Builder(
                $container->get(ConnectionInterface::class),
                $container->get(MySqlGrammar::class)
            );
        });
    }
}
