# منصة جدوى — تشغيل الإنتاج على Hostinger VPS
# الاستخدام: docker compose -f docker-compose.prod.yml up -d --build
# المتغيرات من ملف .env بجوار هذا الملف (انظر .env.example وDEPLOY.md)

services:
  web:
    build:
      context: .
      dockerfile: apps/web/Dockerfile
    restart: unless-stopped
    env_file: .env
    environment:
      DATABASE_URL: postgres://jadwa:${POSTGRES_PASSWORD}@db:5432/jadwa
    depends_on:
      db:
        condition: service_healthy
    volumes:
      - exports:/app/apps/web/.exports
    networks: [jadwa]

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

  caddy:
    image: caddy:2
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    environment:
      JADWA_DOMAIN: ${JADWA_DOMAIN:-www.jadwa.biz}
    volumes:
      - ./deploy/Caddyfile:/etc/caddy/Caddyfile:ro
      - caddy_data:/data
      - caddy_config:/config
    depends_on: [web]
    networks: [jadwa]

  # نسخ احتياطي يومي — يحتفظ بآخر 14 نسخة في ./backups
  backup:
    image: postgres:16
    restart: unless-stopped
    environment:
      PGPASSWORD: ${POSTGRES_PASSWORD}
    volumes:
      - ./backups:/backups
    entrypoint: >
      bash -c 'while true; do
        pg_dump -h db -U jadwa -d jadwa -F c -f "/backups/jadwa-$(date +%Y%m%d-%H%M).dump" &&
        ls -1t /backups/jadwa-*.dump | tail -n +15 | xargs -r rm -- ;
        sleep 86400;
      done'
    depends_on:
      db:
        condition: service_healthy
    networks: [jadwa]

volumes:
  pgdata:
  exports:
  caddy_data:
  caddy_config:

networks:
  jadwa:
