version: "3.8"

services:
  db:
    image: postgres:16-alpine
    container_name: pdg-postgres
    environment:
      POSTGRES_USER: pdg
      POSTGRES_PASSWORD: pdg_secret_2024
      POSTGRES_DB: prop_data_guard
    volumes:
      - pg_data:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U pdg -d prop_data_guard"]
      interval: 5s
      timeout: 5s
      retries: 5

  redis:
    image: redis:7-alpine
    container_name: pdg-redis
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 5s
      retries: 5

  minio:
    image: minio/minio:RELEASE.2024-05-10T01-41-38Z
    container_name: pdg-minio
    environment:
      MINIO_ROOT_USER: pdgminio
      MINIO_ROOT_PASSWORD: pdgminio_secret_2024
    command: server /data --console-address ":9001"
    volumes:
      - minio_data:/data
    ports:
      - "9000:9000"
      - "9001:9001"

  backend:
    build: ./backend
    container_name: pdg-backend
    environment:
      - DATABASE_URL=postgresql+psycopg2://pdg:pdg_secret_2024@db:5432/prop_data_guard
      - REDIS_URL=redis://redis:6379/0
      - MINIO_ENDPOINT=minio:9000
      - MINIO_ACCESS_KEY=pdgminio
      - MINIO_SECRET_KEY=pdgminio_secret_2024
      - SECRET_KEY=prop-data-guard-super-secret-key-change-in-production
      - DB_ENCRYPTION_KEY=${DB_ENCRYPTION_KEY:-}
      - ACCESS_TOKEN_EXPIRE_MINUTES=30
      - REFRESH_TOKEN_EXPIRE_DAYS=7
    volumes:
      - ./backend:/app
    ports:
      - "8000:8000"
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
    command: >
      sh -c "alembic upgrade head && uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload"

  frontend:
    build: ./frontend
    container_name: pdg-frontend
    volumes:
      - ./frontend:/app
      - /app/node_modules
    ports:
      - "5173:5173"
    environment:
      - VITE_API_BASE_URL=http://localhost:8000
    command: >
      sh -c "npm install && npm run dev -- --host 0.0.0.0"

  celery_worker:
    build: ./backend
    container_name: pdg-celery-worker
    environment:
      - DATABASE_URL=postgresql+psycopg2://pdg:pdg_secret_2024@db:5432/prop_data_guard
      - REDIS_URL=redis://redis:6379/0
      - SECRET_KEY=prop-data-guard-super-secret-key-change-in-production
      - DB_ENCRYPTION_KEY=${DB_ENCRYPTION_KEY:-}
    volumes:
      - ./backend:/app
    depends_on:
      - db
      - redis
    command: >
      sh -c "celery -A app.tasks.worker worker --loglevel=info --concurrency=2"

  celery_beat:
    build: ./backend
    container_name: pdg-celery-beat
    environment:
      - DATABASE_URL=postgresql+psycopg2://pdg:pdg_secret_2024@db:5432/prop_data_guard
      - REDIS_URL=redis://redis:6379/0
      - SECRET_KEY=prop-data-guard-super-secret-key-change-in-production
      - DB_ENCRYPTION_KEY=${DB_ENCRYPTION_KEY:-}
    volumes:
      - ./backend:/app
    depends_on:
      - db
      - redis
    command: >
      sh -c "celery -A app.tasks.worker beat --loglevel=info"

  flower:
    build: ./backend
    container_name: pdg-flower
    environment:
      - REDIS_URL=redis://redis:6379/0
    ports:
      - "5555:5555"
    depends_on:
      - redis
      - celery_worker
    command: >
      sh -c "celery -A app.tasks.worker flower --port=5555"

volumes:
  pg_data:
  redis_data:
  minio_data:
