-- ─────────────────────────────────────────────────────────────────────────────
-- Migration: Make TTM_TASK.CLIENT_ID nullable
-- Reason   : Task creation now derives the client from the selected CONTRACT
--            (via CONTRACT.COMPANY_NAME → clients.CLIENT_NAME lookup).
--            The "Select Client" field has been removed from the UI; users
--            identify the client by searching and selecting a Contract.
-- ─────────────────────────────────────────────────────────────────────────────

-- Step 1: Drop the old NOT NULL constraint on CLIENT_ID
--         (also adds/keeps STATUS_CODE and PRIORITY_CODE if not already present)
ALTER TABLE `TTM_TASK`
  MODIFY COLUMN `CLIENT_ID` bigint DEFAULT NULL COMMENT 'FK -> clients.ID (auto-derived from CONTRACT on task creation)';

-- Step 2 (optional): Also ensure STATUS_CODE and PRIORITY_CODE columns exist
--         Run only if they were not added in the previous migration.
--         Safe to skip if already applied.
--
-- ALTER TABLE `TTM_TASK`
--   ADD COLUMN IF NOT EXISTS `STATUS_CODE`   varchar(50) DEFAULT NULL AFTER `STATUS_ID`,
--   ADD COLUMN IF NOT EXISTS `PRIORITY_CODE` varchar(50) DEFAULT NULL AFTER `PRIORITY_ID`;
--
-- Note: MySQL < 8.0.3 does not support ADD COLUMN IF NOT EXISTS.
-- Use the plain version below only if the columns do not yet exist:
--
-- ALTER TABLE `TTM_TASK`
--   ADD COLUMN `STATUS_CODE`   varchar(50) DEFAULT NULL AFTER `STATUS_ID`,
--   ADD COLUMN `PRIORITY_CODE` varchar(50) DEFAULT NULL AFTER `PRIORITY_ID`;
