# UNT — Tilt development environment
# Usage: cd ctrl && tilt up
# Cluster: kind (name: unt)
# Entry point: http://localhost:8040

allow_k8s_contexts('kind-unt')

# Hard guard: Tilt deploys to whatever the shell's current kubectl context is,
# and allow_k8s_contexts auto-permits any local-looking (kind-*) cluster.
# Fail early instead of silently landing the workload in the wrong cluster.
if k8s_context() != 'kind-unt':
    fail("Wrong kubectl context: '%s'. Run: kubectl config use-context kind-unt" % k8s_context())

# Create namespace first to avoid race conditions (pin cluster explicitly)
local('kubectl --context kind-unt create namespace unt --dry-run=client -o yaml | kubectl --context kind-unt apply -f -')

# Apply k8s manifests via kustomize (dev overlay)
k8s_yaml(kustomize('k8s/overlays/dev'))

# --- Images ---

# FastAPI + MCP servers + agents (Python backend)
docker_build(
    'unt-api',
    context='..',
    dockerfile='Dockerfile.api',
    ignore=['.git', 'def', 'ui', '.claude', 'tests', '.venv', 'node_modules'],
    live_update=[
        sync('../mcp_servers', '/app/mcp_servers'),
        sync('../agents', '/app/agents'),
        sync('../api', '/app/api'),
    ],
)

# Vue UI — context is project root so framework link resolves
docker_build(
    'unt-ui',
    context='..',
    dockerfile='Dockerfile.ui',
    live_update=[
        sync('../ui/app/src', '/ui/app/src'),
        sync('../ui/app/index.html', '/ui/app/index.html'),
        sync('../ui/app/vite.config.ts', '/ui/app/vite.config.ts'),
        sync('../ui/framework/src', '/ui/framework/src'),
    ],
)

# --- Resources ---

k8s_resource('api')
k8s_resource('ui', resource_deps=['api'])

# Group infra resources
k8s_resource(
    objects=['unt:namespace', 'unt-config:configmap'],
    new_name='infra',
)
