services:

  # ── Base de datos interna (admins, sesiones, logs) ─────────────────────────────
  internal_db:
    image: postgres:16
    container_name: internal_postgres_db
    restart: unless-stopped

    environment:
      POSTGRES_USER: ${INTERNAL_DB_USER}
      POSTGRES_PASSWORD: ${INTERNAL_DB_PASSWORD}
      POSTGRES_DB: ${INTERNAL_DB_NAME}

    ports:
      - "${INTERNAL_DB_PORT}:5432"

    volumes:
      - internal_db_data:/var/lib/postgresql/data
      - ./internal/schemas/001_admins_table_setup.schema.sql:/docker-entrypoint-initdb.d/001_init.sql:ro

    networks:
      - opendata_network

    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${INTERNAL_DB_USER} -d ${INTERNAL_DB_NAME}"]
      interval: 10s
      timeout: 5s
      retries: 5

  # ── Base de datos de plataforma de usuarios ────────────────────────────────────
  platform_db:
    image: postgres:16
    container_name: user_postgres_db
    restart: unless-stopped

    environment:
      POSTGRES_USER: ${PLATFORM_DB_USER}
      POSTGRES_PASSWORD: ${PLATFORM_DB_PASSWORD}
      POSTGRES_DB: ${PLATFORM_DB_NAME}

    ports:
      - "${PLATFORM_DB_PORT}:5432"

    volumes:
      - platform_db_data:/var/lib/postgresql/data
      - ./user_platform/schemas/001_platform_table_setup.schema.sql:/docker-entrypoint-initdb.d/001_init.sql:ro

    networks:
      - opendata_network

    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${PLATFORM_DB_USER} -d ${PLATFORM_DB_NAME}"]
      interval: 10s
      timeout: 5s
      retries: 5

  # ── MinIO (almacenamiento de archivos) ─────────────────────────────────────────
  minio:
    image: minio/minio:latest
    container_name: minio
    restart: unless-stopped

    command: server /data --console-address ":9001"

    environment:
      MINIO_ROOT_USER: ${MINIO_ROOT_USER}
      MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD}

    ports:
      - "${MINIO_PORT}:9000"
      - "${MINIO_CONSOLE_PORT}:9001"

    volumes:
      - minio_data:/data

    networks:
      - opendata_network

    healthcheck:
      test: ["CMD", "mc", "ready", "local"]
      interval: 15s
      timeout: 5s
      retries: 5

  # ── Backend interno (FastAPI) ──────────────────────────────────────────────────
  backend:
    build:
      context: ./internal
      dockerfile: Dockerfile.backend
    container_name: internal_backend
    restart: unless-stopped

    environment:
      JWT_SECRET_KEY: ${JWT_SECRET_KEY}
      JWT_ACCESS_TOKEN_EXPIRE_MINUTES: ${JWT_ACCESS_TOKEN_EXPIRE_MINUTES}
      INTERNAL_DB_URL: postgresql://${INTERNAL_DB_USER}:${INTERNAL_DB_PASSWORD}@internal_db:5432/${INTERNAL_DB_NAME}
      PLATFORM_DB_URL: postgresql://${PLATFORM_DB_USER}:${PLATFORM_DB_PASSWORD}@platform_db:5432/${PLATFORM_DB_NAME}
      MODELS_BASE_PATH: /app/models

    ports:
      - "${BACKEND_PORT}:8000"

    depends_on:
      internal_db:
        condition: service_healthy
      platform_db:
        condition: service_healthy

    networks:
      - opendata_network

  # ── Backend de plataforma de usuarios (FastAPI + Frontend) ─────────────────────
  user_backend:
    build:
      context: ./user_platform
      dockerfile: Dockerfile
    container_name: user_platform_backend
    restart: unless-stopped

    environment:
      PLATFORM_DB_URL: postgresql://${PLATFORM_DB_USER}:${PLATFORM_DB_PASSWORD}@platform_db:5432/${PLATFORM_DB_NAME}

    ports:
      - "8001:8000"

    depends_on:
      platform_db:
        condition: service_healthy

    networks:
      - opendata_network

volumes:
  internal_db_data:
  platform_db_data:
  minio_data:

networks:
  opendata_network:
    driver: bridge
