name: erp-starline

services:
  postgres:
    image: postgres:16-alpine
    container_name: erp-starline-postgres
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-starline}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-starline_password}
      POSTGRES_DB: ${POSTGRES_DB:-erp_starline}
    ports:
      - "5434:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-starline} -d ${POSTGRES_DB:-erp_starline}"]
      interval: 10s
      timeout: 5s
      retries: 5

  api:
    build:
      context: ./api
    container_name: erp-starline-api
    restart: unless-stopped
    working_dir: /app
    environment:
      DATABASE_URL: postgresql://${POSTGRES_USER:-starline}:${POSTGRES_PASSWORD:-starline_password}@postgres:5432/${POSTGRES_DB:-erp_starline}?schema=public
      PORT: 3000
      JWT_SECRET: ${JWT_SECRET:-erp-starline-change-this-secret}
      AUTH_TOKEN_TTL_SECONDS: ${AUTH_TOKEN_TTL_SECONDS:-28800}
      SMTP_HOST: ${SMTP_HOST:-mail.castorwheel.co}
      SMTP_PORT: ${SMTP_PORT:-465}
      SMTP_USERNAME: ${SMTP_USERNAME:-castorwheel@castorwheel.co}
      SMTP_PASSWORD: ${SMTP_PASSWORD:-}
      SMTP_FROM_EMAIL: ${SMTP_FROM_EMAIL:-castorwheel@castorwheel.co}
      SMTP_FROM_NAME: ${SMTP_FROM_NAME:-Castorwheel Support}
      DELETE_OTP_SECRET: ${DELETE_OTP_SECRET:-erp-starline-delete-otp}
    ports:
      - "${API_PORT:-3000}:3000"
      - "5555:5555"
    volumes:
      - ./api:/app
      - api_node_modules:/app/node_modules
    depends_on:
      postgres:
        condition: service_healthy
    command: sh -c "npm install && npx prisma generate && npx prisma db push && npm run db:seed && npm run start:dev"

  web:
    build:
      context: ./web
    container_name: erp-starline-web
    restart: unless-stopped
    working_dir: /app
    ports:
      - "${WEB_PORT:-4210}:4200"
    volumes:
      - ./web:/app
      - web_node_modules:/app/node_modules
    depends_on:
      - api
    command: sh -c "npm install && npm start -- --host 0.0.0.0 --poll 2000"

  pgadmin:
    image: dpage/pgadmin4:latest
    container_name: erp-starline-pgadmin
    restart: unless-stopped
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-admin@erpstarline.com}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
    ports:
      - "${PGADMIN_PORT:-5050}:80"
    volumes:
      - pgadmin_data:/var/lib/pgadmin
    depends_on:
      postgres:
        condition: service_healthy

volumes:
  postgres_data:
  pgadmin_data:
  api_node_modules:
  web_node_modules:
