-- =====================================================================================
-- Set max_proxy_registrations = 4 for TAT (PT_TAT), cascading down all three levels:
--   1) program_type_v1   (the type)
--   2) program_v1        (all programs of that type)
--   3) program_session   (all sessions of those programs)
-- Idempotent: deterministic UPDATEs. Skips soft-deleted rows; stamps audit fields.
-- =====================================================================================

-- 1) Program type
UPDATE program_type_v1
SET max_proxy_registrations = 4,
    updated_at = NOW()
WHERE key = 'PT_TAT'
  AND deleted_at IS NULL;

-- 2) Programs of that type
UPDATE program_v1
SET max_proxy_registrations = 4,
    updated_at = NOW()
WHERE program_type_id = (SELECT id FROM program_type_v1 WHERE key = 'PT_TAT')
  AND deleted_at IS NULL;

-- 3) Sessions of those programs
UPDATE program_session
SET max_proxy_registrations = 4,
    updated_at = NOW()
WHERE program_id IN (
        SELECT id FROM program_v1
        WHERE program_type_id = (SELECT id FROM program_type_v1 WHERE key = 'PT_TAT')
          AND deleted_at IS NULL
      )
  AND deleted_at IS NULL;
