"""
Service definitions for {{ deployment.project.name }}

Custom service configurations for this deployment.
Imports default services from defaults.k and can override or extend them.

References: schemas.k, defaults.k
Used by: deployment.k, presets.k
"""

import .schemas as s
import .defaults as d

# Assembled services list
# Include all enabled services based on user selection
services = [
{%- if deployment.services.cli %}
    d.cli_service,
{%- endif %}
{%- if deployment.services.tui %}
    d.tui_service,
{%- endif %}
{%- if deployment.services.api %}
    d.api_service,
{%- endif %}
{%- if deployment.services.dashboard %}
    d.dashboard_service,
{%- endif %}
]

# Databases list - all supported types
databases = [
{%- for db in deployment.databases %}
{%- if db.type == "sqlite" %}
    d.sqlite_db,
{%- elif db.type == "postgres" %}
    d.postgres_db,
{%- elif db.type == "mysql" %}
    d.mysql_db,
{%- elif db.type == "surrealdb" %}
    d.surrealdb_db,
{%- endif %}
{%- endfor %}
]

# Cache configuration
{%- if deployment.cache_enabled %}
cache = [
    d.redis_cache,
    d.memcached_cache,
]
{%- else %}
cache = []
{%- endif %}

# Default database type for this deployment
default_database = "{{ deployment.default_database }}"

# Verification checks with comprehensive error handling
try:
    {literal}
    # At least one service must be enabled
    assert len(services) > 0, \
        "Configuration error: At least one service must be enabled in this deployment"

    # Default database must be in supported databases
    assert any db in databases if db.type == default_database, \
        "Configuration error: Default database '${default_database}' must be defined in supported databases. Available: ${[db.type for db in databases]}"

    # All database requirements must be met
    for service in services:
        if service.database_required:
            for db_type in service.database_types:
                assert any db in databases if db.type == db_type, \
                    "Configuration error: Service '${service.name}' requires database '${db_type}' which is not configured"
    {/literal}
catch err:
    # Log validation errors and continue with defaults
    print("⚠️  Service configuration validation error: ${err}")

# Optional: Add custom service modifications here
# Example to override a service:
# api_service_custom = d.api_service | {
#     port = 3001
#     min_memory_mb = 512
# }
