version: '3.8'

services:
  db:
    image: postgres:16-alpine
    container_name: agenda_db
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${DB_USER:-agenda}
      POSTGRES_PASSWORD: ${DB_PASSWORD:?DB_PASSWORD is required}
      POSTGRES_DB: ${DB_NAME:-agenda_tasks}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    networks:
      - backend_network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${DB_USER:-agenda} -d ${DB_NAME:-agenda_tasks}"]
      interval: 10s
      timeout: 5s
      retries: 5

  backend:
    build:
      context: ../..
      dockerfile: deployment/docker/Dockerfile.backend
    container_name: agenda_backend
    restart: unless-stopped
    environment:
      DATABASE_URL: postgresql://${DB_USER:-agenda}:${DB_PASSWORD}@db:5432/${DB_NAME:-agenda_tasks}
      SECRET_KEY: ${SECRET_KEY:?SECRET_KEY is required}
      DEBUG: ${DEBUG:-false}
    depends_on:
      db:
        condition: service_healthy
    networks:
      - backend_network
    expose:
      - "8000"

  nginx:
    image: nginx:alpine
    container_name: agenda_nginx
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ../nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - ../nginx/conf.d:/etc/nginx/conf.d:ro
      - certbot_data:/etc/letsencrypt:ro
      - certbot_www:/var/www/certbot:ro
    depends_on:
      - backend
    networks:
      - backend_network

  certbot:
    image: certbot/certbot
    container_name: agenda_certbot
    volumes:
      - certbot_data:/etc/letsencrypt
      - certbot_www:/var/www/certbot
    entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"

volumes:
  postgres_data:
  certbot_data:
  certbot_www:

networks:
  backend_network:
    driver: bridge
