version: "3.9"

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

  migrate:
    build: .
    container_name: checkflow-migrate
    depends_on:
      postgres:
        condition: service_healthy
    environment:
      DATABASE_URL: postgresql://checkflow:${POSTGRES_PASSWORD:-changeme}@postgres:5432/checkflow
    command: npx prisma migrate deploy
    restart: "no"

  app:
    build: .
    container_name: checkflow-app
    restart: unless-stopped
    depends_on:
      migrate:
        condition: service_completed_successfully
    ports:
      - "${PORT:-3000}:3000"
    environment:
      DATABASE_URL: postgresql://checkflow:${POSTGRES_PASSWORD:-changeme}@postgres:5432/checkflow
      NEXTAUTH_URL: ${NEXTAUTH_URL:-http://localhost:3000}
      NEXTAUTH_SECRET: ${NEXTAUTH_SECRET:-change-this-secret-in-production}
      NODE_ENV: production
    volumes:
      - ./prisma:/app/prisma

volumes:
  postgres_data: