<?php
namespace boru\ai\Persistence\Db;

use boru\ai\Config\Config;
use boru\ai\Config\Env;

class DbConfig
{
    /** @var array */
    protected $config = array();

    public function __construct(array $config = array())
    {
        $this->config = $this->normalize($config);
    }

    public static function fromConfig(Config $config = null)
    {
        $persistence = $config ? $config->persistence() : array();
        $db = $config ? $config->db() : array();

        $tables = array();
        if (isset($persistence['tables']) && is_array($persistence['tables'])) {
            $tables = $persistence['tables'];
        }

        return new self(array(
            'enabled' => isset($persistence['enabled']) ? (bool)$persistence['enabled'] : false,
            'driver' => isset($persistence['driver']) ? $persistence['driver'] : 'db',
            'auto_schema' => isset($persistence['auto_schema']) ? (bool)$persistence['auto_schema'] : false,
            'persist_tool_calls' => isset($persistence['persist_tool_calls']) ? (bool)$persistence['persist_tool_calls'] : false,
            'use' => isset($db['use']) ? (bool)$db['use'] : true,
            'dbhost' => isset($db['dbhost']) ? $db['dbhost'] : Env::get('AI_DB_HOST', '127.0.0.1'),
            'dbport' => isset($db['dbport']) ? (int)$db['dbport'] : (int)Env::get('AI_DB_PORT', 3306),
            'dbname' => isset($db['dbname']) ? $db['dbname'] : Env::get('AI_DB_NAME', 'ai'),
            'dbuser' => isset($db['dbuser']) ? $db['dbuser'] : Env::get('AI_DB_USER', 'root'),
            'dbpass' => isset($db['dbpass']) ? $db['dbpass'] : Env::get('AI_DB_PASS', ''),
            'dbtype' => isset($db['dbtype']) ? $db['dbtype'] : Env::get('AI_DB_ADAPTER', 'mysql'),
            'tables' => $tables,
        ));
    }

    public function toArray()
    {
        return $this->config;
    }

    public function get($key, $default = null)
    {
        return isset($this->config[$key]) ? $this->config[$key] : $default;
    }

    public function enabled()
    {
        return (bool)$this->get('enabled', false);
    }

    public function driver()
    {
        return (string)$this->get('driver', 'db');
    }

    public function autoSchema()
    {
        return (bool)$this->get('auto_schema', false);
    }

    public function persistToolCalls()
    {
        return (bool)$this->get('persist_tool_calls', false);
    }

    public function table($key, $default = null)
    {
        $tables = $this->tables();
        return isset($tables[$key]) ? $tables[$key] : $default;
    }

    public function tables()
    {
        $tables = $this->get('tables', array());
        return is_array($tables) ? $tables : array();
    }

    protected function normalize(array $config)
    {
        $defaults = array(
            'enabled' => false,
            'driver' => 'db',
            'auto_schema' => false,
            'persist_tool_calls' => false,
            'use' => true,
            'dbhost' => '127.0.0.1',
            'dbport' => 3306,
            'dbname' => 'ai',
            'dbuser' => 'root',
            'dbpass' => '',
            'dbtype' => 'mysql',
            'tables' => array(
                'conversations' => 'ai_conversations',
                'turns' => 'ai_turns',
                'assets' => 'ai_assets',
                'provider_asset_bindings' => 'ai_provider_asset_bindings',
                'tool_calls' => 'ai_tool_calls',
                'embeddings' => 'ai_embeddings',
                'conversation_notes' => 'ai_conversation_notes',
            ),
        );

        foreach ($config as $key => $value) {
            if ($key === 'tables' && is_array($value)) {
                foreach ($value as $tableKey => $tableName) {
                    $defaults['tables'][$tableKey] = $tableName;
                }
                continue;
            }

            $defaults[$key] = $value;
        }

        return $defaults;
    }
}
