-- ============================================================================
-- Phase M4-p7b — JOBFORM_QC.qty_rejected column
-- ============================================================================
--
-- M4-p7b lets supervisors report N defects in one Report-defect tap (qty
-- stepper added to the dialog) instead of one POST per defect. The server
-- multiplies its existing decrements (task.qty_completed, task.qty_allocated,
-- task.qty_rejected, plus the bend_batch sibling-task cascade) by N. The
-- JOBFORM_QC row needs to record how many units this entry represents so
-- the M8 Evaluation Report can count defects correctly — five "1-unit"
-- entries and one "5-unit" entry have the same operational meaning and
-- should aggregate identically.
--
-- Default 1 — preserves the M4-p5..M4-p7a-h2 semantic that one
-- JOBFORM_QC row meant "one unit rejected". The few hundred existing rows
-- from M4-p5..M4-p7a-h2 stay accurate without backfill.
--
-- Idempotent — uses information_schema to skip the ALTER if the column
-- already exists. No innodb_strict_mode dance — JOBFORM_QC is a slim
-- table well below the row-size budget.
-- ============================================================================

SET @col_exists = (
    SELECT COUNT(*) FROM information_schema.COLUMNS
     WHERE TABLE_SCHEMA = DATABASE()
       AND TABLE_NAME = 'JOBFORM_QC'
       AND COLUMN_NAME = 'qty_rejected'
);

SET @ddl = IF(@col_exists = 0,
    'ALTER TABLE JOBFORM_QC ADD COLUMN qty_rejected INT NOT NULL DEFAULT 1 AFTER verdict',
    'SELECT "JOBFORM_QC.qty_rejected already exists — skipped"'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- ============================================================================
-- Verification:
--   DESCRIBE JOBFORM_QC;
--     expect qty_rejected INT NOT NULL DEFAULT 1 after verdict
--   SELECT COUNT(*) FROM JOBFORM_QC WHERE qty_rejected != 1;
--     should be 0 immediately after migration (every row defaults to 1).
-- ============================================================================
