"""SQLite configuration module"""

from dataclasses import dataclass
from pathlib import Path
from typing import Dict, Any, Optional, Literal
from ..config import DatabaseConfig

@dataclass
class SqliteConfig(DatabaseConfig):
    path: str
    password: Optional[str] = None
    uri: bool = True  # Enable URI mode to support parameters like password
    type: Literal['sqlite'] = 'sqlite'

    @property
    def absolute_path(self) -> str:
        """Return absolute path to database file"""
        return str(Path(self.path).expanduser().resolve())

    def get_connection_params(self) -> Dict[str, Any]:
        """Get sqlite3 connection parameters"""
        if not self.password:
            return {'database': self.absolute_path, 'uri': self.uri}

        # Use URI format if password is provided
        uri = f"file:{self.absolute_path}?mode=rw"
        if self.password:
            uri += f"&password={self.password}"

        return {
            'database': uri,
            'uri': True
        }

    def get_masked_connection_info(self) -> Dict[str, Any]:
        """Return connection information for logging"""
        info = {
            'database': self.absolute_path,
            'uri': self.uri
        }
        if self.password:
            info['password'] = '******'
        return info

    @classmethod
    def from_yaml(cls, yaml_path: str, db_name: str, **kwargs) -> 'SqliteConfig':
        """Create SQLite configuration from YAML

        Args:
            yaml_path: Path to YAML configuration file
            db_name: Database configuration name
        """
        configs = cls.load_yaml_config(yaml_path)

        if db_name not in configs:
            available_dbs = list(configs.keys())
            raise ValueError(f"Database configuration not found: {db_name}. Available configurations: {available_dbs}")

        db_config = configs[db_name]

        if 'type' not in db_config:
            raise ValueError("Database configuration must include 'type' field")
        if db_config['type'] != 'sqlite':
            raise ValueError(f"Configuration is not SQLite type: {db_config['type']}")
        if 'path' not in db_config:
            raise ValueError("SQLite configuration must include 'path' field")

        config = cls(
            path=db_config['path'],
            password=db_config.get('password'),
            uri=True
        )
        config.debug = cls.get_debug_mode()
        return config
