-- ============================================================================
-- Phase M4-p7c-h1 C — JOBFORM_BEND_CYCLE_LOG audit table
-- ============================================================================
--
-- Captures which physical furnace bend cycle each Glass Bending
-- Mark-Complete batch went into. AGG's bender holds 2 moulds per
-- cycle (one windshield's layer stack per mould); a glass with
-- qty_required > 1 typically spans MULTIPLE cycles (a qty=3 order
-- is bent in 2+1 — first cycle does units 1+2, second does unit 3).
-- Per-mould (= per-JOBFORM_GLASS) defect cascade is correct per the
-- corrected invariant 47, but the supervisor still needs to track
-- which cycle each batch went through for downstream QC root-cause
-- ("which glasses went into cycle BC-145", "did cycle BC-145 have
-- a temperature anomaly").
--
-- Append-only — one row per Mark-Complete call on a Glass Bending
-- task that carried a cycle_no body field. Multiple rows per task
-- are normal (the qty=3 example above lands 2 rows).
--
-- Shape mirrors JOBFORM_REPROCESS (per CLAUDE.md §15) — an event
-- log with task_id back-pointer + qty + supervisor stamp.
--
-- The Mark-Complete handler (task_complete.php) inserts here when
-- the request body carries a non-blank cycle_no AND the parent
-- stage is 'Glass Bending'. Other stages ignore the field (so a
-- stale client sending it on a non-bend task doesn't error — silent
-- drop is the right ergonomics here).
--
-- A future "Bend Cycle Chart Viewer" (analogous to the
-- Autoclave Cycle Chart Viewer in CLAUDE.md §9 backlog) reads from
-- this log to render per-cycle T/P curves keyed off cycle_no.
--
-- Idempotent — CREATE TABLE IF NOT EXISTS. No backfill (legacy
-- Glass Bending Mark-Completes from M4-p3 onward don't have
-- cycle_no captured; those rows pre-date this feature, the column
-- being NULL for them is correct).
-- ============================================================================

CREATE TABLE IF NOT EXISTS JOBFORM_BEND_CYCLE_LOG (
    id              INT          UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    task_id         INT          UNSIGNED NOT NULL,
    cycle_no        VARCHAR(50)           NOT NULL,
    qty             INT                   NOT NULL,
    completed_at    DATETIME              NOT NULL DEFAULT CURRENT_TIMESTAMP,
    completed_by    VARCHAR(50)           NULL,
    KEY idx_task        (task_id),
    KEY idx_cycle       (cycle_no),
    KEY idx_completed   (completed_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
  COMMENT='M4-p7c-h1 C — Glass Bending cycle audit log. One row per Mark-Complete batch that carried a cycle_no.';

-- ============================================================================
-- Verification:
--   DESCRIBE JOBFORM_BEND_CYCLE_LOG;
--     expect id (PK AUTO_INC) + task_id + cycle_no + qty +
--            completed_at + completed_by.
--   SELECT COUNT(*) FROM JOBFORM_BEND_CYCLE_LOG;
--     should be 0 immediately after migration (no Mark-Completes
--     have carried a cycle_no until the API delta lands).
-- ============================================================================
