# Docker Compose configuration for SkillMeat
#
# Three profiles cover local development through enterprise deployment:
#
#   local          -- API + Web + Docs with SQLite backend (zero-config)
#   local-auth     -- API + Web + Docs with SQLite + Clerk authentication
#   enterprise     -- API + Web + Docs + PostgreSQL with full auth
#
# Two additional demo profiles add Backstage integration:
#
#   full           -- API + Web + Docs + Backstage + demo-db (complete demo stack)
#   api-only       -- API + demo-db (headless demo, Backstage connects externally)
#   backstage-only -- Backstage + demo-db only (API runs on host)
#
# QUICK START
#   1. Copy the appropriate env example:
#        cp .env.local.example .env        # local profile
#        cp .env.local-auth.example .env   # local-auth profile
#        cp .env.enterprise.example .env   # enterprise profile
#
#   2. Start services:
#        docker compose --profile local up             # Production mode
#        docker compose --profile local-auth up
#        docker compose --profile enterprise up
#        docker compose --profile full up              # Full demo stack
#        docker compose --profile backstage-only up
#
#   3. Development with hot-reload (local profile only):
#        docker compose -f docker-compose.yml -f docker-compose.dev.yml --profile local up
#
#   3. Tear down (preserve volumes):
#        docker compose down
#
#   4. Tear down + wipe data:
#        docker compose down -v

services:
  # ===================================================================
  # SKILLMEAT-API -- FastAPI Backend
  # ===================================================================
  skillmeat-api:
    image: skillmeat-api:local
    build:
      context: .
      dockerfile: Dockerfile
    profiles:
      - local
      - local-auth
      - enterprise
      - full
      - api-only
    ports:
      - "${SKILLMEAT_API_PORT:-8080}:8080"
    env_file:
      - .env
    environment:
      - SKILLMEAT_HOST=0.0.0.0
      - SKILLMEAT_PORT=8080
    volumes:
      - skillmeat-data:/home/app/.skillmeat
      # Mount .claude directory for project metadata and deployed artifacts
      # :z flag required for SELinux relabeling on Fedora/RHEL
      - /home/miethe/dev/skillmeat/.claude:/home/app/projects/skillmeat/.claude:z
    depends_on:
      postgres:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
      interval: 15s
      timeout: 5s
      retries: 5
      start_period: 30s
    dns:
      - 8.8.8.8
      - 8.8.4.4
    restart: unless-stopped

  # ===================================================================
  # SKILLMEAT-WEB -- Next.js Frontend
  # ===================================================================
  skillmeat-web:
    image: skillmeat-web:local
    build:
      context: skillmeat/web
      dockerfile: Dockerfile
    profiles:
      - local
      - local-auth
      - enterprise
      - full
    ports:
      - "${SKILLMEAT_WEB_PORT:-3000}:3000"
    env_file:
      - .env
    environment:
      # Browser-facing: empty so client uses relative URLs proxied via Next.js rewrites.
      # Override with actual URL only if Next.js rewrite proxy is not used.
      - NEXT_PUBLIC_API_URL=${NEXT_PUBLIC_API_URL:-}
      # Server-side (SSR/rewrites): internal container hostname for Next.js rewrite proxy
      - INTERNAL_API_URL=${INTERNAL_API_URL:-http://skillmeat-api:8080}
      - NEXT_TELEMETRY_DISABLED=1
    depends_on:
      skillmeat-api:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "node", "-e", "fetch('http://localhost:3000').then(r => { if (!r.ok) process.exit(1) }).catch(() => process.exit(1))"]
      interval: 15s
      timeout: 5s
      retries: 5
      start_period: 30s
    restart: unless-stopped

  # ===================================================================
  # SKILLMEAT-DOCS -- MkDocs Material Documentation
  # ===================================================================
  skillmeat-docs:
    image: skillmeat-docs:local
    build:
      context: .
      dockerfile: Dockerfile.docs
    profiles:
      - local
      - local-auth
      - enterprise
      - full
    ports:
      - "${SKILLMEAT_DOCS_PORT:-8000}:8000"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000"]
      interval: 15s
      timeout: 5s
      retries: 3
      start_period: 10s
    restart: unless-stopped

  # ===================================================================
  # POSTGRES -- PostgreSQL Database (enterprise profile only)
  # ===================================================================
  postgres:
    image: docker.io/library/postgres:16-alpine
    profiles:
      - enterprise
    ports:
      - "${POSTGRES_PORT:-5432}:5432"
    environment:
      - POSTGRES_USER=skillmeat
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-skillmeat}
      - POSTGRES_DB=skillmeat
    volumes:
      - postgres-data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U skillmeat -d skillmeat"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 15s
    restart: unless-stopped

  # ===================================================================
  # DEMO-DB -- PostgreSQL 16 for Backstage (demo profiles)
  # ===================================================================
  demo-db:
    image: docker.io/library/postgres:16-alpine
    profiles:
      - full
      - api-only
      - backstage-only
    ports:
      - "5433:5432"
    environment:
      - POSTGRES_USER=demo
      - POSTGRES_PASSWORD=demo_password
      - POSTGRES_DB=demo_finserv
    volumes:
      - demo-db-data:/var/lib/postgresql/data
      - ./demo/init-db.sql:/docker-entrypoint-initdb.d/init-db.sql:ro
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U demo -d demo_finserv"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 15s
    restart: unless-stopped

  # ===================================================================
  # BACKSTAGE -- Backstage app (demo profiles)
  # ===================================================================
  backstage:
    image: skillmeat-backstage:local
    build:
      context: ./demo/backstage-app
      dockerfile: ../dockerfile
    profiles:
      - full
      - backstage-only
    network_mode: host
    environment:
      - POSTGRES_HOST=localhost
      - POSTGRES_PORT=5433
      - POSTGRES_USER=demo
      - POSTGRES_PASSWORD=demo_password
      - POSTGRES_DB=demo_finserv
      # Pass through only when set so Backstage can omit integrations.github[].token.
      - GITHUB_TOKEN
      - SKILLMEAT_API_URL=${SKILLMEAT_API_URL:-http://skillmeat-api:8080}
    volumes:
      # Mount tracked templates over the baked-in examples (ensures git-tracked templates are used)
      - ./demo/backstage-templates/fin-serv-project:/app/examples/skillmeat-template:ro,z
    depends_on:
      demo-db:
        condition: service_healthy
    restart: unless-stopped

# ===================================================================
# VOLUMES
# ===================================================================
volumes:
  skillmeat-data:
    driver: local
  postgres-data:
    driver: local
  demo-db-data:
    driver: local

# ===================================================================
# USAGE REFERENCE
# ===================================================================
# Use ./compose.sh for automatic Docker/Podman detection, or
# substitute `docker compose` / `podman compose` directly.
#
# Local development (SQLite, no auth):
#   ./compose.sh --profile local up
#
# Local with Clerk auth:
#   ./compose.sh --profile local-auth up
#
# Enterprise (Postgres + auth):
#   ./compose.sh --profile enterprise up
#
# Full demo stack (API + Web + Backstage + demo-db):
#   ./compose.sh --profile full up
#
# API + demo-db only (Backstage connects externally):
#   ./compose.sh --profile api-only up
#
# Backstage + demo-db only (API runs on host at port 8080):
#   SKILLMEAT_API_URL=http://host.containers.internal:8080 \
#     ./compose.sh --profile backstage-only up
#
# Build and start fresh:
#   ./compose.sh --profile local up --build
#
# View logs for a single service:
#   ./compose.sh logs -f skillmeat-api
#
# Override ports:
#   SKILLMEAT_API_PORT=9080 SKILLMEAT_WEB_PORT=4000 SKILLMEAT_DOCS_PORT=8001 \
#     ./compose.sh --profile local up
#
# Docs site available at:
#   http://localhost:${SKILLMEAT_DOCS_PORT:-8000}
#
# Stop all services (data preserved):
#   ./compose.sh down
#
# Stop and wipe all data:
#   ./compose.sh down -v
