-- ================================================
-- Migration: PCF-ENT — Parental Consent Form for PT_ENTRAINMENT
-- Date: 2026-08-13
-- DRAFT ONLY — not executed against any database, not yet reviewed/approved.
-- Mirrors database/migrations/2026-06-11-03-parental-consent-tat.sql, adapted for
-- PT_ENTRAINMENT. Enables parental consent on the Entrainment program type, sets the
-- consent form content, sets child_max_age = 18 (per the Entrainment T&C clause 6 —
-- see database/entrainment-migrations-v2(2026-08-10)/04terms-conditions-content-update.sql —
-- "Parental consent is a must for participants who are below 18 years of age"), and
-- clones all of it down to existing PT_ENTRAINMENT programs and sessions.
-- No schema changes (columns already added by database/migrations/2026-06-11-01-parental-consent-form.sql).
-- Safe to re-run (idempotent).
-- ================================================
--
-- OPEN FLAG — please confirm before running:
--   parental_consent_upload_deadline is set to 2025-08-28 (Thursday), taken verbatim from
--   the Entrainment T&C clause 6 wording ("...submitted to infinitheism on or before
--   Thursday, 28 August 2025."). If that date was specific to a past/2025 cohort and this
--   migration is being run for a new cohort, replace both this value and the T&C copy's
--   date before running.
-- ================================================

-- ------------------------------------------------
-- PART 1: Enable parental consent on program_type_v1 for PT_ENTRAINMENT
-- Dynamic placeholders resolved at render time (same generic renderer used for PT_TAT,
-- see src/registration/registration.service.ts:buildParentalConsentAttachment):
--   {{salutation}}              → 'Master' if gender = Male, 'Miss' if gender = Female, else ''
--   {{seeker_name}}             → hdb_program_registration.full_name
--   {{programme_name}}          → program_v1.name
--   {{registration_portal_url}} → process.env.SEEKER_FE_BASE_URL (env-driven, per environment)
--   {{parental_consent_deadline}} → program/session parental_consent_upload_deadline (formatTandCDate)
-- ------------------------------------------------

UPDATE program_type_v1
SET
    parental_consent_enabled            = true,
    parental_consent_form_as_attachment = false,
    parental_consent_upload_deadline    = '2026-08-28 23:59:59+05:30',
    parental_consent_form_content       = $html$<!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1, width=device-width" />
        <link
          rel="stylesheet"
          href="https://fonts.googleapis.com/css2?family=Noto Sans:wght@400;600&display=swap"
        />
        <style>
          body {
            margin: 0;
          }
        </style>
      </head>
      <body>
        <div
          style="
            width: 100%;
            position: relative;
            background-color: #fff;
            overflow: hidden;
            display: flex;
            flex-direction: column;
            align-items: flex-start;
            justify-content: flex-start;
            padding: 0px 0px 605px;
            box-sizing: border-box;
            letter-spacing: normal;
          "
        >
          <section
            style="
              width: 100%;
              margin: 0 !important;
              position: absolute;
              height: 100%;
              top: 0px;
              right: 0px;
              bottom: 0px;
              left: 0px;
              display: flex;
              flex-direction: column;
              align-items: center;
              gap: 28px;
              padding: 80px 40px 40px;
              box-sizing: border-box;
              min-height: 842px;
              max-width: 100%;
              z-index: 1;
              text-align: left;
              font-size: 12px;
              color: #000;
              font-family: 'Noto Sans';
            "
          >
            <img
              style="
                width: 268px;
                height: 58px;
                position: relative;
                object-fit: contain;
              "
              loading="lazy"
              alt=""
              src="https://infinitheism-serverless.s3.ap-south-1.amazonaws.com/static-images/logo/entrainment-logo.svg"
            />
            <div style="display: flex; justify-content: center; text-decoration: underline;">
              <b style="font-weight: 600; font-size: 15px;">Parental Consent/Declaration</b>
            </div>
            <div
              style="
                align-self: stretch;
                display: flex;
                flex-direction: column;
                align-items: flex-start;
                justify-content: flex-start;
                gap: 24px;
              "
            >
              <div style="align-self: stretch; position: relative;">
                <span>I,______________________, father/mother/guardian of </span>
                <span style="font-weight: 600;">{{salutation}} {{seeker_name}},</span>
                <span> ("Participant"), hereby confirm that:</span>
              </div>
              <div
                style="
                  align-self: stretch;
                  display: flex;
                  flex-direction: column;
                  align-items: flex-start;
                  justify-content: center;
                  gap: 24px;
                "
              >
                <ol type="a" style="margin-left: -24px; display: flex; flex-direction: column; align-items: flex-start; justify-content: center; gap: 24px;">
                  <div style="position: relative;">
                    <li>
                      I have read, understood and agree to the 'Terms &amp; Conditions' of
                      '{{programme_name}}' ("Program") and hereby consent on behalf of the
                      Participant to participate in the Program.
                    </li>
                  </div>
                  <div style="align-self: stretch; position: relative;">
                    <li style="margin: 0">
                      I hereby voluntarily and irrevocably allow the Participant to participate and
                      experience the Program.
                    </li>
                  </div>
                  <div style="align-self: stretch; position: relative;">
                    <li style="margin: 0">
                      I undertake on behalf of the Participant that the Participant will strictly adhere to
                      the discipline expected during the Program.
                    </li>
                  </div>
                  <div style="align-self: stretch; position: relative;">
                    <li style="margin: 0">
                      I hereby consent, on behalf of the Participant, to the collection, processing, storage
                      and use of Participant's personal data by Infinitheism Spiritual Foundation Private
                      Limited in accordance with the 'Terms &amp; Conditions' of the Program.
                    </li>
                  </div>
                </ol>
              </div>
            </div>
            <div
              style="
                align-self: stretch;
                display: flex;
                flex-direction: column;
                align-items: flex-start;
                justify-content: flex-start;
                gap: 24px;
              "
            >
              <div style="align-self: stretch; position: relative;">
                <p style="margin: 0 0 24px;">Parent's/Guardian's Signature: <span>_______________________________</span></p>
                <p style="margin: 0;">Parent's/Guardian's Name: <span>____________________________________</span></p>
              </div>
              <div
                style="
                  align-self: stretch;
                  display: flex;
                  flex-direction: column;
                  align-items: flex-start;
                  justify-content: flex-start;
                  gap: 40px;
                "
              >
                <div
                  style="
                    align-self: stretch;
                    display: flex;
                    flex-direction: row;
                    align-items: flex-start;
                    justify-content: space-between;
                    padding: 0px 40px 0px 0px;
                    gap: 20px;
                  "
                >
                  <div style="position: relative; display: inline-block;">Date: <span>________________</span></div>
                  <div style="position: relative; display: inline-block;">Place: <span>________________</span></div>
                </div>
                <div style="align-self: stretch; position: relative;">
                  <hr style="align-self: stretch; border: none; border-top: 1px solid #000000; width: 100%; margin: 0 0 12px;" />
                  <p style="margin: 0">
                    <span>Note: Please sign and upload a scanned copy through the </span>
                    <a href="{{registration_portal_url}}" style="color: #1859b4; text-decoration: underline;">registration portal</a>
                    <span> or email it to </span>
                    <a href="mailto:events@infinitheism.com" style="color: #1859b4; text-decoration: underline;">events@infinitheism.com</a>
                    <span> on or before {{parental_consent_deadline}}.</span>
                  </p>
                </div>
              </div>
            </div>
          </section>
          <section style="align-self: stretch; height: 237px; position: relative">
            <img
              style="
                position: absolute;
                top: 0px;
                left: -3px;
                width: 100%;
                height: 100%;
                object-fit: cover;
              "
              alt=""
              src="https://entrainment-24.s3.amazonaws.com/Images/parentalConsentBackground.png"
            />
            <div
              style="
                position: absolute;
                top: 178px;
                left: 0px;
                background: linear-gradient(180deg, rgba(255, 255, 255, 0), #fff);
                width: 595px;
                height: 59px;
                z-index: 0;
              "
            ></div>
          </section>
        </div>
      </body>
    </html>$html$,
    updated_at                          = CURRENT_TIMESTAMP
WHERE key = 'PT_ENTRAINMENT'
  AND deleted_at IS NULL;

-- ------------------------------------------------
-- PART 2: Clone program_type_v1 settings down to all
-- existing PT_ENTRAINMENT program_v1 rows that still have the
-- default (false). Programs already explicitly configured
-- are left untouched.
-- ------------------------------------------------

UPDATE program_v1 p
SET
    parental_consent_enabled            = pt.parental_consent_enabled,
    parental_consent_form_as_attachment = pt.parental_consent_form_as_attachment,
    parental_consent_upload_deadline    = pt.parental_consent_upload_deadline,
    parental_consent_form_content       = pt.parental_consent_form_content,
    updated_at                          = CURRENT_TIMESTAMP
FROM program_type_v1 pt
WHERE pt.key              = 'PT_ENTRAINMENT'
  AND pt.deleted_at       IS NULL
  AND p.program_type_id   = pt.id
  AND p.deleted_at        IS NULL
  AND p.parental_consent_enabled = false;

-- ------------------------------------------------
-- PART 3: Clone program_v1 settings down to all
-- existing PT_ENTRAINMENT program_session rows (same guard).
-- ------------------------------------------------

UPDATE program_session s
SET
    parental_consent_enabled            = p.parental_consent_enabled,
    parental_consent_form_as_attachment = p.parental_consent_form_as_attachment,
    parental_consent_upload_deadline    = p.parental_consent_upload_deadline,
    parental_consent_form_content       = p.parental_consent_form_content,
    updated_at                          = CURRENT_TIMESTAMP
FROM program_v1 p
JOIN program_type_v1 pt ON pt.id = p.program_type_id
WHERE pt.key                         = 'PT_ENTRAINMENT'
  AND pt.deleted_at                  IS NULL
  AND p.deleted_at                   IS NULL
  AND s.program_id                   = p.id
  AND s.deleted_at                   IS NULL
  AND s.parental_consent_enabled     = false;

-- ------------------------------------------------
-- PART 4: Set child_max_age = 18 for PT_ENTRAINMENT programs
-- (age cutoff below which parental consent is required — see
-- src/common/utils/common.util.ts:isParentalConsentRequired).
-- ------------------------------------------------

UPDATE program_v1
SET child_max_age = 18, updated_at = CURRENT_TIMESTAMP
WHERE deleted_at IS NULL
  AND program_type_id = (
    SELECT id FROM program_type_v1
    WHERE key = 'PT_ENTRAINMENT' AND deleted_at IS NULL
  );

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

-- 1. Confirm program_type_v1 settings
SELECT key, parental_consent_enabled, parental_consent_form_as_attachment, parental_consent_upload_deadline,
       LEFT(parental_consent_form_content, 80) AS form_content_preview
FROM program_type_v1
WHERE key = 'PT_ENTRAINMENT';

-- 2. Count program_v1 rows updated
SELECT COUNT(*) AS entrainment_programs_with_consent_enabled
FROM program_v1 p
JOIN program_type_v1 pt ON pt.id = p.program_type_id
WHERE pt.key = 'PT_ENTRAINMENT'
  AND p.parental_consent_enabled = true
  AND p.deleted_at IS NULL;

-- 3. Confirm child_max_age
SELECT id, child_max_age
FROM program_v1
WHERE deleted_at IS NULL
  AND program_type_id = (
    SELECT id FROM program_type_v1 WHERE key = 'PT_ENTRAINMENT' AND deleted_at IS NULL
  );
