"""
PostgreSQL Database Base Configuration
Core utilities and configuration classes for PostgreSQL integration
Generated by RapidKit - {{ module_class_name }}
"""

from dataclasses import dataclass, field
from typing import Any, Dict, Optional


@dataclass
class {{ module_class_name }}Config:
    """
    PostgreSQL database configuration.

    Attributes:
        database_url: PostgreSQL connection string
        test_database_url: Test database connection string (optional)
        pool_size: Number of permanent connections in pool
        max_overflow: Maximum overflow connections beyond pool_size
        pool_recycle: Seconds before recycling connections
        pool_timeout: Seconds to wait for connection from pool
        echo: Whether to log all SQL statements
        expire_on_commit: Whether to expire objects after commit
        enabled: Whether database is enabled
        metadata: Additional metadata
    """

    # Connection Settings
    database_url: str = "postgresql://postgres:postgres@localhost:5432/myapp"
    test_database_url: Optional[str] = None

    # Pool Settings
    pool_size: int = 10
    max_overflow: int = 20
    pool_recycle: int = 3600  # 1 hour
    pool_timeout: int = 30

    # SQLAlchemy Settings
    echo: bool = False
    expire_on_commit: bool = False

    # Module Settings
    enabled: bool = True
    metadata: Dict[str, Any] = field(default_factory=dict)


def build_default_config() -> {{ module_class_name }}Config:
    """
    Return default configuration payload for PostgreSQL.

    Returns:
        {{ module_class_name }}Config: Default database configuration
    """
    return {{ module_class_name }}Config()


def get_engine_config(config: {{ module_class_name }}Config) -> Dict[str, Any]:
    """
    Extract SQLAlchemy engine configuration from config object.

    Args:
        config: PostgreSQL configuration

    Returns:
        Dict containing engine configuration parameters
    """
    return {
        "echo": config.echo,
        "pool_size": config.pool_size,
        "max_overflow": config.max_overflow,
        "pool_recycle": config.pool_recycle,
        "pool_timeout": config.pool_timeout,
    }


def validate_config(config: {{ module_class_name }}Config) -> bool:
    """
    Validate PostgreSQL configuration.

    Args:
        config: Configuration to validate

    Returns:
        True if configuration is valid

    Raises:
        ValueError: If configuration is invalid
    """
    if not config.database_url:
        raise ValueError("database_url is required")

    if not config.database_url.startswith("postgresql://"):
        raise ValueError("database_url must start with 'postgresql://'")

    if config.pool_size < 1:
        raise ValueError("pool_size must be at least 1")

    if config.max_overflow < 0:
        raise ValueError("max_overflow must be non-negative")

    if config.pool_recycle < 1:
        raise ValueError("pool_recycle must be at least 1 second")

    if config.pool_timeout < 1:
        raise ValueError("pool_timeout must be at least 1 second")

    return True
