services:
  proxy:
    image: nginx:1.27-alpine
    depends_on:
      frontend:
        condition: service_healthy
      backend:
        condition: service_healthy
    ports:
      - "8080:80"
    volumes:
      - ./proxy/nginx.conf:/etc/nginx/conf.d/default.conf:ro
    restart: unless-stopped
    healthcheck:
      test: ["CMD-SHELL", "wget -q --spider http://127.0.0.1/ || exit 1"]
      interval: 15s
      timeout: 5s
      retries: 5
      start_period: 10s

  frontend:
    build:
      context: .
      dockerfile: frontend/Dockerfile
    volumes:
      - ./frontend/public:/usr/share/nginx/html:ro
    restart: unless-stopped
    healthcheck:
      test: ["CMD-SHELL", "wget -q --spider http://127.0.0.1/ || exit 1"]
      interval: 15s
      timeout: 5s
      retries: 5
      start_period: 10s

  backend:
    build:
      context: .
      dockerfile: backend/Dockerfile
    command: uvicorn backend.app.main:app --host 0.0.0.0 --port 8000 --reload
    env_file:
      - .env
    depends_on:
      db:
        condition: service_healthy
    volumes:
      - ./backend/app:/app/backend/app:ro
      - ./DashBoard-organization.html:/app/legacy/DashBoard-organization.html:ro
      - ./DashBoard-organization-backup.html:/app/legacy/DashBoard-organization-backup.html:ro
      - ./legacy/static:/app/legacy/static:ro
      - ./incoming-files:/app/incoming-files:ro
      - uploads_data:/data/uploads
    restart: unless-stopped
    healthcheck:
      test: ["CMD-SHELL", "python -c \"import urllib.request; urllib.request.urlopen('http://127.0.0.1:8000/api/health')\" || exit 1"]
      interval: 15s
      timeout: 5s
      retries: 8
      start_period: 20s

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

volumes:
  postgres_data:
  uploads_data:
