import { getCurrentYear } from "../../../utils/helpers"

interface ProjectSummary {
  projectName: string
  clientName: string
  totalHours: number
  totalCost: number
}

interface EmployeeSummary {
  employeeName: string
  totalHours: number
  totalCost: number
  approvedLeaves: number
}

interface LeaveReport {
  employeeName: string
  totalLeaves: number
}

export const monthlyReportEmail = (
  companyName: string,
  reportMonth: string,
  reportYear: number,
  projectSummaries: ProjectSummary[],
  employeeSummaries: EmployeeSummary[],
  leaveReports: LeaveReport[],
  totalProjectHours: number,
  totalProjectCost: number,
  totalEmployeeHours: number,
  totalEmployeeCost: number,
  totalLeaves: number,
): string => {
  const formatCurrency = (amount: number) =>
    amount.toLocaleString("en-US", {
      minimumFractionDigits: 2,
      maximumFractionDigits: 2,
    })
  const formatHours = (hours: number) => hours.toFixed(1)

  return `
<!DOCTYPE html>
<html lang="en" class="no-js">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="description" content="" />
    <meta name="author" content="" />
    <title>Monthly Report - Costifys</title>
    <style type="text/css" rel="stylesheet">
      body,
      html {
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
        padding: 0;
        margin: 0;
      }
      *,
      ::after,
      ::before {
        box-sizing: border-box;
      }
      body,
      html,
      p {
        font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
          "Helvetica Neue", Helvetica, Arial, sans-serif;
        color: #111111;
        font-size: 16px;
        line-height: 22px;
      }
      body {
        background: #fafafa;
      }
      center {
        width: 600px;
        text-align: center;
        margin: 50px auto;
      }
      .email-top {
        height: 3px;
        background-color: #111111;
        margin-top: 36px;
      }
      .email-bottom {
        margin-bottom: 36px;
        height: 3px;
        background-color: #111111;
      }
      .email-body {
        padding: 50px;
        text-align: left;
        background: #fff;
      }
      h1 {
        margin-top: -20px;
        margin-bottom: 0;
      }
      img.blog-img {
        border-radius: 10px;
        margin-bottom: 15px;
        object-fit: cover;
        max-width: 100%;
      }
      h2 {
        margin-top: 0;
        line-height: 36px;
        font-size: 24px;
        font-weight: 700;
        margin-bottom: 5px;
      }
      h3 {
        margin-top: 25px;
        margin-bottom: 15px;
        font-size: 18px;
        font-weight: 600;
        color: #111111;
      }
      .btn {
        border-radius: 6px;
        background-color: #2f855a;
        color: #fff;
        text-align: center;
        font-size: 16px;
        letter-spacing: 0;
        line-height: 18px;
        padding: 16px 20px;
        display: inline-block;
        min-width: 200px;
      }
      .btn:focus,
      .btn:hover {
        color: #fff;
        opacity: 0.85;
      }
      p {
        margin-top: 0;
      }
      ul {
        list-style: decimal;
        padding-left: 26px;
        margin-top: 0;
        margin-bottom: 20px;
      }
      li {
        padding-left: 10px;
      }
      .f-18 {
        font-size: 18px;
      }
      a {
        text-decoration: none;
        color: #262f37;
      }
      a:hover {
        color: #2f855a;
      }
      .downloads {
        height: 50px;
      }
      .summary-box {
        background-color: #e3f2fd;
        border: 1px solid #2196f3;
        border-left: 4px solid #2196f3;
        padding: 20px;
        border-radius: 6px;
        margin: 25px 0;
      }
      .report-section {
        background-color: #f8f9fa;
        border: 1px solid #dee2e6;
        padding: 20px;
        border-radius: 6px;
        margin: 25px 0;
      }
      .data-table {
        width: 100%;
        border-collapse: collapse;
        margin: 15px 0;
      }
      .data-table th,
      .data-table td {
        border: 1px solid #dee2e6;
        padding: 12px;
        text-align: left;
      }
      .data-table th {
        background-color: #f8f9fa;
        font-weight: 600;
      }
      .data-table tr:nth-child(even) {
        background-color: #f8f9fa;
      }
      .totals-row {
        background-color: #e9ecef !important;
        font-weight: 600;
      }
      @media (max-width: 650px) {
        center {
          width: 430px;
        }
        img.blog-img {
          height: 170px;
        }
        .data-table {
          font-size: 14px;
        }
        .data-table th,
        .data-table td {
          padding: 8px;
        }
      }
      @media (max-width: 420px) {
        center {
          width: 100%;
          padding: 0 15px;
          margin: 30px 0;
        }
        .email-body {
          padding: 30px 20px;
        }
        .downloads {
          height: auto;
        }
        .data-table {
          font-size: 12px;
        }
        .data-table th,
        .data-table td {
          padding: 6px;
        }
      }
    </style>
  </head>
  <body>
    <center>
      <img src="cid:unique@cidLogo" alt="Costify Logo" height="50" />
      <div class="email-top"></div>
      <div class="email-body">
        <p
          style="
            margin-top: 0;
            margin-bottom: 25px;
            font-size: 20px;
            line-height: 22px;
            font-weight: bold;
            color: #111111;
          "
        >
          Monthly Report - ${reportMonth} ${reportYear}
        </p>
        <p style="margin-bottom: 25px">
          Here's your comprehensive monthly report for ${companyName} covering project performance, employee productivity, and leave management for ${reportMonth} ${reportYear}.
        </p>
        
        <div class="summary-box">
          <h3 style="margin-top: 0;">📊 Monthly Overview</h3>
          <p style="margin-bottom: 10px;"><strong>Total Project Hours:</strong> ${formatHours(totalProjectHours)} hours</p>
          <p style="margin-bottom: 10px;"><strong>Total Project Cost:</strong> $${formatCurrency(totalProjectCost)}</p>
          <p style="margin-bottom: 10px;"><strong>Total Employee Hours:</strong> ${formatHours(totalEmployeeHours)} hours</p>
          <p style="margin-bottom: 10px;"><strong>Total Employee Cost:</strong> $${formatCurrency(totalEmployeeCost)}</p>
          <p style="margin-bottom: 0;"><strong>Total Approved Leaves:</strong> ${totalLeaves} days</p>
        </div>

        <div class="report-section">
          <h3>🏗️ Project Summary</h3>
          <p style="margin-bottom: 15px;">Performance breakdown by project:</p>
          <table class="data-table">
            <thead>
              <tr>
                <th>Project Name</th>
                <th>Client</th>
                <th>Hours</th>
                <th>Cost</th>
              </tr>
            </thead>
            <tbody>
              ${projectSummaries
                .map(
                  (project) => `
                <tr>
                  <td>${project.projectName}</td>
                  <td>${project.clientName}</td>
                  <td>${formatHours(project.totalHours)}</td>
                  <td>$${formatCurrency(project.totalCost)}</td>
                </tr>
              `,
                )
                .join("")}
              <tr class="totals-row">
                <td colspan="2"><strong>Total</strong></td>
                <td><strong>${formatHours(totalProjectHours)}</strong></td>
                <td><strong>$${formatCurrency(totalProjectCost)}</strong></td>
              </tr>
            </tbody>
          </table>
        </div>

        <div class="report-section">
          <h3>👥 Employee Summary</h3>
          <p style="margin-bottom: 15px;">Individual employee performance and leave data:</p>
          <table class="data-table">
            <thead>
              <tr>
                <th>Employee Name</th>
                <th>Hours Worked</th>
                <th>Cost</th>
                <th>Approved Leaves</th>
              </tr>
            </thead>
            <tbody>
              ${employeeSummaries
                .map(
                  (employee) => `
                <tr>
                  <td>${employee.employeeName}</td>
                  <td>${formatHours(employee.totalHours)}</td>
                  <td>$${formatCurrency(employee.totalCost)}</td>
                  <td>${employee.approvedLeaves} day${employee.approvedLeaves !== 1 ? "s" : ""}</td>
                </tr>
              `,
                )
                .join("")}
              <tr class="totals-row">
                <td><strong>Total</strong></td>
                <td><strong>${formatHours(totalEmployeeHours)}</strong></td>
                <td><strong>$${formatCurrency(totalEmployeeCost)}</strong></td>
                <td><strong>${totalLeaves}</strong></td>
              </tr>
            </tbody>
          </table>
        </div>

        <div class="report-section">
          <h3>🏖️ Leave Report</h3>
          <p style="margin-bottom: 15px;">Leave utilization by employee:</p>
          <table class="data-table">
            <thead>
              <tr>
                <th>Employee Name</th>
                <th>Total Leaves Taken</th>
              </tr>
            </thead>
            <tbody>
              ${leaveReports
                .map(
                  (leave) => `
                <tr>
                  <td>${leave.employeeName}</td>
                  <td>${leave.totalLeaves} day${leave.totalLeaves !== 1 ? "s" : ""}</td>
                </tr>
              `,
                )
                .join("")}
              <tr class="totals-row">
                <td><strong>Total</strong></td>
                <td><strong>${totalLeaves} days</strong></td>
              </tr>
            </tbody>
          </table>
        </div>

        <p style="margin-bottom: 25px">
          This report provides insights into your company's productivity and resource utilization. Use this data to make informed decisions about project management, resource allocation, and workforce planning.
        </p>

        <p style="margin-bottom: 25px">
          For detailed analysis or custom reports, please log into your Costifys dashboard or contact our support team.
        </p>

        <div>
          <p style="margin-bottom: 0px">Regards,</p>
          <p style="margin-bottom: 0px">Support Team</p>
          <p style="margin-bottom: 0px">Costifys App</p>
          <p style="margin-bottom: 0px">
            <a href="https://www.costifys.com/">https://www.costifys.com/</a>
          </p>
        </div>
      </div>
      <div class="email-bottom"></div>
      <div class="email-footer">
        <img src="cid:unique@cidLogo" alt="Costify Logo" height="50" />
        <p style="line-height: 18px">${getCurrentYear()} Costifys | All Rights Reserved.</p>
      </div>
    </center>
  </body>
</html>
    `
}
