"""
Main deployment configuration for {{ deployment.project.name }}

Root module combining all deployment elements: services, databases, cache, and presets.
This is the primary configuration consumed by provisioning, provctl, and the installer.

Architecture:
  - schemas.k: Type definitions and validation rules
  - defaults.k: Default service/database/cache/preset instances
  - services.k: Active services list (filtered by user selection)
  - presets.k: Deployment presets (local, dev, staging, production)
  - deployment.k: Root configuration (this file)

Extends: provisioning.extensions.taskservs pattern
References: All other modules
Consumed by: provisioning, provctl, installer, dashboard
"""

import .schemas as s
import .defaults as d
import .services as svc
import .presets as p

# Root deployment configuration
deployment = s.Deployment {
    project = d.project
    services = svc.services
    databases = svc.databases
    cache = svc.cache
    presets = p.presets
    default_database = svc.default_database
    default_cache = {% if deployment.cache_enabled %}"redis"{% else %}None{% endif %}
}

# Module exports for consumption by other tools
# provisioning uses this to understand service topology
# provctl uses this to manage service lifecycle
# installer uses presets to offer deployment options

# Export services by type for easy reference
{literal}cli_services = [s for s in deployment.services if s.service_type == "cli"]
tui_services = [s for s in deployment.services if s.service_type == "tui"]
api_services = [s for s in deployment.services if s.service_type == "server"]
web_services = [s for s in deployment.services if s.service_type == "web"]

# Export presets by complexity
manual_presets = [p for p in deployment.presets if p.manager == "manual"]
orchestrated_presets = [p for p in deployment.presets if p.manager in ["provctl", "provisioning"]]{/literal}

# Validation checks
{literal}assert len(deployment.services) > 0, "Deployment must include at least one service"
assert len(deployment.presets) > 0, "Deployment must include at least one preset"

# Service requirement checks
for preset in deployment.presets:
    for service_name in preset.services_enabled:
        required_service = any svc in deployment.services if svc.name == service_name
        assert required_service, "Preset '${preset.name}' requires service '${service_name}' which is not enabled"

# Dependency resolution checks
for service in deployment.services if service.enabled:
    for dependency in service.requires:
        dependent_service = any svc in deployment.services if svc.name == dependency and svc.enabled
        assert dependent_service, \
            "Service '${service.name}' requires '${dependency}' which is not available or enabled"

# Database availability checks
for service in deployment.services if service.database_required:
    for required_db_type in service.database_types:
        available_db = any db in deployment.databases if db.type == required_db_type
        assert available_db, \
            "Service '${service.name}' requires database type '${required_db_type}' which is not configured"{/literal}

# Metadata for tooling integration
metadata = {
    "generated_at": "{{ generated_at | default(value="2025-11-20") }}"
    "generated_by": "provctl gen deployment"
    "project_name": deployment.project.name
    "project_version": deployment.project.version
    "service_count": len(deployment.services)
    "database_types": [db.type for db in deployment.databases]
    "cache_enabled": deployment.default_cache is not None
    "preset_count": len(deployment.presets)
    "high_availability": any preset in deployment.presets if preset.high_availability
}
