<?php

namespace Database\Seeders;

use App\Models\EmailTemplate;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;

/**
 * EmailTemplateSeeder
 *
 * Seeds four default HTML email templates for the SSHIA platform.
 * These templates cover the most critical transactional and automated
 * email communications sent to members, applicants, and trainees.
 * All templates are marked as system templates (is_system = true)
 * to prevent accidental deletion by non-Super Manager users.
 *
 * Templates use {placeholder} syntax for dynamic variable substitution.
 * HTML content is designed to render correctly in major email clients
 * and is compatible with the barryvdh/laravel-dompdf PDF renderer.
 *
 * Uses updateOrCreate keyed on slug for idempotency — safe to re-run.
 */
class EmailTemplateSeeder extends Seeder
{
    public function run(): void
    {
        $templates = [
            [
                'name'         => 'Welcome Email',
                'slug'         => 'welcome-email',
                'subject'      => 'Welcome to the South Sudan Health Informatics Association',
                'preheader'    => 'Your SSHIA membership application has been received.',
                'html_content' => $this->welcomeEmailHtml(),
                'text_content' => "Dear {name},\n\nThank you for applying to join the South Sudan Health Informatics Association (SSHIA).\n\nYour application has been received and is currently under review. You will be notified of the outcome within 5 working days.\n\nApplication Reference: {reference}\nMembership Category: {category}\n\nIf you have any questions, please contact us at info@sshia.org or visit www.sshia.org.\n\nWarm regards,\nThe SSHIA Membership Team\nSouth Sudan Health Informatics Association\nSchool of Public Health, University of Juba\nP.O BOX 82, Juba, South Sudan",
                'description'  => 'Sent automatically to applicants immediately after submitting a membership application. Confirms receipt and provides application reference details.',
                'is_active'    => true,
                'is_system'    => true,
                'placeholders' => ['{name}', '{reference}', '{category}', '{application_date}'],
            ],
            [
                'name'         => 'Membership Approved',
                'slug'         => 'membership-approved',
                'subject'      => 'Congratulations — Your SSHIA Membership Has Been Approved',
                'preheader'    => 'Welcome to the SSHIA professional community.',
                'html_content' => $this->membershipApprovedHtml(),
                'text_content' => "Dear {name},\n\nWe are delighted to inform you that your application for membership of the South Sudan Health Informatics Association (SSHIA) has been approved.\n\nMember ID: {member_id}\nMembership Category: {category}\nApproval Date: {approval_date}\n\nYou can now log in to your member portal at sshia.org to access all SSHIA programmes, resources, and services.\n\nWelcome to the SSHIA professional community.\n\nWarm regards,\nThe SSHIA Membership Team\ninfo@sshia.org | www.sshia.org",
                'description'  => 'Sent to applicants when their membership application is approved. Includes Member ID, category, and portal access instructions.',
                'is_active'    => true,
                'is_system'    => true,
                'placeholders' => ['{name}', '{member_id}', '{category}', '{approval_date}', '{portal_url}'],
            ],
            [
                'name'         => 'Training Registration Confirmation',
                'slug'         => 'training-registration-confirmation',
                'subject'      => 'Training Registration Confirmed — {session_name}',
                'preheader'    => 'Your registration for {session_name} has been confirmed.',
                'html_content' => $this->trainingRegistrationHtml(),
                'text_content' => "Dear {name},\n\nYour registration for the following SSHIA training session has been confirmed:\n\nSession: {session_name}\nDate: {session_date}\nTime: {session_time}\nVenue: {venue}\nRegistration Reference: {reference}\n\nPlease bring this confirmation to the training venue. If you have any questions, contact info@sshia.org.\n\nWe look forward to seeing you.\n\nWarm regards,\nThe SSHIA Training Team\ninfo@sshia.org | www.sshia.org",
                'description'  => 'Sent to trainees upon successful registration for a training session. Includes session details, date, time, venue, and registration reference.',
                'is_active'    => true,
                'is_system'    => true,
                'placeholders' => ['{name}', '{session_name}', '{session_date}', '{session_time}', '{venue}', '{reference}'],
            ],
            [
                'name'         => 'Password Reset',
                'slug'         => 'password-reset',
                'subject'      => 'Reset Your SSHIA Account Password',
                'preheader'    => 'A password reset was requested for your SSHIA account.',
                'html_content' => $this->passwordResetHtml(),
                'text_content' => "Dear {name},\n\nA password reset was requested for your SSHIA account ({email}).\n\nClick the link below to reset your password. This link is valid for {expiry_minutes} minutes:\n\n{reset_url}\n\nIf you did not request a password reset, please ignore this email. Your password will not be changed.\n\nFor security concerns, contact info@sshia.org immediately.\n\nWarm regards,\nThe SSHIA Security Team\ninfo@sshia.org | www.sshia.org",
                'description'  => 'Sent to users who request a password reset. Contains a time-limited secure reset link.',
                'is_active'    => true,
                'is_system'    => true,
                'placeholders' => ['{name}', '{email}', '{reset_url}', '{expiry_minutes}'],
            ],
        ];

        foreach ($templates as $data) {
            EmailTemplate::updateOrCreate(
                ['slug' => $data['slug']],
                $data
            );
        }

        $this->command->info('EmailTemplateSeeder: 4 email templates seeded successfully.');
    }

    private function welcomeEmailHtml(): string
    {
        return <<<HTML
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Welcome to SSHIA</title></head>
<body style="margin:0;padding:0;background-color:#f3f4f6;font-family:Arial,Helvetica,sans-serif;">
<table width="100%" cellpadding="0" cellspacing="0" style="background-color:#f3f4f6;padding:40px 20px;">
  <tr><td align="center">
    <table width="600" cellpadding="0" cellspacing="0" style="background-color:#ffffff;border-radius:8px;overflow:hidden;box-shadow:0 2px 8px rgba(0,0,0,0.08);">
      <tr><td style="background-color:#16a34a;padding:32px 40px;text-align:center;">
        <h1 style="color:#ffffff;margin:0;font-size:24px;font-weight:700;">South Sudan Health Informatics Association</h1>
        <p style="color:#bbf7d0;margin:8px 0 0;font-size:14px;">Advancing Health Informatics in South Sudan</p>
      </td></tr>
      <tr><td style="padding:40px;">
        <h2 style="color:#111827;font-size:20px;margin:0 0 16px;">Welcome, {name}!</h2>
        <p style="color:#374151;font-size:15px;line-height:1.6;margin:0 0 16px;">Thank you for applying to join the <strong>South Sudan Health Informatics Association (SSHIA)</strong>. We are pleased to confirm that your membership application has been received and is currently under review.</p>
        <table width="100%" cellpadding="0" cellspacing="0" style="background-color:#f0fdf4;border:1px solid #bbf7d0;border-radius:6px;padding:20px;margin:24px 0;">
          <tr><td>
            <p style="margin:0 0 8px;color:#374151;font-size:14px;"><strong>Application Reference:</strong> {reference}</p>
            <p style="margin:0 0 8px;color:#374151;font-size:14px;"><strong>Membership Category:</strong> {category}</p>
            <p style="margin:0;color:#374151;font-size:14px;"><strong>Application Date:</strong> {application_date}</p>
          </td></tr>
        </table>
        <p style="color:#374151;font-size:15px;line-height:1.6;margin:0 0 16px;">Our membership team will review your application and notify you of the outcome within <strong>5 working days</strong>. If additional information is required, we will contact you at this email address.</p>
        <p style="color:#374151;font-size:15px;line-height:1.6;margin:0 0 24px;">For any queries, please contact us at <a href="mailto:info@sshia.org" style="color:#16a34a;">info@sshia.org</a> or visit <a href="https://www.sshia.org" style="color:#16a34a;">www.sshia.org</a>.</p>
        <p style="color:#374151;font-size:15px;margin:0;">Warm regards,<br><strong>The SSHIA Membership Team</strong></p>
      </td></tr>
      <tr><td style="background-color:#f9fafb;padding:24px 40px;text-align:center;border-top:1px solid #e5e7eb;">
        <p style="color:#6b7280;font-size:12px;margin:0;">South Sudan Health Informatics Association<br>School of Public Health, University of Juba, P.O BOX 82, Juba, South Sudan<br><a href="mailto:info@sshia.org" style="color:#16a34a;">info@sshia.org</a> | <a href="https://www.sshia.org" style="color:#16a34a;">www.sshia.org</a></p>
        <p style="color:#9ca3af;font-size:11px;margin:12px 0 0;">&copy; {current_year} South Sudan Health Informatics Association. All rights reserved.</p>
      </td></tr>
    </table>
  </td></tr>
</table>
</body>
</html>
HTML;
    }

    private function membershipApprovedHtml(): string
    {
        return <<<HTML
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Membership Approved — SSHIA</title></head>
<body style="margin:0;padding:0;background-color:#f3f4f6;font-family:Arial,Helvetica,sans-serif;">
<table width="100%" cellpadding="0" cellspacing="0" style="background-color:#f3f4f6;padding:40px 20px;">
  <tr><td align="center">
    <table width="600" cellpadding="0" cellspacing="0" style="background-color:#ffffff;border-radius:8px;overflow:hidden;box-shadow:0 2px 8px rgba(0,0,0,0.08);">
      <tr><td style="background-color:#16a34a;padding:32px 40px;text-align:center;">
        <h1 style="color:#ffffff;margin:0;font-size:24px;font-weight:700;">South Sudan Health Informatics Association</h1>
        <p style="color:#bbf7d0;margin:8px 0 0;font-size:14px;">Advancing Health Informatics in South Sudan</p>
      </td></tr>
      <tr><td style="padding:40px;">
        <div style="text-align:center;margin-bottom:32px;">
          <div style="display:inline-block;background-color:#f0fdf4;border:2px solid #16a34a;border-radius:50%;width:64px;height:64px;line-height:64px;font-size:32px;text-align:center;">&#10003;</div>
        </div>
        <h2 style="color:#111827;font-size:22px;margin:0 0 16px;text-align:center;">Congratulations, {name}!</h2>
        <p style="color:#374151;font-size:15px;line-height:1.6;margin:0 0 16px;text-align:center;">We are delighted to inform you that your application for membership of the <strong>South Sudan Health Informatics Association (SSHIA)</strong> has been <strong style="color:#16a34a;">approved</strong>.</p>
        <table width="100%" cellpadding="0" cellspacing="0" style="background-color:#f0fdf4;border:1px solid #bbf7d0;border-radius:6px;padding:20px;margin:24px 0;">
          <tr><td>
            <p style="margin:0 0 8px;color:#374151;font-size:14px;"><strong>Member ID:</strong> {member_id}</p>
            <p style="margin:0 0 8px;color:#374151;font-size:14px;"><strong>Membership Category:</strong> {category}</p>
            <p style="margin:0;color:#374151;font-size:14px;"><strong>Approval Date:</strong> {approval_date}</p>
          </td></tr>
        </table>
        <p style="color:#374151;font-size:15px;line-height:1.6;margin:0 0 24px;">You can now log in to your <strong>SSHIA Member Portal</strong> to access training programmes, mentorship opportunities, research publications, and all other member benefits.</p>
        <div style="text-align:center;margin:32px 0;">
          <a href="{portal_url}" style="background-color:#16a34a;color:#ffffff;padding:14px 32px;border-radius:6px;text-decoration:none;font-size:15px;font-weight:600;display:inline-block;">Access Your Member Portal</a>
        </div>
        <p style="color:#374151;font-size:15px;margin:0;">Welcome to the SSHIA professional community.<br><br>Warm regards,<br><strong>The SSHIA Membership Team</strong></p>
      </td></tr>
      <tr><td style="background-color:#f9fafb;padding:24px 40px;text-align:center;border-top:1px solid #e5e7eb;">
        <p style="color:#6b7280;font-size:12px;margin:0;">South Sudan Health Informatics Association<br>School of Public Health, University of Juba, P.O BOX 82, Juba, South Sudan<br><a href="mailto:info@sshia.org" style="color:#16a34a;">info@sshia.org</a> | <a href="https://www.sshia.org" style="color:#16a34a;">www.sshia.org</a></p>
        <p style="color:#9ca3af;font-size:11px;margin:12px 0 0;">&copy; {current_year} South Sudan Health Informatics Association. All rights reserved.</p>
      </td></tr>
    </table>
  </td></tr>
</table>
</body>
</html>
HTML;
    }

    private function trainingRegistrationHtml(): string
    {
        return <<<HTML
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Training Registration Confirmed — SSHIA</title></head>
<body style="margin:0;padding:0;background-color:#f3f4f6;font-family:Arial,Helvetica,sans-serif;">
<table width="100%" cellpadding="0" cellspacing="0" style="background-color:#f3f4f6;padding:40px 20px;">
  <tr><td align="center">
    <table width="600" cellpadding="0" cellspacing="0" style="background-color:#ffffff;border-radius:8px;overflow:hidden;box-shadow:0 2px 8px rgba(0,0,0,0.08);">
      <tr><td style="background-color:#2563eb;padding:32px 40px;text-align:center;">
        <h1 style="color:#ffffff;margin:0;font-size:24px;font-weight:700;">South Sudan Health Informatics Association</h1>
        <p style="color:#bfdbfe;margin:8px 0 0;font-size:14px;">Training &amp; Capacity Building</p>
      </td></tr>
      <tr><td style="padding:40px;">
        <h2 style="color:#111827;font-size:20px;margin:0 0 16px;">Registration Confirmed, {name}!</h2>
        <p style="color:#374151;font-size:15px;line-height:1.6;margin:0 0 24px;">Your registration for the following SSHIA training session has been confirmed. Please retain this email as your registration confirmation.</p>
        <table width="100%" cellpadding="0" cellspacing="0" style="background-color:#eff6ff;border:1px solid #bfdbfe;border-radius:6px;padding:20px;margin:0 0 24px;">
          <tr><td>
            <p style="margin:0 0 10px;color:#1e40af;font-size:16px;font-weight:700;">{session_name}</p>
            <p style="margin:0 0 8px;color:#374151;font-size:14px;"><strong>Date:</strong> {session_date}</p>
            <p style="margin:0 0 8px;color:#374151;font-size:14px;"><strong>Time:</strong> {session_time}</p>
            <p style="margin:0 0 8px;color:#374151;font-size:14px;"><strong>Venue:</strong> {venue}</p>
            <p style="margin:0;color:#374151;font-size:14px;"><strong>Registration Reference:</strong> {reference}</p>
          </td></tr>
        </table>
        <p style="color:#374151;font-size:15px;line-height:1.6;margin:0 0 16px;">Please bring this confirmation email (printed or digital) to the training venue. Attendance will be recorded against your registration reference.</p>
        <p style="color:#374151;font-size:15px;line-height:1.6;margin:0 0 24px;">For any queries regarding this training session, please contact us at <a href="mailto:info@sshia.org" style="color:#2563eb;">info@sshia.org</a>.</p>
        <p style="color:#374151;font-size:15px;margin:0;">We look forward to seeing you.<br><br>Warm regards,<br><strong>The SSHIA Training Team</strong></p>
      </td></tr>
      <tr><td style="background-color:#f9fafb;padding:24px 40px;text-align:center;border-top:1px solid #e5e7eb;">
        <p style="color:#6b7280;font-size:12px;margin:0;">South Sudan Health Informatics Association<br>School of Public Health, University of Juba, P.O BOX 82, Juba, South Sudan<br><a href="mailto:info@sshia.org" style="color:#2563eb;">info@sshia.org</a> | <a href="https://www.sshia.org" style="color:#2563eb;">www.sshia.org</a></p>
        <p style="color:#9ca3af;font-size:11px;margin:12px 0 0;">&copy; {current_year} South Sudan Health Informatics Association. All rights reserved.</p>
      </td></tr>
    </table>
  </td></tr>
</table>
</body>
</html>
HTML;
    }

    private function passwordResetHtml(): string
    {
        return <<<HTML
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Reset Your SSHIA Password</title></head>
<body style="margin:0;padding:0;background-color:#f3f4f6;font-family:Arial,Helvetica,sans-serif;">
<table width="100%" cellpadding="0" cellspacing="0" style="background-color:#f3f4f6;padding:40px 20px;">
  <tr><td align="center">
    <table width="600" cellpadding="0" cellspacing="0" style="background-color:#ffffff;border-radius:8px;overflow:hidden;box-shadow:0 2px 8px rgba(0,0,0,0.08);">
      <tr><td style="background-color:#374151;padding:32px 40px;text-align:center;">
        <h1 style="color:#ffffff;margin:0;font-size:24px;font-weight:700;">South Sudan Health Informatics Association</h1>
        <p style="color:#d1d5db;margin:8px 0 0;font-size:14px;">Account Security</p>
      </td></tr>
      <tr><td style="padding:40px;">
        <h2 style="color:#111827;font-size:20px;margin:0 0 16px;">Password Reset Request</h2>
        <p style="color:#374151;font-size:15px;line-height:1.6;margin:0 0 16px;">Dear {name},</p>
        <p style="color:#374151;font-size:15px;line-height:1.6;margin:0 0 24px;">A password reset was requested for your SSHIA account (<strong>{email}</strong>). Click the button below to set a new password. This link is valid for <strong>{expiry_minutes} minutes</strong>.</p>
        <div style="text-align:center;margin:32px 0;">
          <a href="{reset_url}" style="background-color:#374151;color:#ffffff;padding:14px 32px;border-radius:6px;text-decoration:none;font-size:15px;font-weight:600;display:inline-block;">Reset My Password</a>
        </div>
        <p style="color:#374151;font-size:15px;line-height:1.6;margin:0 0 16px;">If the button above does not work, copy and paste the following link into your browser:</p>
        <p style="color:#6b7280;font-size:13px;word-break:break-all;background-color:#f9fafb;padding:12px;border-radius:4px;margin:0 0 24px;">{reset_url}</p>
        <div style="background-color:#fef3c7;border:1px solid #fcd34d;border-radius:6px;padding:16px;margin:0 0 24px;">
          <p style="color:#92400e;font-size:14px;margin:0;"><strong>Security Notice:</strong> If you did not request a password reset, please ignore this email. Your password will not be changed. If you believe your account has been compromised, contact <a href="mailto:info@sshia.org" style="color:#92400e;">info@sshia.org</a> immediately.</p>
        </div>
        <p style="color:#374151;font-size:15px;margin:0;">Warm regards,<br><strong>The SSHIA Security Team</strong></p>
      </td></tr>
      <tr><td style="background-color:#f9fafb;padding:24px 40px;text-align:center;border-top:1px solid #e5e7eb;">
        <p style="color:#6b7280;font-size:12px;margin:0;">South Sudan Health Informatics Association<br>School of Public Health, University of Juba, P.O BOX 82, Juba, South Sudan<br><a href="mailto:info@sshia.org" style="color:#374151;">info@sshia.org</a> | <a href="https://www.sshia.org" style="color:#374151;">www.sshia.org</a></p>
        <p style="color:#9ca3af;font-size:11px;margin:12px 0 0;">&copy; {current_year} South Sudan Health Informatics Association. All rights reserved.</p>
      </td></tr>
    </table>
  </td></tr>
</table>
</body>
</html>
HTML;
    }
}
