""" Internal API for Suppliers Service Handles internal service-to-service operations """ from fastapi import APIRouter, Depends, HTTPException, Header from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy import select, func from uuid import UUID import structlog from app.core.database import get_db from app.core.config import settings from app.models.suppliers import Supplier, SupplierStatus logger = structlog.get_logger() router = APIRouter(prefix="/internal", tags=["internal"]) @router.get("/count") async def get_supplier_count( tenant_id: str, db: AsyncSession = Depends(get_db) ): """ Get count of active suppliers for onboarding status check. Internal endpoint for tenant service. """ try: count = await db.scalar( select(func.count()).select_from(Supplier) .where( Supplier.tenant_id == UUID(tenant_id), Supplier.status == SupplierStatus.active ) ) return { "count": count or 0, "tenant_id": tenant_id } except Exception as e: logger.error("Failed to get supplier count", tenant_id=tenant_id, error=str(e)) raise HTTPException(status_code=500, detail=f"Failed to get supplier count: {str(e)}")