-- Monaco USA Portal 2026 - Onboarding Payment Tracking
-- Track new member payment deadlines for the 30-day payment window

-- ============================================
-- ADD PAYMENT TRACKING COLUMNS TO MEMBERS
-- ============================================

-- Payment deadline for new signups (30 days from onboarding completion)
ALTER TABLE public.members ADD COLUMN IF NOT EXISTS payment_deadline TIMESTAMPTZ;

-- Track when onboarding was completed
ALTER TABLE public.members ADD COLUMN IF NOT EXISTS onboarding_completed_at TIMESTAMPTZ;

-- Index for efficient reminder queries (only index non-null deadlines)
CREATE INDEX IF NOT EXISTS idx_members_payment_deadline ON public.members(payment_deadline)
  WHERE payment_deadline IS NOT NULL;

-- ============================================
-- ADD ONBOARDING REMINDER TYPES TO LOGS TABLE
-- ============================================

-- Update the check constraint on dues_reminder_logs to include onboarding types
ALTER TABLE public.dues_reminder_logs DROP CONSTRAINT IF EXISTS dues_reminder_logs_reminder_type_check;
ALTER TABLE public.dues_reminder_logs ADD CONSTRAINT dues_reminder_logs_reminder_type_check
  CHECK (reminder_type IN (
    'due_soon_30', 'due_soon_7', 'due_soon_1', 'overdue', 'grace_period', 'inactive_notice',
    'onboarding_welcome', 'onboarding_reminder_7', 'onboarding_reminder_1', 'onboarding_expired'
  ));

-- ============================================
-- ADD EMAIL TEMPLATES FOR ONBOARDING REMINDERS
-- ============================================

-- Welcome email with payment instructions (sent immediately after onboarding)
INSERT INTO public.email_templates (template_key, template_name, category, subject, body_html, body_text, is_active, is_system, variables_schema) VALUES
(
  'onboarding_welcome',
  'Welcome - Complete Your Membership',
  'onboarding',
  'Welcome to Monaco USA - Complete Your Membership',
  '<p style="margin: 0 0 16px 0; color: #334155; line-height: 1.6;">Dear {{first_name}},</p>
<p style="margin: 0 0 16px 0; color: #334155; line-height: 1.6;">Welcome to Monaco USA! We''re thrilled to have you join our community of Americans living in and connected to Monaco.</p>
<p style="margin: 0 0 16px 0; color: #334155; line-height: 1.6;">Your account has been created and you now have <strong>30 days</strong> to complete your membership by paying your annual dues.</p>
<div style="background: #f0fdf4; border: 1px solid #bbf7d0; border-radius: 12px; padding: 20px; margin: 0 0 20px 0;">
  <p style="margin: 0 0 8px 0; color: #166534; font-size: 14px; font-weight: 600;">Your Membership Details:</p>
  <p style="margin: 0 0 4px 0; color: #166534; font-size: 14px;"><strong>Member ID:</strong> {{member_id}}</p>
  <p style="margin: 0 0 4px 0; color: #166534; font-size: 14px;"><strong>Annual Dues:</strong> {{amount}}</p>
  <p style="margin: 0 0 4px 0; color: #166534; font-size: 14px;"><strong>Payment Deadline:</strong> {{payment_deadline}}</p>
</div>
<div style="background: #dbeafe; border: 1px solid #93c5fd; border-radius: 12px; padding: 20px; margin: 0 0 20px 0;">
  <p style="margin: 0 0 8px 0; color: #1e40af; font-size: 14px; font-weight: 600;">Bank Transfer Details:</p>
  <p style="margin: 0 0 4px 0; color: #1e3a8a; font-size: 14px;"><strong>Account Holder:</strong> {{account_holder}}</p>
  <p style="margin: 0 0 4px 0; color: #1e3a8a; font-size: 14px;"><strong>Bank:</strong> {{bank_name}}</p>
  <p style="margin: 0 0 4px 0; color: #1e3a8a; font-size: 14px;"><strong>IBAN:</strong> {{iban}}</p>
  <p style="margin: 0 0 4px 0; color: #1e3a8a; font-size: 14px;"><strong>Reference:</strong> {{member_id}}</p>
</div>
<p style="margin: 0 0 16px 0; color: #334155; line-height: 1.6;">In the meantime, explore your new member dashboard and connect with our community:</p>
<p style="margin: 0 0 20px 0; text-align: center;">
  <a href="{{portal_url}}" style="display: inline-block; background: #CE1126; color: white; padding: 12px 24px; border-radius: 8px; text-decoration: none; font-weight: 600;">Go to Dashboard</a>
</p>
<p style="margin: 0; color: #64748b; font-size: 14px; text-align: center;">Questions? Contact us at contact@monacousa.org</p>',
  'Dear {{first_name}},

Welcome to Monaco USA! We''re thrilled to have you join our community of Americans living in and connected to Monaco.

Your account has been created and you now have 30 days to complete your membership by paying your annual dues.

Your Membership Details:
- Member ID: {{member_id}}
- Annual Dues: {{amount}}
- Payment Deadline: {{payment_deadline}}

Bank Transfer Details:
- Account Holder: {{account_holder}}
- Bank: {{bank_name}}
- IBAN: {{iban}}
- Reference: {{member_id}}

Visit your dashboard: {{portal_url}}

Questions? Contact us at contact@monacousa.org',
  true,
  true,
  '{"first_name": "Member first name", "member_id": "Member ID", "amount": "Annual dues amount", "payment_deadline": "Payment deadline date", "account_holder": "Bank account holder", "bank_name": "Bank name", "iban": "IBAN number", "portal_url": "Portal URL"}'
) ON CONFLICT (template_key) DO NOTHING;

-- 7 days left reminder
INSERT INTO public.email_templates (template_key, template_name, category, subject, body_html, body_text, is_active, is_system, variables_schema) VALUES
(
  'onboarding_reminder_7',
  'Onboarding Reminder - 7 Days Left',
  'onboarding',
  '7 Days Left to Complete Your Monaco USA Membership',
  '<p style="margin: 0 0 16px 0; color: #334155; line-height: 1.6;">Dear {{first_name}},</p>
<p style="margin: 0 0 16px 0; color: #334155; line-height: 1.6;">You have <strong style="color: #d97706;">7 days left</strong> to complete your Monaco USA membership by paying your annual dues.</p>
<div style="background: #fef3c7; border: 1px solid #fcd34d; border-radius: 12px; padding: 20px; margin: 0 0 20px 0;">
  <p style="margin: 0 0 8px 0; color: #92400e; font-size: 14px; font-weight: 600;">Payment Due:</p>
  <p style="margin: 0 0 4px 0; color: #78350f; font-size: 14px;"><strong>Amount:</strong> {{amount}}</p>
  <p style="margin: 0 0 4px 0; color: #78350f; font-size: 14px;"><strong>Deadline:</strong> {{payment_deadline}}</p>
</div>
<div style="background: #dbeafe; border: 1px solid #93c5fd; border-radius: 12px; padding: 20px; margin: 0 0 20px 0;">
  <p style="margin: 0 0 8px 0; color: #1e40af; font-size: 14px; font-weight: 600;">Bank Transfer Details:</p>
  <p style="margin: 0 0 4px 0; color: #1e3a8a; font-size: 14px;"><strong>Account Holder:</strong> {{account_holder}}</p>
  <p style="margin: 0 0 4px 0; color: #1e3a8a; font-size: 14px;"><strong>Bank:</strong> {{bank_name}}</p>
  <p style="margin: 0 0 4px 0; color: #1e3a8a; font-size: 14px;"><strong>IBAN:</strong> {{iban}}</p>
  <p style="margin: 0 0 4px 0; color: #1e3a8a; font-size: 14px;"><strong>Reference:</strong> {{member_id}}</p>
</div>
<p style="margin: 0 0 16px 0; color: #334155; line-height: 1.6;">After the deadline, your account will be marked as inactive and you''ll lose access to member features.</p>
<p style="margin: 0 0 20px 0; text-align: center;">
  <a href="{{portal_url}}" style="display: inline-block; background: #CE1126; color: white; padding: 12px 24px; border-radius: 8px; text-decoration: none; font-weight: 600;">View My Account</a>
</p>',
  'Dear {{first_name}},

You have 7 days left to complete your Monaco USA membership by paying your annual dues.

Payment Due:
- Amount: {{amount}}
- Deadline: {{payment_deadline}}

Bank Transfer Details:
- Account Holder: {{account_holder}}
- Bank: {{bank_name}}
- IBAN: {{iban}}
- Reference: {{member_id}}

After the deadline, your account will be marked as inactive.

Visit: {{portal_url}}',
  true,
  true,
  '{"first_name": "Member first name", "member_id": "Member ID", "amount": "Annual dues amount", "payment_deadline": "Payment deadline date", "account_holder": "Bank account holder", "bank_name": "Bank name", "iban": "IBAN number", "portal_url": "Portal URL"}'
) ON CONFLICT (template_key) DO NOTHING;

-- Last day reminder (urgent)
INSERT INTO public.email_templates (template_key, template_name, category, subject, body_html, body_text, is_active, is_system, variables_schema) VALUES
(
  'onboarding_reminder_1',
  'Onboarding Reminder - Last Day',
  'onboarding',
  'URGENT: Last Day to Complete Your Monaco USA Membership',
  '<p style="margin: 0 0 16px 0; color: #334155; line-height: 1.6;">Dear {{first_name}},</p>
<p style="margin: 0 0 16px 0; color: #334155; line-height: 1.6;"><strong style="color: #dc2626;">Today is your last day</strong> to complete your Monaco USA membership payment.</p>
<div style="background: #fef2f2; border: 1px solid #fecaca; border-radius: 12px; padding: 20px; margin: 0 0 20px 0;">
  <p style="margin: 0 0 8px 0; color: #991b1b; font-size: 14px; font-weight: 600;">Payment Required Today:</p>
  <p style="margin: 0 0 4px 0; color: #7f1d1d; font-size: 14px;"><strong>Amount:</strong> {{amount}}</p>
  <p style="margin: 0 0 4px 0; color: #7f1d1d; font-size: 14px;"><strong>Deadline:</strong> {{payment_deadline}}</p>
</div>
<div style="background: #dbeafe; border: 1px solid #93c5fd; border-radius: 12px; padding: 20px; margin: 0 0 20px 0;">
  <p style="margin: 0 0 8px 0; color: #1e40af; font-size: 14px; font-weight: 600;">Bank Transfer Details:</p>
  <p style="margin: 0 0 4px 0; color: #1e3a8a; font-size: 14px;"><strong>Account Holder:</strong> {{account_holder}}</p>
  <p style="margin: 0 0 4px 0; color: #1e3a8a; font-size: 14px;"><strong>Bank:</strong> {{bank_name}}</p>
  <p style="margin: 0 0 4px 0; color: #1e3a8a; font-size: 14px;"><strong>IBAN:</strong> {{iban}}</p>
  <p style="margin: 0 0 4px 0; color: #1e3a8a; font-size: 14px;"><strong>Reference:</strong> {{member_id}}</p>
</div>
<p style="margin: 0 0 16px 0; color: #dc2626; line-height: 1.6; font-weight: 600;">If we don''t receive your payment today, your account will be marked as inactive tomorrow.</p>
<p style="margin: 0 0 20px 0; text-align: center;">
  <a href="{{portal_url}}" style="display: inline-block; background: #dc2626; color: white; padding: 12px 24px; border-radius: 8px; text-decoration: none; font-weight: 600;">Complete Payment Now</a>
</p>',
  'Dear {{first_name}},

TODAY IS YOUR LAST DAY to complete your Monaco USA membership payment.

Payment Required Today:
- Amount: {{amount}}
- Deadline: {{payment_deadline}}

Bank Transfer Details:
- Account Holder: {{account_holder}}
- Bank: {{bank_name}}
- IBAN: {{iban}}
- Reference: {{member_id}}

If we don''t receive your payment today, your account will be marked as inactive tomorrow.

Visit: {{portal_url}}',
  true,
  true,
  '{"first_name": "Member first name", "member_id": "Member ID", "amount": "Annual dues amount", "payment_deadline": "Payment deadline date", "account_holder": "Bank account holder", "bank_name": "Bank name", "iban": "IBAN number", "portal_url": "Portal URL"}'
) ON CONFLICT (template_key) DO NOTHING;

-- Account marked inactive (deadline passed)
INSERT INTO public.email_templates (template_key, template_name, category, subject, body_html, body_text, is_active, is_system, variables_schema) VALUES
(
  'onboarding_expired',
  'Onboarding Expired - Account Inactive',
  'onboarding',
  'Your Monaco USA Account Has Been Marked Inactive',
  '<p style="margin: 0 0 16px 0; color: #334155; line-height: 1.6;">Dear {{first_name}},</p>
<p style="margin: 0 0 16px 0; color: #334155; line-height: 1.6;">Your 30-day payment window has expired and your Monaco USA account has been marked as <strong style="color: #dc2626;">INACTIVE</strong>.</p>
<div style="background: #fef2f2; border: 1px solid #fecaca; border-radius: 12px; padding: 20px; margin: 0 0 20px 0;">
  <p style="margin: 0 0 8px 0; color: #991b1b; font-size: 14px; font-weight: 600;">Account Status:</p>
  <p style="margin: 0 0 4px 0; color: #7f1d1d; font-size: 14px;"><strong>Status:</strong> Inactive</p>
  <p style="margin: 0 0 4px 0; color: #7f1d1d; font-size: 14px;"><strong>Outstanding Amount:</strong> {{amount}}</p>
</div>
<p style="margin: 0 0 16px 0; color: #334155; line-height: 1.6;">As an inactive member, you no longer have access to:</p>
<ul style="margin: 0 0 16px 0; padding-left: 20px; color: #334155;">
  <li>Member-only events</li>
  <li>Member directory</li>
  <li>Member communications</li>
</ul>
<p style="margin: 0 0 16px 0; color: #334155; line-height: 1.6;">To reactivate your membership, please complete your dues payment:</p>
<div style="background: #dbeafe; border: 1px solid #93c5fd; border-radius: 12px; padding: 20px; margin: 0 0 20px 0;">
  <p style="margin: 0 0 8px 0; color: #1e40af; font-size: 14px; font-weight: 600;">Bank Transfer Details:</p>
  <p style="margin: 0 0 4px 0; color: #1e3a8a; font-size: 14px;"><strong>Account Holder:</strong> {{account_holder}}</p>
  <p style="margin: 0 0 4px 0; color: #1e3a8a; font-size: 14px;"><strong>Bank:</strong> {{bank_name}}</p>
  <p style="margin: 0 0 4px 0; color: #1e3a8a; font-size: 14px;"><strong>IBAN:</strong> {{iban}}</p>
  <p style="margin: 0 0 4px 0; color: #1e3a8a; font-size: 14px;"><strong>Reference:</strong> {{member_id}}</p>
</div>
<p style="margin: 0 0 20px 0; text-align: center;">
  <a href="{{portal_url}}" style="display: inline-block; background: #CE1126; color: white; padding: 12px 24px; border-radius: 8px; text-decoration: none; font-weight: 600;">Reactivate My Account</a>
</p>
<p style="margin: 0; color: #64748b; font-size: 14px; text-align: center;">Questions? Contact us at contact@monacousa.org</p>',
  'Dear {{first_name}},

Your 30-day payment window has expired and your Monaco USA account has been marked as INACTIVE.

Account Status:
- Status: Inactive
- Outstanding Amount: {{amount}}

As an inactive member, you no longer have access to member-only events, directory, and communications.

To reactivate your membership, please pay your dues:

Bank Transfer Details:
- Account Holder: {{account_holder}}
- Bank: {{bank_name}}
- IBAN: {{iban}}
- Reference: {{member_id}}

Visit: {{portal_url}}

Questions? Contact us at contact@monacousa.org',
  true,
  true,
  '{"first_name": "Member first name", "member_id": "Member ID", "amount": "Annual dues amount", "account_holder": "Bank account holder", "bank_name": "Bank name", "iban": "IBAN number", "portal_url": "Portal URL"}'
) ON CONFLICT (template_key) DO NOTHING;
