-- Workcenter status cards aggregation
-- Placeholders:
--   LATEST_STATUS_SUBQUERY - Base subquery for latest resource status
--   WHERE_CLAUSE          - Dynamic WHERE conditions

SELECT
    WORKCENTERNAME,
    COUNT(*) as TOTAL,
    SUM(CASE WHEN NEWSTATUSNAME = 'PRD' THEN 1 ELSE 0 END) as PRD,
    SUM(CASE WHEN NEWSTATUSNAME = 'SBY' THEN 1 ELSE 0 END) as SBY,
    SUM(CASE WHEN NEWSTATUSNAME = 'UDT' THEN 1 ELSE 0 END) as UDT,
    SUM(CASE WHEN NEWSTATUSNAME = 'SDT' THEN 1 ELSE 0 END) as SDT,
    SUM(CASE WHEN NEWSTATUSNAME = 'EGT' THEN 1 ELSE 0 END) as EGT,
    SUM(CASE WHEN NEWSTATUSNAME = 'NST' THEN 1 ELSE 0 END) as NST
FROM ({{ LATEST_STATUS_SUBQUERY }}) rs
WHERE {{ WHERE_CLAUSE }}
GROUP BY WORKCENTERNAME
