-- ================================================
-- Migration: PCF-003 — Set Parental Consent Upload Deadline
-- Date: 2026-06-12
-- Sets parental_consent_upload_deadline directly on
-- program_v1 and program_session for PT_TAT programs.
-- Safe to re-run (idempotent — uses explicit timestamp).
-- ================================================

-- ------------------------------------------------
-- STEP 1: Set deadline on all PT_TAT program_v1 rows
-- ------------------------------------------------

UPDATE program_v1 pv
SET parental_consent_upload_deadline = '2026-06-30 23:59:59+05:30'
FROM program_type_v1 pt
WHERE pv.program_type_id = pt.id
  AND pt.key = 'PT_TAT';

-- ------------------------------------------------
-- STEP 2: Set deadline on all linked program_session rows
-- ------------------------------------------------

UPDATE program_session ps
SET parental_consent_upload_deadline = '2026-06-30 23:59:59+05:30'
FROM program_v1 pv
JOIN program_type_v1 pt ON pv.program_type_id = pt.id
WHERE ps.program_id = pv.id
  AND pt.key = 'PT_TAT';

-- ------------------------------------------------
-- Verification
-- ------------------------------------------------

SELECT
    'program_v1'     AS source,
    pv.name          AS identifier,
    pv.parental_consent_upload_deadline
FROM program_v1 pv
JOIN program_type_v1 pt ON pv.program_type_id = pt.id
WHERE pt.key = 'PT_TAT'

UNION ALL

SELECT
    'program_session',
    ps.name,
    ps.parental_consent_upload_deadline
FROM program_session ps
JOIN program_v1 pv ON ps.program_id = pv.id
JOIN program_type_v1 pt ON pv.program_type_id = pt.id
WHERE pt.key = 'PT_TAT'

ORDER BY source, identifier;
