services:
  # ─── Base de Datos PostgreSQL ───
  postgres:
    image: postgres:15-alpine
    container_name: saas_postgres
    environment:
      POSTGRES_USER: ${DB_USERNAME:-postgres}
      POSTGRES_PASSWORD: ${DB_PASSWORD:-i2osietnq}
      POSTGRES_DB: ${DB_NAME:-saasDB}
    ports:
      - "${DB_PORT:-5433}:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./docker/init.sql:/docker-entrypoint-initdb.d/init.sql
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready -U ${DB_USERNAME:-postgres} -d ${DB_NAME:-saasDB}" ]
      interval: 10s
      timeout: 5s
      retries: 5
    restart: always
    networks:
      - saas_network
  # ─── pgAdmin (Administrador de BD) ───

  # ─── pgAdmin (Administrador de BD) ───
  pgadmin:
    image: dpage/pgadmin4:latest
    container_name: saas_pgadmin
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_EMAIL:-admin@saas.com}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_PASSWORD:-admin123}
      PGADMIN_CONFIG_SERVER_MODE: "False"
    ports:
      - "5050:80"
    volumes:
      - pgadmin_data:/var/lib/pgadmin
    depends_on:
      postgres:
        condition: service_healthy
    restart: unless-stopped
    networks:
      - saas_network

  # ─── Backend NestJS ───
  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: saas_backend
    environment:
      DB_HOST: postgres
      DB_PORT: 5433
      DB_USERNAME: ${DB_USERNAME:-postgres}
      DB_PASSWORD: ${DB_PASSWORD:-i2osietnq}
      DB_NAME: ${DB_NAME:-saasDB}
      JWT_SECRET: ${JWT_SECRET:-supersecretkey}
      PORT: 3001
    ports:
      - "3001:3001"
    volumes:
      - backend_uploads:/app/uploads
    depends_on:
      postgres:
        condition: service_healthy
    restart: unless-stopped
    networks:
      - saas_network

  # ─── Frontend React (Nginx) ───
  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
      args:
        VITE_API_URL: /api
    container_name: saas_frontend
    ports:
      - "82:80"
    depends_on:
      - backend
    restart: unless-stopped
    networks:
      - saas_network

volumes:
  postgres_data:
  pgadmin_data:
  backend_uploads:


networks:
  saas_network:
    driver: bridge
