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

  app:
    build: .
    restart: unless-stopped
    environment:
      APP_NAME: ${APP_NAME:-Sistema de Notas U.E.N. Alberto Smith}
      SECRET_KEY: ${SECRET_KEY:?Defina SECRET_KEY en .env}
      DATABASE_URL: postgresql+psycopg://${POSTGRES_USER:-alberto_smith}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB:-alberto_smith}
      ADMIN_USERNAME: ${ADMIN_USERNAME:-administrador}
      ADMIN_PASSWORD: ${ADMIN_PASSWORD:?Defina ADMIN_PASSWORD en .env}
      SCHOOL_YEAR: ${SCHOOL_YEAR:-2025-2026}
      COOKIE_SECURE: ${COOKIE_SECURE:-true}
      ARTICLE112_THRESHOLD: ${ARTICLE112_THRESHOLD:-0.30}
      ARTICLE112_PASSING_SCORE: ${ARTICLE112_PASSING_SCORE:-10}
      ARTICLE112_ABSENCE_COUNTS_AS_FAIL: ${ARTICLE112_ABSENCE_COUNTS_AS_FAIL:-false}
      INSTITUTION_TIMEZONE: ${INSTITUTION_TIMEZONE:-America/Caracas}
      GRADE_UPLOAD_HOURS: ${GRADE_UPLOAD_HOURS:-48}
    depends_on:
      db:
        condition: service_healthy
    expose:
      - "8000"

  caddy:
    image: caddy:2-alpine
    restart: unless-stopped
    environment:
      DOMAIN: ${DOMAIN:?Defina DOMAIN en .env}
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile:ro
      - caddy_data:/data
      - caddy_config:/config
    depends_on:
      - app

volumes:
  postgres_data:
  caddy_data:
  caddy_config:
