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

Predefined deployment configurations for different environments.
Each preset defines which services to run, infrastructure backend, and policies.

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

import .schemas as s
import .defaults as d

# Assembled presets list based on user selection
presets = [
{%- if "local" in deployment.presets_to_generate %}
    d.local_preset,
{%- endif %}
{%- if "dev" in deployment.presets_to_generate %}
    d.dev_preset,
{%- endif %}
{%- if "staging" in deployment.presets_to_generate %}
    d.staging_preset,
{%- endif %}
{%- if "production" in deployment.presets_to_generate %}
    d.production_preset,
{%- endif %}
]

# Validation: ensure at least one preset is defined with error handling
try:
    {literal}
    # At least one deployment preset must be defined
    assert len(presets) > 0, \
        "Configuration error: At least one deployment preset must be defined"

    # Validation: each preset's services must be valid
    for preset in presets:
        assert len(preset.services_enabled) > 0 or preset.name == "local", \
            "Preset '${preset.name}' must have at least one service enabled (except 'local' which is CLI-only)"

        for service_name in preset.services_enabled:
            available_services = [svc.name for svc in d.services]
            assert any svc in d.services if svc.name == service_name, \
                "Preset '${preset.name}' references unknown service '${service_name}'. Available: ${available_services}"

        # Validate manager selection
        assert preset.manager in ["manual", "provctl", "provisioning"], \
            "Preset '${preset.name}' has invalid manager '${preset.manager}'. Must be: manual, provctl, or provisioning"

        # Validate database type
        available_dbs = ["sqlite", "postgres", "mysql", "surrealdb"]
        assert preset.database_type in available_dbs, \
            "Preset '${preset.name}' requires database '${preset.database_type}' which is not valid"
    {/literal}
catch err:
    # Provide detailed error information and recommendations
    print("❌ Preset validation error: ${err}")
    print("💡 Please check presets.k for configuration issues")

# Default preset for development
default_preset = "{{ deployment.default_preset | default(value='dev') }}"

# Optional: Add custom presets or override existing ones
# Example custom preset:
# custom_preset = s.Preset {
#     name = "custom"
#     description = "Custom deployment configuration"
#     services_enabled = ["cli", "api"]
#     manager = "provctl"
#     database_type = "sqlite"
#     cache_enabled = false
# }
#
# presets = presets + [custom_preset]
