-- ============================================================================
-- TTM_TASK_GENERATION_LOG — Deduplication table for recurring task generation
-- Run once to create the table. Idempotent (IF NOT EXISTS).
-- ============================================================================

CREATE TABLE IF NOT EXISTS `TTM_TASK_GENERATION_LOG` (
  `ID`            bigint       NOT NULL AUTO_INCREMENT,
  `TEMPLATE_ID`   bigint       NOT NULL COMMENT 'FK -> TTM_TASK_TEMPLATE.ID',
  `CLIENT_ID`     bigint       NOT NULL COMMENT 'CONTRACT_ID from TTM_CLIENT_FL_MAPPING',
  `TASK_ID`       bigint       NOT NULL COMMENT 'FK -> TTM_TASK.ID (the generated task)',
  `TASK_PERIOD`   varchar(50)  NOT NULL COMMENT 'Period key e.g. "2026-03", "2026-Q1", "2026-W10"',
  `RULE_CODE`     varchar(50)  NOT NULL COMMENT 'Recurrence rule e.g. MONTHLY, QUARTERLY',
  `GENERATED_AT`  datetime     NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`ID`),
  UNIQUE KEY `UQ_TPL_CLIENT_PERIOD` (`TEMPLATE_ID`, `CLIENT_ID`, `TASK_PERIOD`),
  KEY `IDX_TASK_ID` (`TASK_ID`),
  KEY `IDX_GENERATED_AT` (`GENERATED_AT`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
