-- ============================================================
-- Migration: Add ENGAGEMENT_TYPE_ID + DEFAULT_DEADLINE_DATE to TTM_TASK_TEMPLATE
-- Links tasks to CRM engagement types (replaces free-text SERVICE_NAME)
-- Adds a deadline date field
-- ============================================================

ALTER TABLE TTM_TASK_TEMPLATE
  ADD COLUMN ENGAGEMENT_TYPE_ID int DEFAULT NULL
    COMMENT 'FK → ENGAGEMENT_TYPE_MASTER.ID — links task to CRM engagement type'
  AFTER SERVICE_NAME;

ALTER TABLE TTM_TASK_TEMPLATE
  ADD INDEX idx_engagement_type (ENGAGEMENT_TYPE_ID);

-- Replace DEFAULT_DEADLINE_OFFSET_DAYS (int) with DEFAULT_DEADLINE_DATE (date)
ALTER TABLE TTM_TASK_TEMPLATE
  ADD COLUMN DEFAULT_DEADLINE_DATE DATE DEFAULT NULL
    COMMENT 'Default deadline date for the task'
  AFTER RECURRENCE_RULE_ID;

-- Drop the old offset column if it exists
ALTER TABLE TTM_TASK_TEMPLATE
  DROP COLUMN IF EXISTS DEFAULT_DEADLINE_OFFSET_DAYS;
