"""
Deployment schemas for {{ deployment.project.name }}

Auto-generated KCL schemas for service definitions, presets, and deployment configuration.
This module defines the structure and validation rules for all deployment elements.

Extends: provisioning.extensions.taskservs schema patterns
References: deployment.k, defaults.k, services.k, presets.k
"""

# Service configuration schema
schema Service:
    """Service definition with metadata and requirements"""

    # Basic identification
    name: str
    display_name: str
    service_type: str  # "cli", "tui", "server", "web"
    enabled: bool = True

    # Execution context
    port: int = 0  # Optional port number (0 if not used)
    background_service: bool = False

    # Resource requirements
    min_memory_mb: int = 64
    min_disk_space_mb: int = 50

    # Configuration
    config_location: str = "~/.config"

    # Database support
    database_required: bool = False
    database_types: [str] = []

    # Platform support
    platform_support: [str] = []

    # Service dependencies
    requires: [str] = []

    # Health check configuration
    health_check: HealthCheck = {}

    # Restart policy for background services
    restart_policy: str = ""  # "on-failure", "always", or empty
    restart_delay_seconds: int = 0

    check:
        port == 0 or (1 <= port < 65536), "Port must be between 1 and 65535"


schema HealthCheck:
    """Health check configuration for services"""

    type: str  # "http", "tcp", "cmd"
    interval_seconds: int = 10
    timeout_seconds: int = 5

    # Type-specific fields (conditional validation)
    endpoint: str = ""  # For HTTP checks
    method: str = ""    # For HTTP checks: GET, POST, etc
    expected_status: int = 0  # For HTTP checks
    port: int = 0  # For TCP checks
    command: str = ""  # For command checks

    check:
        interval_seconds > 0, "Interval must be positive"
        timeout_seconds > 0, "Timeout must be positive"
        timeout_seconds < interval_seconds, "Timeout should be less than interval"


schema Database:
    """Database backend configuration"""

    type: str  # "sqlite", "postgres", "mysql", "surrealdb"
    host: str = ""
    port: int = 0
    user: str = ""
    password: str = ""
    path: str = ""  # For file-based databases
    platform_support: [str] = []

    check:
        port == 0 or (1 <= port < 65536), "Port must be between 1 and 65535"


schema Cache:
    """Cache backend configuration"""

    type: str  # "redis", "memcached"
    enabled: bool = False
    host: str = "localhost"
    port: int = 6379
    platform_support: [str] = []

    check:
        1 <= port < 65536, "Port must be between 1 and 65535"


schema Preset:
    """Deployment preset configuration"""

    name: str
    description: str
    services_enabled: [str]
    manager: str  # "manual", "provctl", "provisioning"
    database_type: str
    cache_enabled: bool = False
    restart_policy: str = ""
    high_availability: bool = False

    check:
        manager in ["manual", "provctl", "provisioning"], "Manager must be 'manual', 'provctl', or 'provisioning'"


schema ProjectInfo:
    """Project metadata and configuration"""

    name: str
    version: str
    description: str = ""

    check:
        len(name) > 0, "Project name cannot be empty"
        len(version) > 0, "Project version cannot be empty"


schema Deployment:
    """Root deployment configuration"""

    project: ProjectInfo
    services: [Service]
    databases: [Database]
    cache: [Cache] = []
    presets: [Preset]
    default_database: str
    default_cache: str = ""

    check:
        len(services) > 0, "At least one service must be defined"
        len(presets) > 0, "At least one preset must be defined"
