version: '3.9'

services:
  postgres:
    image: postgres:16-alpine
    container_name: auction_postgres
    restart: unless-stopped
    environment:
      POSTGRES_DB: ${DB_NAME:-auction_db}
      POSTGRES_USER: ${DB_USER:-auction_user}
      POSTGRES_PASSWORD: ${DB_PASSWORD:-auction_password}
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${DB_USER:-auction_user} -d ${DB_NAME:-auction_db}"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 10s
    networks:
      - auction_network

  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: auction_backend
    restart: unless-stopped
    depends_on:
      postgres:
        condition: service_healthy
    environment:
      NODE_ENV: ${NODE_ENV:-production}
      PORT: 3000
      DB_HOST: postgres
      DB_PORT: 5432
      DB_NAME: ${DB_NAME:-auction_db}
      DB_USER: ${DB_USER:-auction_user}
      DB_PASSWORD: ${DB_PASSWORD:-auction_password}
      JWT_SECRET: ${JWT_SECRET:-changeme_super_secret_jwt_key_in_production}
      JWT_EXPIRES_IN: ${JWT_EXPIRES_IN:-7d}
      JWT_REFRESH_SECRET: ${JWT_REFRESH_SECRET:-changeme_refresh_secret}
      JWT_REFRESH_EXPIRES_IN: ${JWT_REFRESH_EXPIRES_IN:-30d}
      SMTP_HOST: ${SMTP_HOST:-smtp.gmail.com}
      SMTP_PORT: ${SMTP_PORT:-587}
      SMTP_SECURE: ${SMTP_SECURE:-false}
      SMTP_USER: ${SMTP_USER}
      SMTP_PASS: ${SMTP_PASS}
      EMAIL_FROM_NAME: ${EMAIL_FROM_NAME:-Furniture Auction Platform}
      EMAIL_FROM_ADDRESS: ${EMAIL_FROM_ADDRESS:-noreply@furnitureauction.com.au}
      SQUARE_ACCESS_TOKEN: ${SQUARE_ACCESS_TOKEN}
      SQUARE_ENVIRONMENT: ${SQUARE_ENVIRONMENT:-sandbox}
      SQUARE_LOCATION_ID: ${SQUARE_LOCATION_ID}
      FRONTEND_URL: ${FRONTEND_URL:-http://localhost:3001}
      ADMIN_URL: ${ADMIN_URL:-http://localhost:3001/admin}
      UPLOAD_DIR: /app/uploads
      MAX_FILE_SIZE: ${MAX_FILE_SIZE:-5242880}
      SESSION_TIMEOUT_MINUTES: ${SESSION_TIMEOUT_MINUTES:-60}
      MAX_LOGIN_ATTEMPTS: ${MAX_LOGIN_ATTEMPTS:-5}
      ACCOUNT_LOCK_DURATION_MINUTES: ${ACCOUNT_LOCK_DURATION_MINUTES:-30}
      DEFAULT_PAYMENT_DEADLINE_HOURS: ${DEFAULT_PAYMENT_DEADLINE_HOURS:-48}
      MAX_RE_AWARD_ATTEMPTS: ${MAX_RE_AWARD_ATTEMPTS:-3}
      ENDING_SOON_HOURS: ${ENDING_SOON_HOURS:-48}
    ports:
      - "3000:3000"
    volumes:
      - uploads_data:/app/uploads
    networks:
      - auction_network
    healthcheck:
      test: ["CMD-SHELL", "wget -qO- http://localhost:3000/health || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 20s

  migrate:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: auction_migrate
    depends_on:
      postgres:
        condition: service_healthy
    environment:
      DB_HOST: postgres
      DB_PORT: 5432
      DB_NAME: ${DB_NAME:-auction_db}
      DB_USER: ${DB_USER:-auction_user}
      DB_PASSWORD: ${DB_PASSWORD:-auction_password}
    command: ["node", "src/migrations/run.js"]
    networks:
      - auction_network
    profiles:
      - migrate

  seed:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: auction_seed
    depends_on:
      postgres:
        condition: service_healthy
    environment:
      DB_HOST: postgres
      DB_PORT: 5432
      DB_NAME: ${DB_NAME:-auction_db}
      DB_USER: ${DB_USER:-auction_user}
      DB_PASSWORD: ${DB_PASSWORD:-auction_password}
    command: ["node", "src/seeds/seed.js"]
    networks:
      - auction_network
    profiles:
      - seed

volumes:
  postgres_data:
    driver: local
  uploads_data:
    driver: local

networks:
  auction_network:
    driver: bridge
