version: "3.9"

services:
  db:
    image: postgres:16-alpine
    environment:
      POSTGRES_DB: ${POSTGRES_DB:-walkup}
      POSTGRES_USER: ${POSTGRES_USER:-walkup}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-walkup}
    ports:
      - "5432:5432"
    volumes:
      - postgres-data:/var/lib/postgresql/data

  backend:
    build:
      context: ./backend
    depends_on:
      - db
    env_file:
      - .env
    environment:
      DATABASE_URL: ${DATABASE_URL:-postgresql+psycopg://walkup:walkup@db:5432/walkup}
      MEDIA_ROOT: ${MEDIA_ROOT:-/app/storage}
      BACKEND_CORS_ORIGINS: ${BACKEND_CORS_ORIGINS:-https://kif.local.ascorrea.com}
      TEAMSNAP_CLIENT_ID_FILE: /run/secrets/teamsnap_client_id
      TEAMSNAP_CLIENT_SECRET_FILE: /run/secrets/teamsnap_client_secret
    ports:
      - "8000:8000"
    volumes:
      - ./backend:/app
      - backend-media:/app/storage
    secrets:
      - teamsnap_client_id
      - teamsnap_client_secret
    command: uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload --proxy-headers

  frontend:
    build:
      context: ./frontend
    depends_on:
      - backend
    env_file:
      - .env
    ports:
      - "5173:5173"
    volumes:
      - ./frontend:/app
      - frontend-node-modules:/app/node_modules
    command: sh -c "npm ci && npm run dev -- --host 0.0.0.0 --port 5173"

  proxy:
    image: caddy:2.9-alpine
    depends_on:
      - backend
      - frontend
    ports:
      - "${PROXY_HTTP_PORT:-80}:80"
      - "${PROXY_HTTPS_PORT:-443}:443"
    volumes:
      - ./ops/Caddyfile:/etc/caddy/Caddyfile:ro
      - caddy-data:/data
      - caddy-config:/config
      - ./secrets/dev-proxy-cert.pem:/certs/dev-proxy-cert.pem:ro
      - ./secrets/dev-proxy-key.pem:/certs/dev-proxy-key.pem:ro
    environment:
      APP_HOST: ${APP_HOST:-kif.local.ascorrea.com}

volumes:
  postgres-data:
  backend-media:
  frontend-node-modules:
  caddy-data:
  caddy-config:

secrets:
  teamsnap_client_id:
    file: ./secrets/teamsnap_client_id.txt
  teamsnap_client_secret:
    file: ./secrets/teamsnap_client_secret.txt
