"""
Default service and infrastructure definitions for {{ deployment.project.name }}

Auto-generated default instances based on user configuration.
These defaults can be overridden in services.k and presets.k modules.

Pattern: taskservs.development (from provisioning extensions)
References: schemas.k
"""

import .schemas as s

# Project information
project = s.ProjectInfo {
    name = "{{ deployment.project.name }}"
    version = "{{ deployment.project.version }}"
    description = "{{ deployment.project.description }}"
}

# Default databases - user selected: [{% for db in deployment.databases %}"{{ db }}"{% if not loop.last %}, {% endif %}{% endfor %}]
{%- if "sqlite" in deployment.databases %}
sqlite_db = s.Database {
    type = "sqlite"
    path = "/var/lib/{{ deployment.project.name }}/db.sqlite"
    platform_support = ["linux", "macos", "windows"]
}
{%- endif %}

{%- if "postgres" in deployment.databases %}
postgres_db = s.Database {
    type = "postgres"
    host = "localhost"
    port = 5432
    user = "{{ deployment.project.name }}"
    platform_support = ["linux", "macos"]
}
{%- endif %}

{%- if "mysql" in deployment.databases %}
mysql_db = s.Database {
    type = "mysql"
    host = "localhost"
    port = 3306
    user = "{{ deployment.project.name }}"
    platform_support = ["linux", "macos"]
}
{%- endif %}

{%- if "surrealdb" in deployment.databases %}
surrealdb_db = s.Database {
    type = "surrealdb"
    host = "localhost"
    port = 8000
    platform_support = ["linux", "macos"]
}
{%- endif %}

# Cache defaults
{%- if deployment.cache_enabled %}
redis_cache = s.Cache {
    type = "redis"
    host = "localhost"
    port = 6379
    platform_support = ["linux", "macos"]
    enabled = True
}

memcached_cache = s.Cache {
    type = "memcached"
    host = "localhost"
    port = 11211
    platform_support = ["linux", "macos"]
    enabled = False
}
{%- else %}
# Cache disabled - can be enabled in services.k
{%- endif %}

# Service defaults
{%- if deployment.services.cli %}
cli_service = s.Service {
    name = "{{ deployment.project.name }}-cli"
    display_name = "CLI"
    service_type = "cli"
    enabled = True
    platform_support = ["linux", "macos", "windows"]
    min_memory_mb = 64
    min_disk_space_mb = 50
    background_service = False
    config_location = "~/.config/{{ deployment.project.name }}"
    database_required = True
    database_types = [{% for db in deployment.databases %}"{{ db }}"{% if not loop.last %}, {% endif %}{% endfor %}]
}
{%- endif %}

{%- if deployment.services.tui %}
tui_service = s.Service {
    name = "{{ deployment.project.name }}-tui"
    display_name = "Terminal UI"
    service_type = "tui"
    enabled = True
    platform_support = ["linux", "macos"]
    min_memory_mb = 128
    min_disk_space_mb = 50
    background_service = False
    config_location = "~/.config/{{ deployment.project.name }}"
    database_required = True
    database_types = [{% for db in deployment.databases %}"{{ db }}"{% if not loop.last %}, {% endif %}{% endfor %}]
    requires = ["cli"]
}
{%- endif %}

{%- if deployment.services.api %}
api_service = s.Service {
    name = "{{ deployment.project.name }}-api"
    display_name = "REST API"
    service_type = "server"
    enabled = True
    port = {{ deployment.services.api.port | default(value=3000) }}
    platform_support = ["linux", "macos", "windows"]
    min_memory_mb = 256
    min_disk_space_mb = 100
    background_service = True
    config_location = "~/.config/{{ deployment.project.name }}"
    database_required = True
    database_types = [{% for db in deployment.databases %}"{{ db }}"{% if not loop.last %}, {% endif %}{% endfor %}]
    restart_policy = "on-failure"
    restart_delay_seconds = 5
    requires = ["cli"]
    health_check = s.HealthCheck {
        type = "http"
        endpoint = "http://127.0.0.1:{{ deployment.services.api.port | default(value=3000) }}/health"
        method = "GET"
        expected_status = 200
        interval_seconds = 10
        timeout_seconds = 5
    }
}
{%- endif %}

{%- if deployment.services.dashboard %}
dashboard_service = s.Service {
    name = "{{ deployment.project.name }}-dashboard"
    display_name = "Dashboard"
    service_type = "web"
    enabled = True
    port = {{ deployment.services.dashboard.port | default(value=8080) }}
    platform_support = ["linux", "macos", "windows"]
    min_memory_mb = 128
    min_disk_space_mb = 50
    background_service = True
    config_location = "~/.config/{{ deployment.project.name }}"
    database_required = False
    restart_policy = "on-failure"
    restart_delay_seconds = 5
    {%- if deployment.services.dashboard.requires_api %}
    requires = ["api"]
    {%- endif %}
    health_check = s.HealthCheck {
        type = "http"
        endpoint = "http://127.0.0.1:{{ deployment.services.dashboard.port | default(value=8080) }}"
        method = "GET"
        expected_status = 200
        interval_seconds = 10
        timeout_seconds = 5
    }
}
{%- endif %}

# Preset defaults
{%- if "local" in deployment.presets_to_generate %}
local_preset = s.Preset {
    name = "local"
    description = "CLI Only - Command-line only, manual execution"
    services_enabled = ["cli"]
    manager = "manual"
    database_type = "{{ deployment.default_database }}"
    cache_enabled = False
}
{%- endif %}

{%- if "dev" in deployment.presets_to_generate %}
dev_preset = s.Preset {
    name = "dev"
    description = "Development - Full development stack with provctl"
    services_enabled = [
        {%- for svc in ["cli", "tui", "api", "dashboard"] %}
        {%- if deployment.services[svc] %}
        "{{ svc }}"{% if not loop.last %},{% endif %}
        {%- endif %}
        {%- endfor %}
    ]
    manager = "provctl"
    database_type = "{{ deployment.default_database }}"
    {%- if deployment.cache_enabled %}
    cache_enabled = True
    {%- else %}
    cache_enabled = False
    {%- endif %}
    restart_policy = "on-failure"
}
{%- endif %}

{%- if "staging" in deployment.presets_to_generate %}
staging_preset = s.Preset {
    name = "staging"
    description = "Staging - Multi-machine deployment"
    services_enabled = ["cli", "api", "dashboard"]
    manager = "provisioning"
    database_type = "postgres"
    cache_enabled = True
    restart_policy = "on-failure"
}
{%- endif %}

{%- if "production" in deployment.presets_to_generate %}
production_preset = s.Preset {
    name = "production"
    description = "Production - HA production deployment"
    services_enabled = ["api", "dashboard"]
    manager = "provisioning"
    database_type = "postgres"
    cache_enabled = True
    restart_policy = "always"
    high_availability = True
}
{%- endif %}
