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

use boru\ai\Config\Config;
use boru\ai\Persistence\Db\DbConfig;
use PHPUnit\Framework\TestCase;

class DbConfigTest extends TestCase
{
    public function testFromConfigReadsPersistenceAndDbSections()
    {
        $config = new Config();
        $config->persistence(array(
            'enabled' => true,
            'driver' => 'db',
            'auto_schema' => true,
            'tables' => array(
                'conversations' => 'custom_conversations',
                'turns' => 'custom_turns',
            ),
        ));
        $config->db(array(
            'dbhost' => 'db.example.test',
            'dbport' => 3307,
            'dbname' => 'ai_test',
            'dbuser' => 'tester',
            'dbpass' => 'secret',
            'dbtype' => 'mysql',
        ));

        $dbConfig = DbConfig::fromConfig($config);

        $this->assertTrue($dbConfig->enabled());
        $this->assertSame('db', $dbConfig->driver());
        $this->assertTrue($dbConfig->autoSchema());
        $this->assertSame('db.example.test', $dbConfig->get('dbhost'));
        $this->assertSame(3307, $dbConfig->get('dbport'));
        $this->assertSame('ai_test', $dbConfig->get('dbname'));
        $this->assertSame('tester', $dbConfig->get('dbuser'));
        $this->assertSame('secret', $dbConfig->get('dbpass'));
        $this->assertSame('custom_conversations', $dbConfig->table('conversations'));
        $this->assertSame('custom_turns', $dbConfig->table('turns'));
        $this->assertSame('ai_assets', $dbConfig->table('assets'));
        $this->assertSame('ai_provider_asset_bindings', $dbConfig->table('provider_asset_bindings'));
        $this->assertSame('ai_conversation_notes', $dbConfig->table('conversation_notes'));
    }

    public function testConstructorAppliesDefaultTablesWhenNotProvided()
    {
        $dbConfig = new DbConfig(array(
            'enabled' => true,
        ));

        $this->assertTrue($dbConfig->enabled());
        $this->assertFalse($dbConfig->autoSchema());
        $this->assertSame('ai_conversations', $dbConfig->table('conversations'));
        $this->assertSame('ai_turns', $dbConfig->table('turns'));
        $this->assertSame('ai_assets', $dbConfig->table('assets'));
        $this->assertSame('ai_provider_asset_bindings', $dbConfig->table('provider_asset_bindings'));
        $this->assertSame('ai_conversation_notes', $dbConfig->table('conversation_notes'));
    }
}
