services:
  caddy:
    build:
      context: .
      dockerfile: Dockerfile.frontend
    container_name: pottery-caddy
    depends_on:
      backend:
        condition: service_healthy
    environment:
      SITE_HOST: ${SITE_HOST}
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - caddy_data:/data
      - caddy_config:/config
    restart: unless-stopped

  backend:
    build:
      context: ./server
      dockerfile: Dockerfile
    container_name: pottery-backend
    depends_on:
      db:
        condition: service_healthy
    environment:
      PORT: 5000
      DB_USER: ${POSTGRES_USER}
      DB_HOST: db
      DB_NAME: ${POSTGRES_DB}
      DB_PASSWORD: ${POSTGRES_PASSWORD}
      DB_PORT: 5432
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "wget", "-qO-", "http://127.0.0.1:5000/health"]
      interval: 10s
      timeout: 5s
      retries: 10
      start_period: 15s

  db:
    image: postgres:16-alpine
    container_name: pottery-db
    environment:
      POSTGRES_DB: ${POSTGRES_DB}
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./server/init.sql:/docker-entrypoint-initdb.d/00-init.sql:ro
    restart: unless-stopped
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
      interval: 10s
      timeout: 5s
      retries: 10

volumes:
  postgres_data:
  caddy_data:
  caddy_config:
