-- Member approval workflow enhancements
-- ============================================

-- Add approval tracking columns to members table
ALTER TABLE public.members ADD COLUMN IF NOT EXISTS approved_at TIMESTAMPTZ;
ALTER TABLE public.members ADD COLUMN IF NOT EXISTS approved_by UUID REFERENCES auth.users(id);
ALTER TABLE public.members ADD COLUMN IF NOT EXISTS rejected_at TIMESTAMPTZ;
ALTER TABLE public.members ADD COLUMN IF NOT EXISTS rejected_by UUID REFERENCES auth.users(id);
ALTER TABLE public.members ADD COLUMN IF NOT EXISTS rejection_reason TEXT;

-- Add approval email templates
INSERT INTO public.email_templates (template_key, template_name, category, subject, body_html, body_text, is_active)
VALUES
  ('member_approved', 'Member Approved', 'membership', 'Welcome to Monaco USA - Membership Approved!',
   '<h2 style="color: #22c55e; text-align: center;">Membership Approved!</h2>
    <p style="color: #334155; line-height: 1.6;">Dear {{first_name}},</p>
    <p style="color: #334155; line-height: 1.6;">We are pleased to inform you that your membership application to Monaco USA has been <strong>approved</strong>!</p>
    <p style="color: #334155; line-height: 1.6;">Your member ID is: <strong>{{member_id}}</strong></p>
    <p style="color: #334155; line-height: 1.6;">You now have full access to the member portal, including events, documents, and the member directory.</p>
    <div style="text-align: center; margin: 24px 0;">
      <a href="{{site_url}}/dashboard" style="background: #CE1126; color: white; padding: 12px 24px; border-radius: 8px; text-decoration: none; font-weight: bold;">Visit Your Dashboard</a>
    </div>',
   'Dear {{first_name}}, Your membership to Monaco USA has been approved! Your member ID is {{member_id}}.',
   true),
  ('member_rejected', 'Member Rejected', 'membership', 'Monaco USA - Membership Application Update',
   '<h2 style="color: #ef4444; text-align: center;">Application Update</h2>
    <p style="color: #334155; line-height: 1.6;">Dear {{first_name}},</p>
    <p style="color: #334155; line-height: 1.6;">Thank you for your interest in Monaco USA. After careful review, we regret to inform you that your membership application was not approved at this time.</p>
    <p style="color: #334155; line-height: 1.6;">{{reason}}</p>
    <p style="color: #334155; line-height: 1.6;">If you have questions, please contact us at info@monacousa.org.</p>',
   'Dear {{first_name}}, Thank you for your interest in Monaco USA. After review, your membership application was not approved at this time.',
   true)
ON CONFLICT (template_key) DO NOTHING;
