-- Ensure each active tenant has a default freeze policy (7 days)

INSERT INTO `play_freeze_policy` (`id`, `tenant_id`, `clerk_id`, `freeze_hours`, `created_by`, `created_time`, `updated_by`, `updated_time`, `deleted`, `version`)
SELECT
  REPLACE(UUID(), '-', ''),
  t.`tenant_id`,
  NULL,
  168,
  'migration_v8_default_freeze',
  NOW(),
  'migration_v8_default_freeze',
  NOW(),
  0,
  1
FROM `sys_tenant` t
WHERE (t.`deleted` IS NULL OR t.`deleted` = 0)
  AND NOT EXISTS (
    SELECT 1
    FROM `play_freeze_policy` p
    WHERE p.`tenant_id` = t.`tenant_id`
      AND p.`clerk_id` IS NULL
      AND (p.`deleted` IS NULL OR p.`deleted` = 0)
  );
