name: ftb-local-server

services:
  postgres:
    image: postgres:16-alpine
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-postgres}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-postgres}
      POSTGRES_DB: ${POSTGRES_DB:-ftb_pm}
    volumes:
      - local_postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -U "$${POSTGRES_USER}" -d "$${POSTGRES_DB}"']
      interval: 10s
      timeout: 5s
      retries: 10

  redis:
    image: redis:7-alpine
    restart: unless-stopped
    command: ['redis-server', '--appendonly', 'yes']
    volumes:
      - local_redis_data:/data
    healthcheck:
      test: ['CMD', 'redis-cli', 'ping']
      interval: 10s
      timeout: 5s
      retries: 10

  server:
    build:
      context: .
      dockerfile: Dockerfile.server
    restart: unless-stopped
    environment:
      NODE_ENV: production
      DATABASE_URL: ${DATABASE_URL:-postgresql://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-postgres}@postgres:5432/${POSTGRES_DB:-ftb_pm}}
      REDIS_URL: ${REDIS_URL:-redis://redis:6379}
      ANTHROPIC_API_KEY: ${ANTHROPIC_API_KEY:-}
      SMTP_HOST: ${SMTP_HOST:-}
      SMTP_PORT: ${SMTP_PORT:-}
      SMTP_USER: ${SMTP_USER:-}
      SMTP_PASS: ${SMTP_PASS:-}
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    volumes:
      - local_server_data:/app/apps/server/data
    expose:
      - '3001'
    healthcheck:
      test:
        [
          'CMD-SHELL',
          'node -e "fetch(''http://127.0.0.1:3001/api/v1/config/ai'').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))"',
        ]
      interval: 15s
      timeout: 5s
      retries: 20
      start_period: 20s

  web:
    build:
      context: .
      dockerfile: Dockerfile.web
      args:
        NEXT_PUBLIC_API_URL: ${NEXT_PUBLIC_API_URL:-/api/v1}
    restart: unless-stopped
    environment:
      NODE_ENV: production
      NEXT_PUBLIC_API_URL: ${NEXT_PUBLIC_API_URL:-/api/v1}
      NEXTAUTH_URL: ${NEXTAUTH_URL:-http://localhost:8080}
      NEXTAUTH_SECRET: ${NEXTAUTH_SECRET:-local-server-change-me}
    depends_on:
      server:
        condition: service_healthy
    expose:
      - '3000'
    healthcheck:
      test:
        [
          'CMD-SHELL',
          'node -e "fetch(''http://127.0.0.1:3000'').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))"',
        ]
      interval: 15s
      timeout: 5s
      retries: 20
      start_period: 20s

  nginx:
    image: nginx:1.27-alpine
    restart: unless-stopped
    ports:
      - '${LOCAL_HTTP_PORT:-8080}:80'
    environment:
      SERVER_NAME: ${LOCAL_ACCESS_HOST:-localhost}
      CLIENT_MAX_BODY_SIZE: ${CLIENT_MAX_BODY_SIZE:-20m}
    volumes:
      - ./deploy/nginx/default.conf.template:/etc/nginx/templates/default.conf.template:ro
    depends_on:
      web:
        condition: service_healthy
      server:
        condition: service_healthy

volumes:
  local_postgres_data:
  local_redis_data:
  local_server_data:
