version: '3.8'

services:
  postgres:
    image: postgres:15-alpine
    container_name: erp-generic-db
    restart: unless-stopped
    environment:
      POSTGRES_DB: ${POSTGRES_DB:-erp_generic}
      POSTGRES_USER: ${POSTGRES_USER:-erp_admin}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-erp_secret_2024}
      PGDATA: /var/lib/postgresql/data/pgdata
    ports:
      - "${POSTGRES_PORT:-5432}:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./ddl:/docker-entrypoint-initdb.d:ro
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-erp_admin} -d ${POSTGRES_DB:-erp_generic}"]
      interval: 10s
      timeout: 5s
      retries: 5

  # Optional: pgAdmin for database management
  pgadmin:
    image: dpage/pgadmin4:latest
    container_name: erp-generic-pgadmin
    restart: unless-stopped
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_EMAIL:-admin@erp-generic.local}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_PASSWORD:-admin123}
      PGADMIN_CONFIG_SERVER_MODE: 'False'
    ports:
      - "${PGADMIN_PORT:-5050}:80"
    volumes:
      - pgadmin_data:/var/lib/pgadmin
    depends_on:
      postgres:
        condition: service_healthy
    profiles:
      - tools

volumes:
  postgres_data:
    driver: local
  pgadmin_data:
    driver: local

networks:
  default:
    name: erp-generic-network
