<?php
require_once($_SERVER['DOCUMENT_ROOT'] . "/include/vendor/autoload.php");

use PHPMailer\PHPMailer\PHPMailer;

require_once($_SERVER['DOCUMENT_ROOT'] . "/include/Email/smtp.php");

class emailMessage
{
    // ─── Shared base template ─────────────────────────────────────────────────────
    private function baseTemplate($title, $preheader, $content, $APP_NAME, $APP_URL, $SITE_ADDRESS)
    {
        return "<!doctype html>
<html lang='en'>
<head>
    <meta charset='UTF-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <title>$title</title>
    <style>
        body, table, td, a { -webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100%; }
        table, td { mso-table-lspace: 0pt; mso-table-rspace: 0pt; }
        img { border: 0; height: auto; line-height: 100%; outline: none; text-decoration: none; }
        body { margin: 0 !important; padding: 0 !important; background-color: #f0f4f8; }
        .email-wrapper { background-color: #f0f4f8; padding: 40px 20px; }
        .email-container { max-width: 580px; margin: 0 auto; }
        .email-header { text-align: center; padding: 0 0 24px 0; }
        .email-header h1 { margin: 0; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; font-size: 22px; font-weight: 700; color: #1a202c; letter-spacing: -0.5px; }
        .email-body { background-color: #ffffff; border-radius: 12px; overflow: hidden; box-shadow: 0 1px 3px rgba(0,0,0,0.08); }
        .email-top-bar { height: 4px; background: linear-gradient(90deg, #3b82f6, #6366f1); }
        .email-content { padding: 40px 40px 32px; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; }
        .greeting { font-size: 20px; font-weight: 600; color: #1a202c; margin: 0 0 16px; }
        .body-text { font-size: 15px; color: #4a5568; line-height: 1.7; margin: 0 0 20px; }
        .detail-card { background-color: #f7fafc; border: 1px solid #e2e8f0; border-radius: 8px; padding: 20px 24px; margin: 24px 0; }
        .alert-box { border-left: 4px solid #f59e0b; background-color: #fffbeb; border-radius: 0 8px 8px 0; padding: 14px 16px; margin: 20px 0; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; font-size: 14px; color: #92400e; }
        .alert-box-blue { border-left: 4px solid #3b82f6; background-color: #eff6ff; border-radius: 0 8px 8px 0; padding: 14px 16px; margin: 20px 0; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; font-size: 14px; color: #1e40af; }
        .alert-box-green { border-left: 4px solid #10b981; background-color: #ecfdf5; border-radius: 0 8px 8px 0; padding: 14px 16px; margin: 20px 0; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; font-size: 14px; color: #065f46; }
        .divider { border: none; border-top: 1px solid #e2e8f0; margin: 28px 0; }
        .sign-off { font-size: 14px; color: #718096; line-height: 1.6; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; }
        .email-footer { text-align: center; padding: 24px 20px; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; }
        .footer-text { font-size: 12px; color: #a0aec0; line-height: 1.6; margin: 0 0 8px; }
        .footer-link { color: #718096; text-decoration: none; }
        @media only screen and (max-width: 600px) {
            .email-content { padding: 28px 24px 24px !important; }
        }
    </style>
</head>
<body>
<span style='display:none;font-size:1px;color:#f0f4f8;max-height:0;'>$preheader</span>
<div class='email-wrapper'>
    <div class='email-container'>

        <!-- Header -->
        <div class='email-header'>
            <h1>$APP_NAME</h1>
        </div>

        <!-- Body -->
        <div class='email-body'>
            <div class='email-top-bar'></div>
            <div class='email-content'>
                $content
                <hr class='divider'>
                <p class='sign-off'>Warm regards,<br><strong>$APP_NAME Team</strong></p>
            </div>
        </div>

        <!-- Footer -->
        <div class='email-footer'>
            <p class='footer-text'>$APP_NAME &bull; $SITE_ADDRESS</p>
            <p class='footer-text'>
                &copy; " . date('Y') . " <a href='$APP_URL' class='footer-link'>$APP_NAME</a>. All rights reserved.
            </p>
        </div>

    </div>
</div>
</body>
</html>";
    }

    // ─── Helper: detail row ───────────────────────────────────────────────────────
    private function detailRow($label, $value)
    {
        return "<table role='presentation' width='100%' cellpadding='0' cellspacing='0' style='margin:6px 0;'>
            <tr>
                <td style='font-family:-apple-system,BlinkMacSystemFont,\"Segoe UI\",Roboto,Helvetica,Arial,sans-serif;font-size:14px;color:#718096;font-weight:500;padding:6px 0;border-bottom:1px solid #edf2f7;width:50%;'>$label</td>
                <td style='font-family:-apple-system,BlinkMacSystemFont,\"Segoe UI\",Roboto,Helvetica,Arial,sans-serif;font-size:14px;color:#1a202c;font-weight:600;padding:6px 0;border-bottom:1px solid #edf2f7;text-align:right;'>$value</td>
            </tr>
        </table>";
    }

    // ─── Helper: amount display ───────────────────────────────────────────────────
    private function amountDisplay($label, $amount, $color = '#1a202c')
    {
        return "<div style='text-align:center;margin:24px 0;'>
            <div style='font-family:-apple-system,BlinkMacSystemFont,\"Segoe UI\",Roboto,Helvetica,Arial,sans-serif;font-size:13px;color:#718096;margin-bottom:6px;text-transform:uppercase;letter-spacing:1px;'>$label</div>
            <div style='font-family:-apple-system,BlinkMacSystemFont,\"Segoe UI\",Roboto,Helvetica,Arial,sans-serif;font-size:32px;font-weight:700;color:$color;'>\$$amount</div>
        </div>";
    }

    // ─── 1. Admin Funding Notification ───────────────────────────────────────────
    public function AdminFundingMsg($full_name, $amount, $user_balance, $user_acctno, $trans_status, $trans_type, $APP_NAME, $APP_URL, $SITE_ADDRESS)
    {
        $formattedAmount  = number_format((float)$amount, 2);
        $formattedBalance = number_format((float)$user_balance, 2);

        $content = "
            <p class='greeting'>Hello, $full_name</p>
            <p class='body-text'>Your account has been credited. Here are the details of the transaction:</p>

            " . $this->amountDisplay('Amount Credited', $formattedAmount, '#10b981') . "

            <div class='detail-card'>
                " . $this->detailRow('Account Number', $user_acctno) . "
                " . $this->detailRow('Transaction Type', ucwords($trans_type)) . "
                " . $this->detailRow('Status', ucwords($trans_status)) . "
                " . $this->detailRow('Updated Balance', "\$$formattedBalance") . "
            </div>

            <div class='alert-box-green'>
                Your account balance has been updated to reflect this transaction.
            </div>

            <p class='body-text'>If you did not expect this credit, please contact our support team immediately.</p>
        ";

        return $this->baseTemplate("Account Funded - $APP_NAME", "Your account has been credited with \$$formattedAmount.", $content, $APP_NAME, $APP_URL, $SITE_ADDRESS);
    }

    // ─── 2. Admin Withdrawal Notification ────────────────────────────────────────
    public function AdminWithdrawalMsg($full_name, $amount, $user_balance, $user_acctno, $trans_status, $trans_type, $APP_NAME, $APP_URL, $SITE_ADDRESS)
    {
        $formattedAmount  = number_format((float)$amount, 2);
        $formattedBalance = number_format((float)$user_balance, 2);

        $content = "
            <p class='greeting'>Hello, $full_name</p>
            <p class='body-text'>A withdrawal has been processed on your account. Here are the details:</p>

            " . $this->amountDisplay('Amount Withdrawn', $formattedAmount, '#e53e3e') . "

            <div class='detail-card'>
                " . $this->detailRow('Account Number', $user_acctno) . "
                " . $this->detailRow('Transaction Type', ucwords($trans_type)) . "
                " . $this->detailRow('Status', ucwords($trans_status)) . "
                " . $this->detailRow('Updated Balance', "\$$formattedBalance") . "
            </div>

            <div class='alert-box'>
                If you did not authorize this withdrawal, please contact our support team right away.
            </div>
        ";

        return $this->baseTemplate("Withdrawal Notification - $APP_NAME", "A withdrawal of \$$formattedAmount was processed on your account.", $content, $APP_NAME, $APP_URL, $SITE_ADDRESS);
    }

    // ─── 3. Admin Register Welcome ────────────────────────────────────────────────
   public function AdminRegisterMsg($full_name, $acct_no, $acct_status, $APP_NAME, $APP_URL, $SITE_ADDRESS)
{
    if (strtolower($acct_status) == 'hold') {

        $content = "
            <p class='greeting'>Hello, $full_name</p>
            <p class='body-text'>Your <strong>$APP_NAME</strong> is currently on hold. Please review the details below:</p>

            <div class='detail-card'>
                " . $this->detailRow('Account Number', $acct_no) . "
                " . $this->detailRow('Account Status', 'On Hold') . "
            </div>

            <div class='alert-box'>
                <strong>Account Restricted.</strong> Your account is currently on hold and certain features may be unavailable. Please contact our support team to resolve this and regain full access.
            </div>

            <p class='body-text'>If you believe this is an error or need assistance, please reach out to our support team at <a href='$APP_URL' style='color:#3b82f6;text-decoration:none;font-weight:600;'>$APP_URL</a>.</p>
        ";

        return $this->baseTemplate("Account On Hold - $APP_NAME", "Your account is currently restricted.", $content, $APP_NAME, $APP_URL, $SITE_ADDRESS);

    } else {

        $content = "
            <p class='greeting'>Welcome, $full_name!</p>
            <p class='body-text'>Welcome<strong>$APP_NAME</strong>. Your account is active and is ready to use.</p>

            <div class='detail-card'>
                " . $this->detailRow('Account Number', $acct_no) . "
                " . $this->detailRow('Account Status', 'Active') . "
            </div>

            <div class='alert-box-blue'>
                Keep your account number safe and never share your login credentials with anyone.
            </div>

            <p class='body-text'>If you need any assistance, our support team is always here to help. Visit us at <a href='$APP_URL' style='color:#3b82f6;text-decoration:none;font-weight:600;'>$APP_URL</a>.</p>
        ";

        return $this->baseTemplate("Welcome to $APP_NAME", "Your $APP_NAME account is ready.", $content, $APP_NAME, $APP_URL, $SITE_ADDRESS);
    }
}

    // ─── 4. Admin Wire Transfer Notification ─────────────────────────────────────
    public function AdminWireMsg($full_name, $amount, $user_balance, $trans_status, $trans_type, $APP_NAME, $APP_URL, $SITE_ADDRESS)
    {
        $formattedAmount  = number_format((float)$amount, 2);
        $formattedBalance = number_format((float)$user_balance, 2);

        $content = "
            <p class='greeting'>Hello, $full_name</p>
            <p class='body-text'>Your wire transfer has been submitted and is currently being processed. Here are the details:</p>

            " . $this->amountDisplay('Transfer Amount', $formattedAmount) . "

            <div class='detail-card'>
                " . $this->detailRow('Transaction Type', ucwords($trans_type)) . "
                " . $this->detailRow('Status', ucwords($trans_status)) . "
                " . $this->detailRow('Account Balance', "\$$formattedBalance") . "
            </div>

            <div class='alert-box-blue'>
                Wire transfers may take 1–3 business days to complete depending on the receiving bank.
            </div>

            <p class='body-text'>If you did not authorize this transaction, please contact our support team immediately.</p>
        ";

        return $this->baseTemplate("Wire Transfer Notification - $APP_NAME", "Your wire transfer of \$$formattedAmount is $trans_status.", $content, $APP_NAME, $APP_URL, $SITE_ADDRESS);
    }

    // ─── 5. Admin Domestic Transfer Notification ──────────────────────────────────
    public function AdminDomMsg($full_name, $amount, $user_balance, $trans_status, $trans_type, $APP_NAME, $APP_URL, $SITE_ADDRESS)
    {
        $formattedAmount  = number_format((float)$amount, 2);
        $formattedBalance = number_format((float)$user_balance, 2);

        $content = "
            <p class='greeting'>Hello, $full_name</p>
            <p class='body-text'>Your domestic transfer has been submitted. Here is a summary of the transaction:</p>

            " . $this->amountDisplay('Transfer Amount', $formattedAmount) . "

            <div class='detail-card'>
                " . $this->detailRow('Transaction Type', ucwords($trans_type)) . "
                " . $this->detailRow('Status', ucwords($trans_status)) . "
                " . $this->detailRow('Account Balance', "\$$formattedBalance") . "
            </div>

            <p class='body-text'>If you did not authorize this transaction, please contact our support team immediately.</p>
        ";

        return $this->baseTemplate("Domestic Transfer Notification - $APP_NAME", "Your domestic transfer of \$$formattedAmount is $trans_status.", $content, $APP_NAME, $APP_URL, $SITE_ADDRESS);
    }

    // ─── 6. Admin Interbank Transfer Notification ─────────────────────────────────
    public function AdminInterMsg($full_name, $amount, $user_balance, $trans_status, $trans_type, $APP_NAME, $APP_URL, $SITE_ADDRESS)
    {
        $formattedAmount  = number_format((float)$amount, 2);
        $formattedBalance = number_format((float)$user_balance, 2);

        $content = "
            <p class='greeting'>Hello, $full_name</p>
            <p class='body-text'>Your interbank transfer has been submitted. Here is a summary of the transaction:</p>

            " . $this->amountDisplay('Transfer Amount', $formattedAmount) . "

            <div class='detail-card'>
                " . $this->detailRow('Transaction Type', ucwords($trans_type)) . "
                " . $this->detailRow('Status', ucwords($trans_status)) . "
                " . $this->detailRow('Account Balance', "\$$formattedBalance") . "
            </div>

            <p class='body-text'>If you did not authorize this transaction, please contact our support team immediately.</p>
        ";

        return $this->baseTemplate("Interbank Transfer Notification - $APP_NAME", "Your interbank transfer of \$$formattedAmount is $trans_status.", $content, $APP_NAME, $APP_URL, $SITE_ADDRESS);
    }

    // ─── 7. Admin Stock Interest Notification ────────────────────────────────────
    public function AdminStockMsg($full_name, $amount, $user_balance, $trans_status, $trans_type, $APP_NAME, $APP_URL, $SITE_ADDRESS)
    {
        $formattedAmount  = number_format((float)$amount, 2);
        $formattedBalance = number_format((float)$user_balance, 2);

        $content = "
            <p class='greeting'>Hello, $full_name</p>
            <p class='body-text'>Your stock interest has been processed. Here are the details:</p>

            " . $this->amountDisplay('Interest Amount', $formattedAmount, '#10b981') . "

            <div class='detail-card'>
                " . $this->detailRow('Transaction Type', ucwords($trans_type)) . "
                " . $this->detailRow('Status', ucwords($trans_status)) . "
                " . $this->detailRow('Updated Balance', "\$$formattedBalance") . "
            </div>

            <div class='alert-box-green'>
                Your account balance has been updated to reflect this stock interest.
            </div>

            <p class='body-text'>If you have any questions about this transaction, please contact our support team.</p>
        ";

        return $this->baseTemplate("Stock Interest Notification - $APP_NAME", "Your stock interest of \$$formattedAmount is $trans_status.", $content, $APP_NAME, $APP_URL, $SITE_ADDRESS);
    }

    // ─── 8. Admin Loan Notification ──────────────────────────────────────────────
    public function AdminLoanMsg($full_name, $amount, $loan_balance, $user_acctno, $trans_status, $APP_NAME, $APP_URL, $SITE_ADDRESS)
    {
        $formattedAmount  = number_format((float)$amount, 2);
        $formattedLoan    = number_format((float)$loan_balance, 2);

        $content = "
            <p class='greeting'>Hello, $full_name</p>
            <p class='body-text'>There is an update on your loan application. Here are the details:</p>

            " . $this->amountDisplay('Loan Amount', $formattedAmount, '#6366f1') . "

            <div class='detail-card'>
                " . $this->detailRow('Account Number', $user_acctno) . "
                " . $this->detailRow('Status', ucwords($trans_status)) . "
                " . $this->detailRow('Loan Balance', "\$$formattedLoan") . "
            </div>

            <div class='alert-box-blue'>
                Our team will be in touch with further details regarding your loan application.
            </div>

            <p class='body-text'>If you have any questions, please do not hesitate to contact our support team.</p>
        ";

        return $this->baseTemplate("Loan Notification - $APP_NAME", "Your loan application of \$$formattedAmount is $trans_status.", $content, $APP_NAME, $APP_URL, $SITE_ADDRESS);
    }
}