services:
  postgres:
    image: postgres:16-alpine
    environment:
      POSTGRES_DB: colorjet_erp
      POSTGRES_USER: colorjet
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-CHANGE_THIS_DB_PASSWORD}
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./database/0001_schema_postgres.sql:/docker-entrypoint-initdb.d/0001_schema_postgres.sql:ro
      - ./database/0002_role_permissions.sql:/docker-entrypoint-initdb.d/0002_role_permissions.sql:ro
      - ./database/0003_enterprise_required_tables.sql:/docker-entrypoint-initdb.d/0003_enterprise_required_tables.sql:ro
      - ./database/0004_push_notification_devices.sql:/docker-entrypoint-initdb.d/0004_push_notification_devices.sql:ro
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U colorjet -d colorjet_erp"]
      interval: 10s
      timeout: 5s
      retries: 10
    restart: unless-stopped

  backend:
    build:
      context: ./backend
    env_file: .env
    environment:
      DATABASE_URL: postgresql://colorjet:${POSTGRES_PASSWORD:-CHANGE_THIS_DB_PASSWORD}@postgres:5432/colorjet_erp
      UPLOAD_DIR: /app/uploads
    volumes:
      - uploads_data:/app/uploads
      - ./data:/app/data:ro
    depends_on:
      postgres:
        condition: service_healthy
    restart: unless-stopped

  web:
    build:
      context: ./web-admin
      args:
        VITE_API_BASE_URL: /api
        VITE_SOCKET_URL: /
    restart: unless-stopped

  nginx:
    image: nginx:1.27-alpine
    ports:
      - "80:80"
    volumes:
      - ./deploy/nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
    depends_on:
      - backend
      - web
    restart: unless-stopped

volumes:
  postgres_data:
  uploads_data:
