/**
 * TAT Reminder Email Template
 */

import { TATReminderData } from './types';
import { getEmailFooter, getEmailHeader, HeaderStyles, getResponsiveStyles, getEmailContainerStyles } from './helpers';
import { getBrandedHeader } from './branding.config';

/**
 * Get urgency styling based on threshold percentage
 */
function getUrgencyStyle(threshold: number) {
  if (threshold >= 75) {
    return {
      bgColor: '#fff3cd',
      borderColor: '#ffc107',
      textColor: '#856404',
      title: 'Urgent - Action Required'
    };
  } else if (threshold >= 50) {
    return {
      bgColor: '#e7f3ff',
      borderColor: '#0066cc',
      textColor: '#004085',
      title: 'Action Required Soon'
    };
  } else {
    return {
      bgColor: '#e7f3ff',
      borderColor: '#0066cc',
      textColor: '#004085',
      title: 'Early Warning'
    };
  }
}

export function getTATReminderEmail(data: TATReminderData): string {
  const urgencyStyle = getUrgencyStyle(data.thresholdPercentage);
  
  return `
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="format-detection" content="telephone=no">
    <title>TAT Reminder - Action Required</title>
    ${getResponsiveStyles()}
</head>
<body style="margin: 0; padding: 0; font-family: Arial, Helvetica, sans-serif; background-color: #f4f4f4;">
    <table role="presentation" style="width: 100%; border-collapse: collapse; background-color: #f4f4f4;" cellpadding="0" cellspacing="0">
        <tr>
            <td style="padding: 40px 0;">
                <table role="presentation" class="email-container" style="${getEmailContainerStyles()}" cellpadding="0" cellspacing="0">
                    ${getEmailHeader(getBrandedHeader({
                      title: 'TAT Reminder',
                      subtitle: `${data.thresholdPercentage}% Elapsed - ${urgencyStyle.title}`,
                      ...HeaderStyles.warning
                    }))}
                    
                    <tr>
                        <td class="email-content" style="padding: 40px 30px;">
                            <p style="margin: 0 0 20px; color: #333333; font-size: 16px; line-height: 1.6;">
                                Dear <strong style="color: #ff9800;">${data.approverName}</strong>,
                            </p>
                            
                            <p style="margin: 0 0 30px; color: #666666; font-size: 16px; line-height: 1.6;">
                                This is a ${data.urgencyLevel === 'high' ? 'urgent' : 'friendly'} reminder that you have a pending request that requires your attention. <strong>${data.thresholdPercentage}%</strong> of the TAT has elapsed.
                            </p>
                            
                            <table role="presentation" style="width: 100%; border-collapse: collapse; background-color: #fff8e1; border: 1px solid #ffc107; border-radius: 6px; margin-bottom: 30px;" cellpadding="0" cellspacing="0">
                                <tr>
                                    <td style="padding: 25px;">
                                        <h2 style="margin: 0 0 20px; color: #f57c00; font-size: 18px; font-weight: 600;">Request Details</h2>
                                        
                                        <table role="presentation" style="width: 100%; border-collapse: collapse;" cellpadding="0" cellspacing="0">
                                            <tr>
                                                <td style="padding: 8px 0; color: #f57c00; font-size: 14px; width: 140px;">
                                                    <strong>Request ID:</strong>
                                                </td>
                                                <td style="padding: 8px 0; color: #f57c00; font-size: 14px;">
                                                    ${data.requestId}
                                                </td>
                                            </tr>
                                            <tr>
                                                <td style="padding: 8px 0; color: #f57c00; font-size: 14px;">
                                                    <strong>Title:</strong>
                                                </td>
                                                <td style="padding: 8px 0; color: #f57c00; font-size: 14px;">
                                                    ${data.requestTitle || 'N/A'}
                                                </td>
                                            </tr>
                                            <tr>
                                                <td style="padding: 8px 0; color: #f57c00; font-size: 14px;">
                                                    <strong>Initiator:</strong>
                                                </td>
                                                <td style="padding: 8px 0; color: #f57c00; font-size: 14px;">
                                                    ${data.initiatorName}
                                                </td>
                                            </tr>
                                            <tr>
                                                <td style="padding: 8px 0; color: #f57c00; font-size: 14px;">
                                                    <strong>Assigned On:</strong>
                                                </td>
                                                <td style="padding: 8px 0; color: #f57c00; font-size: 14px;">
                                                    ${data.assignedDate}
                                                </td>
                                            </tr>
                                            <tr>
                                                <td style="padding: 8px 0; color: #f57c00; font-size: 14px;">
                                                    <strong>TAT Deadline:</strong>
                                                </td>
                                                <td style="padding: 8px 0; color: #f57c00; font-size: 14px;">
                                                    <strong>${data.tatDeadline}</strong>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td style="padding: 8px 0; color: #f57c00; font-size: 14px;">
                                                    <strong>Time Remaining:</strong>
                                                </td>
                                                <td style="padding: 8px 0; color: #f57c00; font-size: 14px;">
                                                    <strong>${data.timeRemaining}</strong>
                                                </td>
                                            </tr>
                                        </table>
                                    </td>
                                </tr>
                            </table>
                            
                            <!-- Dynamic Urgency Note based on threshold -->
                            <div style="padding: 20px; background-color: ${urgencyStyle.bgColor}; border-left: 4px solid ${urgencyStyle.borderColor}; border-radius: 4px; margin-bottom: 30px;">
                                <h3 style="margin: 0 0 10px; color: ${urgencyStyle.textColor}; font-size: 16px; font-weight: 600;">${urgencyStyle.title}</h3>
                                <p style="margin: 0; color: ${urgencyStyle.textColor}; font-size: 14px; line-height: 1.6;">
                                    ${data.thresholdPercentage}% of the TAT has elapsed. Please review and take action on this request ${data.thresholdPercentage >= 75 ? 'immediately' : 'at your earliest convenience'} to avoid TAT breach. The initiator and other stakeholders are waiting for your decision.
                                </p>
                            </div>
                            
                            <table role="presentation" style="width: 100%; border-collapse: collapse; margin-bottom: 20px;" cellpadding="0" cellspacing="0">
                                <tr>
                                    <td style="text-align: center;">
                                        <a href="${data.viewDetailsLink}" class="cta-button" style="display: inline-block; padding: 15px 40px; background-color: #1a1a1a; color: #ffffff; text-decoration: none; text-align: center; border-radius: 6px; font-size: 16px; font-weight: 600; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2); min-width: 200px;">
                                            View Request Details
                                        </a>
                                    </td>
                                </tr>
                            </table>
                            
                            <p style="margin: 0; color: #666666; font-size: 14px; line-height: 1.6; text-align: center;">
                                Your prompt action is greatly appreciated.
                            </p>
                        </td>
                    </tr>
                    
                    ${getEmailFooter(data.companyName)}
                </table>
            </td>
        </tr>
    </table>
</body>
</html>
  `;
}

