services:
  database:
    container_name: glossa-database
    image: postgres:18.3-alpine3.23
    env_file:
      - .env
    environment:
      - PGDATA=/var/lib/postgresql/data
    ports:
      - "5432:5432"
    volumes:
      - glossa-db:/var/lib/postgresql/data
    restart: unless-stopped
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
      interval: 5s
      timeout: 5s
      retries: 5

  valkey:
    container_name: glossa-valkey
    image: valkey/valkey:9.1-alpine3.23
    ports:
      - "6379:6379"
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "valkey-cli", "ping"]
      interval: 5s
      timeout: 3s
      retries: 5

  api:
    container_name: glossa-api
    build:
      context: .
      dockerfile: ./apps/api/Dockerfile
      target: dev
    env_file:
      - .env
    ports:
      - "3000:3000"
    volumes:
      - ./apps/api:/app/apps/api # Hot reload API code
      - ./packages/shared:/app/packages/shared # Hot reload shared
      - /app/node_modules
    restart: unless-stopped
    healthcheck:
      test:
        ["CMD-SHELL", "wget -qO- http://localhost:3000/api/health || exit 1"]
      interval: 5s
      timeout: 3s
      retries: 5
    depends_on:
      database:
        condition: service_healthy
      valkey:
        condition: service_healthy

  web:
    container_name: glossa-web
    build:
      context: .
      dockerfile: ./apps/web/Dockerfile
      target: dev
    ports:
      - "5173:5173"
    volumes:
      - ./apps/web:/app/apps/web # Hot reload: local edits reflect immediately
      - /app/node_modules # Protect container's node_modules from being overwritten
    environment:
      - VITE_API_URL=http://localhost:3000
    restart: unless-stopped
    depends_on:
      api:
        condition: service_healthy

volumes:
  glossa-db:
