# =============================================================================
# MOPC Platform - Development Docker Compose
# =============================================================================
# Use this for local development. Includes PostgreSQL, MinIO, and the app.

services:
  postgres:
    image: postgres:16-alpine
    container_name: mopc-postgres-dev
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_USER=${POSTGRES_USER:-mopc}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-devpassword}
      - POSTGRES_DB=${POSTGRES_DB:-mopc}
    volumes:
      - postgres_dev_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U mopc"]
      interval: 5s
      timeout: 5s
      retries: 5

  minio:
    image: minio/minio
    container_name: mopc-minio-dev
    ports:
      - "9000:9000"
      - "9001:9001"
    environment:
      - MINIO_ROOT_USER=${MINIO_ACCESS_KEY:-minioadmin}
      - MINIO_ROOT_PASSWORD=${MINIO_SECRET_KEY:-minioadmin}
    volumes:
      - minio_dev_data:/data
    command: server /data --console-address ":9001"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
      interval: 30s
      timeout: 20s
      retries: 3

  # MinIO client to create default bucket on startup
  createbuckets:
    image: minio/mc
    depends_on:
      minio:
        condition: service_healthy
    environment:
      - MINIO_ACCESS_KEY=${MINIO_ACCESS_KEY:-minioadmin}
      - MINIO_SECRET_KEY=${MINIO_SECRET_KEY:-minioadmin}
      - MINIO_BUCKET=${MINIO_BUCKET:-mopc-files}
    entrypoint: >
      /bin/sh -c "
      mc alias set myminio http://minio:9000 $${MINIO_ACCESS_KEY} $${MINIO_SECRET_KEY};
      mc mb --ignore-existing myminio/$${MINIO_BUCKET};
      mc anonymous set download myminio/$${MINIO_BUCKET};
      echo 'Bucket created successfully';
      "

  # Next.js application
  app:
    build:
      context: ..
      dockerfile: docker/Dockerfile.dev
    container_name: mopc-app-dev
    ports:
      - "3000:3000"
    env_file:
      - ../.env
    environment:
      - DATABASE_URL=postgresql://${POSTGRES_USER:-mopc}:${POSTGRES_PASSWORD:-devpassword}@postgres:5432/${POSTGRES_DB:-mopc}
      - NEXTAUTH_URL=${NEXTAUTH_URL:-http://localhost:3000}
      - NEXTAUTH_SECRET=${NEXTAUTH_SECRET:-dev-secret-key-for-local-development-only}
      - AUTH_SECRET=${AUTH_SECRET:-dev-secret-key-for-local-development-only}
      - MINIO_ENDPOINT=http://minio:9000
      - MINIO_ACCESS_KEY=${MINIO_ACCESS_KEY:-minioadmin}
      - MINIO_SECRET_KEY=${MINIO_SECRET_KEY:-minioadmin}
      - MINIO_BUCKET=${MINIO_BUCKET:-mopc-files}
      - NODE_ENV=development
      - SMTP_HOST=${SMTP_HOST}
      - SMTP_PORT=${SMTP_PORT:-587}
      - SMTP_USER=${SMTP_USER}
      - SMTP_PASS=${SMTP_PASS}
      - EMAIL_FROM=${EMAIL_FROM}
    volumes:
      - ../src:/app/src
      - ../public:/app/public
      - ../prisma:/app/prisma
    depends_on:
      postgres:
        condition: service_healthy
      minio:
        condition: service_healthy

volumes:
  postgres_dev_data:
  minio_dev_data:
