-- ============================================================
-- FashCognitive Apparel MES - Phase 7: Manufacturing Control
-- Center (Level 3 of the MES spec: Control).
--
-- The Control Center is a read-only operational cockpit that
-- aggregates the Planning, Execution, Intelligence (AI) and
-- Logistics layers built in Phases 1-6 into a single view,
-- powered by a deterministic exception / alert engine:
--
--   api/manufacturing/control-center.php
--     overview  - fleet KPIs
--     orders    - live production snapshot w/ risk scoring
--     lines     - sewing line load vs capacity
--     shipment  - produced / packed / dispatched / manifested
--     alerts    - exception engine (schedule, quality, material,
--                 downtime, bottlenecks, PO ETD, dispatch ETA)
--
-- It introduces no new operational tables; it consumes the
-- analytics of the previous phases.
--
-- This migration only adds the supporting index used by the
-- Control Center and AI analytics for per-order operation
-- rollups.
-- ============================================================

SET @db_name = DATABASE();

SET @sql = (SELECT IF(
  (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS
    WHERE TABLE_SCHEMA = @db_name AND TABLE_NAME = 'production_order_operations'
      AND INDEX_NAME = 'idx_opo_order_seq') = 0,
  'ALTER TABLE production_order_operations ADD KEY idx_opo_order_seq (prod_order_id, sequence, status)',
  'SELECT 1'));
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
