"""BSC四层配置 API"""
from fastapi import APIRouter, Depends, Query
from sqlalchemy.orm import Session
from typing import List

from app.database import get_db
from app.deps import get_entity_id
from app.auth_middleware import require_auth
from app.models import BscLayerConfig, Entity

router = APIRouter(prefix="/api/cma/bsc-layers", tags=["BSC层配置"],
    dependencies=[Depends(require_auth)],
)


@router.get("")
def list_bsc_layers(entity_id: int = Depends(get_entity_id), db: Session = Depends(get_db)):
    """获取某企业的BSC四层权重配置"""
    # 验证企业存在
    entity = db.query(Entity).filter(Entity.id == entity_id).first()
    if not entity:
        from fastapi.responses import JSONResponse
        return JSONResponse(status_code=404, content={"detail": "企业不存在"})

    layers = db.query(BscLayerConfig).filter(
        BscLayerConfig.entity_id == entity_id
    ).order_by(BscLayerConfig.id).all()

    return {
        "entity_id": entity_id,
        "entity_name": entity.short_name or entity.name,
        "layers": [
            {
                "id": l.id,
                "layer": l.layer,
                "weight": float(l.weight),
                "kpi_count_min": l.kpi_count_min,
                "kpi_count_max": l.kpi_count_max,
            }
            for l in layers
        ]
    }
