<?php
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    header("Content-Type: application/json");

    include('config.php');

    date_default_timezone_set("Asia/Kolkata");

    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;
    require '../vendor/autoload.php';

    $requestData = json_decode(file_get_contents("php://input"), true);

    $teamId = $requestData['team_id'] ?? '';
    $ccList = $requestData['cc']      ?? [];
    $email  = $requestData['email']   ?? '';

    $filePath   = "https://www.procorporateleague.com/2026/pcl/assets/mail_image/";
    $imgVersion = time();

    /* ── PCL OVERALL STATS ── */
    $statscurl = curl_init();
    curl_setopt_array($statscurl, [
        CURLOPT_URL            => 'https://procorporateleague.com/pcl_rest/api/v1/pcl/stats',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING       => '',
        CURLOPT_MAXREDIRS      => 10,
        CURLOPT_TIMEOUT        => 10,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST  => 'GET'
    ]);
    $statsResponse = curl_exec($statscurl);
    curl_close($statscurl);
    $statsJson = json_decode($statsResponse, true);
    $statsArrayData = [
        'total_matches_played'  => $statsJson['results'][0]['Total_Matches_Played']  ?? 0,
        'total_runs_Scored'     => $statsJson['results'][0]['Total_Runs_Scored']     ?? 0,
        'total_wickets_Claimed' => $statsJson['results'][0]['Total_Wickets_Claimed'] ?? 0,
        'total_hundreds'        => $statsJson['results'][0]['Total_Hundreds']        ?? 0,
        'total_fifties'         => $statsJson['results'][0]['Total_Fifties']         ?? 0,
    ];

    /* ── TEAM AI PROFILE ── */
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL            => "https://procorporateleague.com/pcl_rest/api/v12/teams/{$teamId}/ai-profile",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING       => '',
        CURLOPT_MAXREDIRS      => 10,
        CURLOPT_TIMEOUT        => 8,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST  => 'GET',
    ));
    $response = curl_exec($curl);
    curl_close($curl);

    $teamData = json_decode($response, true);

    $profile = [];
    if (!empty($teamData) && !empty($teamData['data'])) {
        $profile = $teamData['data'][0] ?? [];
    }

    $LogoUrl = 'https://procorporateleague.com/wp-includes/images/team/' . rawurlencode((string)($profile['team_id'] ?? '')) . '.png';

    /* ── OLD TEAM MAP DATA ── */
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL            => 'https://www.procorporateleague.com/pcl_rest/api/v9/old-team-map-data/getAll',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING       => '',
        CURLOPT_MAXREDIRS      => 10,
        CURLOPT_TIMEOUT        => 8,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST  => 'GET',
    ));
    $response = curl_exec($curl);
    curl_close($curl);

    $teamInfo     = json_decode($response, true);
    $teamInfoData = [];
    if (!empty($teamInfo) && !empty($teamInfo['results']) && $teamInfo['code'] === 200) {
        foreach ($teamInfo['results'] as $key => $values) {
            if ($teamId == $values['team_id']) {
                $teamInfoData = $values;
                break;
            }
        }
    }

    // ── email: pehle requestData se, fallback teamInfoData se ──
    if (empty($email)) {
        $email = $teamInfoData['email'] ?? '';
    }
    $name = $profile['team_name'] ?? '';

    /* ── ALL CITIES ── */
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL            => 'https://www.procorporateleague.com/pcl_rest/api/v9/corporate/getAllCities',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING       => '',
        CURLOPT_MAXREDIRS      => 10,
        CURLOPT_TIMEOUT        => 8,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST  => 'GET',
    ));
    $response = curl_exec($curl);
    curl_close($curl);

    $getData  = json_decode($response, true);
    $cityData = [];
    if (!empty($getData) && !empty($getData['results']) && $getData['code'] === 200) {
        $cityData = $getData['results'] ?? [];
    }

    /* ── ALL SECTORS ── */
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL            => 'https://www.procorporateleague.com/pcl_rest/api/v9/corporate/getAllSectors',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING       => '',
        CURLOPT_MAXREDIRS      => 10,
        CURLOPT_TIMEOUT        => 8,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST  => 'GET',
    ));
    $response = curl_exec($curl);
    curl_close($curl);

    $getData    = json_decode($response, true);
    $sectorData = [];
    if (!empty($getData) && !empty($getData['results']) && $getData['code'] === 200) {
        $sectorData = $getData['results'] ?? [];
    }

    /* ── MATCH CITY & SECTOR ── */
    $singleCity   = [];
    $singleSector = [];

    if (!empty($teamInfoData) && is_array($teamInfoData)) {
        foreach ($cityData as $cityVal) {
            if (($teamInfoData['city'] ?? '') == $cityVal['id']) {
                $singleCity = $cityVal;
                break;
            }
        }

        foreach ($sectorData as $sectorVal) {
            if (($teamInfoData['sector'] ?? '') == $sectorVal['id']) {
                $singleSector = $sectorVal;
                break;
            }
        }
    }

    /* ── SMTP CREDENTIALS ── */
    $smtpSql = "SELECT * FROM `smtp_auth_data` WHERE id = 1";
    $smtpExe = mysqli_query($conn, $smtpSql);
    $smtpRow = mysqli_fetch_assoc($smtpExe);
    if (empty($smtpRow)) {
        echo json_encode(["status_code" => 400, "status" => "ERROR", "message" => "SMTP AUTH MISSING"]);
        die;
    }

    /* ===============================
       CXO-LEVEL DYNAMIC EMAIL TEMPLATE
       Corporate World Cup™ Invitation
    ================================ */

    $teamName   = trim((string)($profile['team_name'] ?? $teamInfoData['team_name'] ?? 'Your Team'));
    $cityName   = trim((string)($singleCity['city_name'] ?? $teamInfoData['city_name'] ?? 'Your City'));
    $sectorName = trim((string)($singleSector['name'] ?? $singleSector['sector_name'] ?? $teamInfoData['sector_name'] ?? 'Your Sector'));
    $safeTeam   = htmlspecialchars($teamName, ENT_QUOTES, 'UTF-8');
    $safeCity   = htmlspecialchars($cityName, ENT_QUOTES, 'UTF-8');
    $safeSector = htmlspecialchars($sectorName, ENT_QUOTES, 'UTF-8');

    $teamDashboardUrl = 'https://www.procorporateleague.com/ai/corporate-cricket-team-ai-analysis/pcl-ai-corporate-team-profile.php?team_id=' . rawurlencode((string)$teamId) . '&team_name=' . rawurlencode($teamName);
    $reserveUrl       = 'https://www.procorporateleague.com/pcl/team-register/';
    $cwcUrl           = 'https://corporateworldcup.com/';
    $pclUrl           = 'https://www.procorporateleague.com/';

    $trophyImage      = 'https://wplspeedqueen.com/email_api/cwctrophy.png';
    $firechatImage    = 'https://wplspeedqueen.com/email_api/firechat.png';
    $merchandiseImage = 'https://wplspeedqueen.com/email_api/cwcmerchandise.jpeg';
    $pclLogo          = 'https://www.procorporateleague.com/2026/pcl/Imp_PCL_Logo_Black.png';
    $sonyLivLogo      = 'https://www.procorporateleague.com/2026/pcl/SonyLIV_2020.png';
    $cwcHeroImage     = 'https://www.roadtocorporateworldcup.com/assets/corporateworldcup.jpeg';
    $celebrityImage   = 'https://www.roadtocorporateworldcup.com/assets/PCLxCelebrity_Engagement.jpeg';
    $chrisGayleImage  = 'https://www.procorporateleague.com/uploads/sliders/mobile/images/20260603085928_Chris_Gayle_X_PCL__Slider_for_Mobile_.jpg';
    $championsVideo   = 'https://procorporateleague.com/assets/highlights/2025_Corporate_Champions.mp4';
    $sonyLivCoverageUrl = 'https://www.procorporateleague.com/pcl/media/pcl-eleven11-sony-liv-highlight/';

    $teamLogoRaw = trim((string)(
        $LogoUrl
        ?? $profile['team_logo']
        ?? $profile['logo']
        ?? $profile['company_logo']
        ?? $teamInfoData['team_logo']
        ?? $teamInfoData['logo']
        ?? $teamInfoData['company_logo']
        ?? ''
    ));

    if ($teamLogoRaw !== '' && !preg_match('/^https?:\/\//i', $teamLogoRaw)) {
        $teamLogoRaw = 'https://www.procorporateleague.com/2026/pcl/assets/team_logo/' . ltrim($teamLogoRaw, '/');
    }

    $teamLogoUrl = $teamLogoRaw !== '' ? $teamLogoRaw : $pclLogo;
    $teamLogoAlt = $safeTeam . ' Logo';

    ob_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Corporate World Cup™ Invitation | <?= $safeTeam ?></title>
</head>

<body style="margin:0;padding:0;background:#edf1f7;font-family:Arial,Helvetica,sans-serif;color:#17233c;">

<table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="background:#edf1f7;">
<tr>
<td align="center" style="padding:22px 10px;">

<table role="presentation" width="900" cellpadding="0" cellspacing="0" border="0" style="width:900px;max-width:900px;background:#ffffff;border-radius:20px;overflow:hidden;border:1px solid #d8dfeb;">

<!-- TOP BRAND BAR -->
<tr>
<td style="background:#071d46;padding:18px 26px;border-bottom:3px solid #d6ad45;">
  <table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0">
    <tr>
      <td valign="middle">
        <table role="presentation" cellpadding="0" cellspacing="0" border="0">
          <tr>
            <td>
              <div style="font-family:Georgia,'Times New Roman',serif;font-size:24px;line-height:1.2;color:#ffffff;letter-spacing:.3px;">Corporate World Cup™</div>
              <div style="font-size:11px;line-height:1.5;color:#d6ad45;letter-spacing:1.8px;text-transform:uppercase;margin-top:3px;">Qualification Round Invitation</div>
            </td>
          </tr>
        </table>
      </td>
      <td align="right" valign="middle" style="font-size:12px;color:#ffffff;line-height:1.6;">
        <?= $safeCity ?> Qualification Round<br>
        <?= $safeSector ?> Category
      </td>
    </tr>
  </table>
</td>
</tr>


<!-- CORPORATE WORLD CUP HERO BANNER -->
<tr>
<td style="background:#071d46;padding:0;">
  <table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" background="<?= $cwcHeroImage ?>" style="background-image:url('<?= $cwcHeroImage ?>');background-size:cover;background-position:center center;background-repeat:no-repeat;">
    <tr>
      <td style="padding:34px 28px 150px 28px;">
        <table role="presentation" cellpadding="0" cellspacing="0" border="0" style="background:rgba(7,29,70,.86);border:1px solid rgba(214,173,69,.55);border-radius:18px;">
          <tr>
            <td style="padding:16px 18px 16px 18px;">
              <img src="<?= $teamLogoUrl ?>" width="145" alt="<?= $teamLogoAlt ?>" style="display:block;width:auto;max-width:145px;max-height:62px;height:auto;border:0;object-fit:contain;margin-bottom:14px;background:#ffffff;border-radius:10px;padding:8px;">
              <div style="font-size:11px;line-height:1.6;color:#d6ad45;letter-spacing:2px;text-transform:uppercase;">Out of 500+ Corporate Teams Evaluated</div>
              <div style="font-family:Georgia,'Times New Roman',serif;font-size:30px;line-height:1.18;color:#ffffff;font-weight:700;margin-top:6px;"><?= $safeTeam ?></div>
              <div style="font-size:14px;line-height:1.6;color:#eef4ff;margin-top:8px;">Corporate World Cup™ Qualification Round</div>
            </td>
          </tr>
        </table>
      </td>
    </tr>
  </table>
</td>
</tr>

<!-- HERO -->
<tr>
<td style="background:#071d46;padding:34px 30px 36px 30px;">
  <table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0">
    <tr>
      <td width="58%" valign="middle" style="padding-right:24px;">
        <div style="font-size:12px;line-height:1.6;color:#d6ad45;letter-spacing:2px;text-transform:uppercase;margin-bottom:12px;">Out of 500+ Corporate Teams Evaluated</div>

        <h1 style="margin:0;font-family:Georgia,'Times New Roman',serif;font-size:44px;line-height:1.08;color:#ffffff;font-weight:700;">
          <?= $safeTeam ?> Has Been Shortlisted
        </h1>

        <div style="margin-top:16px;font-size:18px;line-height:1.6;color:#eef4ff;">
          For the inaugural Corporate World Cup™ Qualification Round.
        </div>

        <div style="margin-top:22px;padding:14px 16px;background:rgba(255,255,255,.08);border:1px solid rgba(214,173,69,.45);border-radius:14px;color:#ffffff;font-size:14px;line-height:1.7;">
          Only 8 corporate teams per city. Only 1 team per sector. Top 2 city teams qualify for the Corporate World Cup™ National Finals ecosystem.
        </div>

        <table role="presentation" cellpadding="0" cellspacing="0" border="0" style="margin-top:24px;">
          <tr>
            <td style="padding-right:10px;">
              <a href="<?= $teamDashboardUrl ?>" style="display:inline-block;background:#d6ad45;color:#071d46;text-decoration:none;font-size:13px;letter-spacing:.6px;text-transform:uppercase;padding:13px 20px;border-radius:999px;">View Team Dashboard</a>
            </td>
            <td>
              <a href="<?= $reserveUrl ?>" style="display:inline-block;border:1px solid #d6ad45;color:#ffffff;text-decoration:none;font-size:13px;letter-spacing:.6px;text-transform:uppercase;padding:12px 20px;border-radius:999px;">Reserve Category</a>
            </td>
          </tr>
        </table>
      </td>

      <td width="42%" valign="middle" align="center">
        <div style="background:#ffffff;border-radius:18px;padding:18px 18px 20px 18px;border:1px solid rgba(214,173,69,.55);">
          <table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="margin-bottom:14px;">
            <tr>
              <td align="left" valign="middle" style="width:50%;">
                <img src="<?= $teamLogoUrl ?>" width="120" alt="<?= $teamLogoAlt ?>" style="display:block;width:auto;max-width:120px;max-height:50px;height:auto;border:0;object-fit:contain;">
              </td>
              <td align="right" valign="middle" style="width:50%;">
                <img src="<?= $sonyLivLogo ?>" width="82" alt="Sony LIV" style="display:block;width:auto;max-width:82px;max-height:40px;height:auto;border:0;object-fit:contain;">
              </td>
            </tr>
          </table>
          <img src="<?= $trophyImage ?>" width="300" alt="Corporate World Cup Trophy" style="display:block;width:100%;max-width:300px;height:auto;border:0;margin:0 auto;">
          <div style="font-size:12px;line-height:1.6;color:#071d46;margin-top:10px;">
            Only 2 teams from <?= $safeCity ?> will qualify for the National Finals ecosystem.
          </div>
        </div>
      </td>
    </tr>
  </table>
</td>
</tr>

<!-- STATUS CARDS -->
<tr>
<td style="padding:24px 28px 4px 28px;background:#ffffff;">
  <table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0">
    <tr>
      <td width="25%" style="padding:6px;">
        <div style="border:1px solid #e2e7f0;border-radius:15px;padding:16px 12px;min-height:92px;">
          <div style="font-family:Georgia,'Times New Roman',serif;font-size:27px;color:#071d46;line-height:1.1;">500+</div>
          <div style="font-size:12px;line-height:1.5;color:#5d6880;margin-top:7px;">Corporate teams evaluated</div>
        </div>
      </td>
      <td width="25%" style="padding:6px;">
        <div style="border:1px solid #e2e7f0;border-radius:15px;padding:16px 12px;min-height:92px;">
          <div style="font-family:Georgia,'Times New Roman',serif;font-size:20px;color:#071d46;line-height:1.2;"><?= $safeTeam ?></div>
          <div style="font-size:12px;line-height:1.5;color:#5d6880;margin-top:7px;">Shortlisted for qualification</div>
        </div>
      </td>
      <td width="25%" style="padding:6px;">
        <div style="border:1px solid #e2e7f0;border-radius:15px;padding:16px 12px;min-height:92px;">
          <div style="font-family:Georgia,'Times New Roman',serif;font-size:20px;color:#071d46;line-height:1.2;">Category Protection</div>
          <div style="font-size:12px;line-height:1.5;color:#5d6880;margin-top:7px;">Only one <?= $safeSector ?> team per city</div>
        </div>
      </td>
      <td width="25%" style="padding:6px;">
        <div style="border:1px solid #e2e7f0;border-radius:15px;padding:16px 12px;min-height:92px;">
          <div style="font-family:Georgia,'Times New Roman',serif;font-size:20px;color:#071d46;line-height:1.2;">Reservation Open</div>
          <div style="font-size:12px;line-height:1.5;color:#5d6880;margin-top:7px;">Before public category allocation</div>
        </div>
      </td>
    </tr>
  </table>
</td>
</tr>

<!-- LETTER COPY -->
<tr>
<td style="padding:22px 36px 8px 36px;background:#ffffff;">
  <div style="font-size:15px;line-height:1.8;color:#24304a;">
    <p style="margin:0 0 14px 0;">Dear Team <?= $safeTeam ?>,</p>

    <p style="margin:0 0 14px 0;">Greetings from Pro Corporate League.</p>

    <p style="margin:0 0 14px 0;">We are pleased to inform you that <?= $safeTeam ?> has been shortlisted for participation in the Corporate World Cup™ Qualification Round.</p>

    <p style="margin:0 0 14px 0;">Out of more than 500 corporate teams evaluated through the Pro Corporate League ecosystem, <?= $safeTeam ?> has been identified as one of the organizations eligible to participate in the inaugural qualification pathway leading to the Corporate World Cup™.</p>

    <p style="margin:0 0 14px 0;">This invitation is not based on registration order. It is extended to organizations that have demonstrated a combination of corporate participation, team continuity, ecosystem contribution and long-term engagement with corporate cricket.</p>

    <p style="margin:0 0 14px 0;">For the first time, Corporate India will have a structured pathway to its own World Cup.</p>
  </div>
</td>
</tr>

<!-- WHY THIS MATTERS -->
<tr>
<td style="padding:12px 28px;background:#ffffff;">
  <table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border:1px solid #e2e7f0;border-radius:18px;overflow:hidden;">
    <tr>
      <td style="background:#f7f9fc;padding:20px 24px;">
        <h2 style="margin:0;font-family:Georgia,'Times New Roman',serif;font-size:26px;line-height:1.2;color:#071d46;font-weight:700;">Why This Matters</h2>
      </td>
    </tr>
    <tr>
      <td style="padding:22px 24px;font-size:15px;line-height:1.8;color:#24304a;">
        The Corporate World Cup™ has been created to provide organizations with a platform that combines employee engagement, leadership participation, employer branding and competitive sport within a professionally managed national framework.
      </td>
    </tr>
  </table>
</td>
</tr>

<!-- PARTICIPATION INCLUDES -->
<tr>
<td style="padding:12px 28px;background:#ffffff;">
  <table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0">
    <tr>
      <td width="58%" valign="top" style="padding-right:14px;">
        <div style="border:1px solid #e2e7f0;border-radius:18px;padding:22px 24px;">
          <h2 style="margin:0 0 16px 0;font-family:Georgia,'Times New Roman',serif;font-size:25px;line-height:1.2;color:#071d46;font-weight:700;">Participation Includes</h2>

          <table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="font-size:14px;line-height:1.8;color:#24304a;">
            <tr><td style="padding:5px 0;">• 18 matches across three formats</td></tr>
            <tr><td style="padding:5px 0;">• Team merchandise for 30 players</td></tr>
            <tr><td style="padding:5px 0;">• Live match coverage and highlights</td></tr>
            <tr><td style="padding:5px 0;">• AI-powered team and player rankings</td></tr>
            <tr><td style="padding:5px 0;">• Corporate storytelling and employer branding opportunities</td></tr>
            <tr><td style="padding:5px 0;">• Eligibility to qualify for the Corporate World Cup™ National Finals ecosystem</td></tr>
          </table>
        </div>
      </td>

      <td width="42%" valign="top">
        <div style="border:1px solid #e2e7f0;border-radius:18px;overflow:hidden;background:#f7f9fc;">
          <img src="<?= $merchandiseImage ?>" width="340" alt="Corporate World Cup Team Merchandise" style="display:block;width:100%;height:auto;border:0;">
          <div style="padding:16px 18px;font-size:13px;line-height:1.7;color:#24304a;">
            30-player team merchandise package included as part of the qualification journey.
          </div>
        </div>
      </td>
    </tr>
  </table>
</td>
</tr>

<!-- FIRECHAT IMAGE BLOCK -->
<tr>
<td style="padding:12px 28px;background:#ffffff;">
  <table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="background:#071d46;border-radius:18px;overflow:hidden;">
    <tr>
      <td width="48%" valign="middle" style="padding:24px;">
        <div style="font-size:12px;line-height:1.6;color:#d6ad45;letter-spacing:2px;text-transform:uppercase;">Invitation Included</div>
        <h2 style="margin:8px 0 10px 0;font-family:Georgia,'Times New Roman',serif;font-size:28px;line-height:1.2;color:#ffffff;font-weight:700;">Corporate World Cup™ Firechat</h2>
        <div style="font-size:15px;line-height:1.8;color:#eef4ff;">
          Featuring Sanath Jayasuriya and Anjum Chopra on 25 July 2026.
        </div>
      </td>
      <td width="52%" valign="middle" align="right">
        <img src="<?= $firechatImage ?>" width="460" alt="Corporate World Cup Firechat" style="display:block;width:100%;max-width:460px;height:auto;border:0;">
      </td>
    </tr>
  </table>
</td>
</tr>

<!-- EXCLUSIVITY + QUALIFICATION -->
<tr>
<td style="padding:12px 28px;background:#ffffff;">
  <table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0">
    <tr>
      <td width="50%" valign="top" style="padding-right:7px;">
        <div style="border:1px solid #e2e7f0;border-radius:18px;padding:22px 24px;min-height:220px;">
          <h2 style="margin:0 0 14px 0;font-family:Georgia,'Times New Roman',serif;font-size:24px;line-height:1.2;color:#071d46;font-weight:700;">Category Exclusivity</h2>
          <div style="font-size:14px;line-height:1.8;color:#24304a;">
            As part of the Corporate World Cup™ structure, only one corporate team per sector is permitted in each city. This ensures category exclusivity throughout the qualification journey.
            <br><br>
            Once a sector is allocated, it is closed for the remainder of the qualification cycle.
          </div>
        </div>
      </td>
      <td width="50%" valign="top" style="padding-left:7px;">
        <div style="border:1px solid #e2e7f0;border-radius:18px;padding:22px 24px;min-height:220px;">
          <h2 style="margin:0 0 14px 0;font-family:Georgia,'Times New Roman',serif;font-size:24px;line-height:1.2;color:#071d46;font-weight:700;">Qualification Structure</h2>
          <div style="font-size:14px;line-height:1.9;color:#24304a;">
            • Only 8 corporate teams per city<br>
            • Only 1 corporate team per sector<br>
            • Top 2 city teams qualify for the Corporate World Cup™ National Finals ecosystem<br>
            • City champions receive complimentary entry into the National Finals structure
          </div>
        </div>
      </td>
    </tr>
  </table>
</td>
</tr>


<!-- CELEBRITY TRUST + PCL AUTHENTICITY -->
<tr>
<td style="padding:12px 28px;background:#ffffff;">
  <table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border:1px solid #e2e7f0;border-radius:18px;overflow:hidden;background:#071d46;">
    <tr>
      <td style="padding:24px 24px 12px 24px;">
        <div style="font-size:12px;line-height:1.6;color:#d6ad45;letter-spacing:2px;text-transform:uppercase;">Celebrity Trust & Corporate Credibility</div>
        <h2 style="margin:8px 0 10px 0;font-family:Georgia,'Times New Roman',serif;font-size:28px;line-height:1.2;color:#ffffff;font-weight:700;">15 Years Of Building Corporate Teams Beyond The Workplace</h2>
        <div style="font-size:14px;line-height:1.8;color:#eaf1ff;">
          Pro Corporate League has engaged 500+ corporate teams and worked alongside global cricket personalities to create one of India's most visible corporate cricket ecosystems.
        </div>
      </td>
    </tr>
    <tr>
      <td style="padding:10px 24px 0 24px;">
        <img src="<?= $celebrityImage ?>" width="852" alt="PCL Celebrity Engagement" style="display:block;width:100%;max-width:852px;height:auto;border:0;border-radius:14px;">
      </td>
    </tr>
    <tr>
      <td style="padding:14px 24px 24px 24px;font-size:13px;line-height:1.7;color:#dfe8ff;">
        A credibility-led ecosystem supported by cricket legends, corporate leaders and national-level corporate sporting stories.
      </td>
    </tr>
  </table>
</td>
</tr>

<!-- CHRIS GAYLE AUTHENTICITY + CHAMPIONS VIDEO -->
<tr>
<td style="padding:12px 28px;background:#ffffff;">
  <table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border:1px solid #d6ad45;border-radius:18px;overflow:hidden;background:#fff8e4;">
    <tr>
      <td width="58%" valign="middle" style="padding:24px;">
        <div style="font-size:12px;line-height:1.6;color:#a17300;letter-spacing:2px;text-transform:uppercase;">PCL Season 11 Closing Ceremony</div>
        <h2 style="margin:8px 0 10px 0;font-family:Georgia,'Times New Roman',serif;font-size:26px;line-height:1.25;color:#071d46;font-weight:700;">Chris Gayle Presence Added Authenticity To The Corporate Champions Celebration</h2>
        <div style="font-size:14px;line-height:1.8;color:#24304a;">
          Chris Gayle was present during the PCL Season 11 closing ceremony, recognising corporate champions and strengthening the credibility of the Pro Corporate League ecosystem.
        </div>
      </td>
      <td width="42%" valign="middle" align="center" style="padding:18px;">
        <a href="<?= $championsVideo ?>" style="text-decoration:none;">
          <img src="<?= $chrisGayleImage ?>" width="330" alt="Chris Gayle at PCL Season 11 Closing Ceremony" style="display:block;width:100%;max-width:330px;height:auto;border:0;border-radius:14px;">
          <div style="display:inline-block;margin-top:12px;background:#071d46;color:#ffffff;font-size:12px;letter-spacing:.6px;text-transform:uppercase;padding:10px 16px;border-radius:999px;">Watch Champions Highlights</div>
        </a>
      </td>
    </tr>
  </table>
</td>
</tr>

<!-- SONY LIV AUTHENTICITY -->
<tr>
<td style="padding:12px 28px;background:#ffffff;">
  <table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border:1px solid #e2e7f0;border-radius:18px;overflow:hidden;background:#ffffff;">
    <tr>
      <td width="28%" valign="middle" align="center" style="padding:22px;background:#f7f9fc;">
        <img src="<?= $sonyLivLogo ?>" width="120" alt="Sony LIV" style="display:block;width:120px;max-width:120px;height:auto;border:0;margin:0 auto;">
      </td>
      <td width="72%" valign="middle" style="padding:22px;">
        <div style="font-size:12px;line-height:1.6;color:#a17300;letter-spacing:2px;text-transform:uppercase;">Broadcast Visibility</div>
        <h2 style="margin:8px 0 10px 0;font-family:Georgia,'Times New Roman',serif;font-size:25px;line-height:1.25;color:#071d46;font-weight:700;">PCL On Sony LIV</h2>
        <div style="font-size:14px;line-height:1.8;color:#24304a;">
          Pro Corporate League's Eleven 11 highlight property has been showcased through the Sony LIV ecosystem, adding broadcast credibility and visibility to participating corporate teams.
        </div>
        <div style="margin-top:14px;">
          <a href="<?= $sonyLivCoverageUrl ?>" style="display:inline-block;background:#071d46;color:#ffffff;text-decoration:none;font-size:12px;letter-spacing:.6px;text-transform:uppercase;padding:11px 17px;border-radius:999px;">View Sony LIV Coverage</a>
        </div>
      </td>
    </tr>
  </table>
</td>
</tr>

<!-- COMMERCIAL STRUCTURE -->
<tr>
<td style="padding:12px 28px;background:#ffffff;">
  <table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border:1px solid #d6ad45;border-radius:18px;overflow:hidden;">
    <tr>
      <td style="background:#fff8e4;padding:20px 24px;">
        <h2 style="margin:0;font-family:Georgia,'Times New Roman',serif;font-size:26px;line-height:1.2;color:#071d46;font-weight:700;">Commercial Structure</h2>
      </td>
    </tr>
    <tr>
      <td style="padding:20px 18px 22px 18px;background:#ffffff;">
        <table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0">
          <tr>
            <td width="25%" style="padding:6px;">
              <div style="background:#f7f9fc;border:1px solid #e2e7f0;border-radius:14px;padding:16px 10px;text-align:center;">
                <div style="font-size:12px;color:#5d6880;line-height:1.5;">Advance Category Reservation</div>
                <div style="font-family:Georgia,'Times New Roman',serif;font-size:24px;color:#071d46;line-height:1.2;margin-top:7px;">₹2,00,000</div>
                <div style="font-size:11px;color:#5d6880;margin-top:4px;">+ GST</div>
              </div>
            </td>
            <td width="25%" style="padding:6px;">
              <div style="background:#f7f9fc;border:1px solid #e2e7f0;border-radius:14px;padding:16px 10px;text-align:center;">
                <div style="font-size:12px;color:#5d6880;line-height:1.5;">Before 31 Dec 2026</div>
                <div style="font-family:Georgia,'Times New Roman',serif;font-size:24px;color:#071d46;line-height:1.2;margin-top:7px;">₹2,00,000</div>
                <div style="font-size:11px;color:#5d6880;margin-top:4px;">+ GST</div>
              </div>
            </td>
            <td width="25%" style="padding:6px;">
              <div style="background:#f7f9fc;border:1px solid #e2e7f0;border-radius:14px;padding:16px 10px;text-align:center;">
                <div style="font-size:12px;color:#5d6880;line-height:1.5;">Before 31 Mar 2027</div>
                <div style="font-family:Georgia,'Times New Roman',serif;font-size:24px;color:#071d46;line-height:1.2;margin-top:7px;">₹1,00,000</div>
                <div style="font-size:11px;color:#5d6880;margin-top:4px;">+ GST</div>
              </div>
            </td>
            <td width="25%" style="padding:6px;">
              <div style="background:#071d46;border:1px solid #071d46;border-radius:14px;padding:16px 10px;text-align:center;">
                <div style="font-size:12px;color:#d6ad45;line-height:1.5;">Total Participation Fee</div>
                <div style="font-family:Georgia,'Times New Roman',serif;font-size:24px;color:#ffffff;line-height:1.2;margin-top:7px;">₹5,00,000</div>
                <div style="font-size:11px;color:#d6ad45;margin-top:4px;">+ GST</div>
              </div>
            </td>
          </tr>
        </table>
      </td>
    </tr>
  </table>
</td>
</tr>


<!-- PCL LEGACY COMPACT -->
<tr>
<td style="padding:12px 28px;background:#ffffff;">
  <table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="background:#071d46;border-radius:18px;overflow:hidden;">
    <tr>
      <td style="padding:24px 24px 10px 24px;">
        <div style="font-size:12px;line-height:1.6;color:#d6ad45;letter-spacing:2px;text-transform:uppercase;">Why Pro Corporate League</div>
        <h2 style="margin:8px 0 0 0;font-family:Georgia,'Times New Roman',serif;font-size:28px;line-height:1.2;color:#ffffff;font-weight:700;">15 Years Of Building Corporate Teams Beyond The Workplace</h2>
      </td>
    </tr>
    <tr>
      <td style="padding:10px 18px 22px 18px;">
        <table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0">
          <tr>
            <td width="25%" style="padding:6px;"><div style="background:rgba(255,255,255,.08);border:1px solid rgba(255,255,255,.16);border-radius:14px;padding:15px 10px;text-align:center;"><div style="font-size:24px;color:#d6ad45;font-family:Georgia,'Times New Roman',serif;">15+</div><div style="font-size:11px;line-height:1.5;color:#eaf1ff;">Years Building Corporate Cricket</div></div></td>
            <td width="25%" style="padding:6px;"><div style="background:rgba(255,255,255,.08);border:1px solid rgba(255,255,255,.16);border-radius:14px;padding:15px 10px;text-align:center;"><div style="font-size:24px;color:#d6ad45;font-family:Georgia,'Times New Roman',serif;">500+</div><div style="font-size:11px;line-height:1.5;color:#eaf1ff;">Corporate Teams Engaged</div></div></td>
            <td width="25%" style="padding:6px;"><div style="background:rgba(255,255,255,.08);border:1px solid rgba(255,255,255,.16);border-radius:14px;padding:15px 10px;text-align:center;"><div style="font-size:24px;color:#d6ad45;font-family:Georgia,'Times New Roman',serif;">3000+</div><div style="font-size:11px;line-height:1.5;color:#eaf1ff;">Matches Conducted</div></div></td>
            <td width="25%" style="padding:6px;"><div style="background:rgba(255,255,255,.08);border:1px solid rgba(255,255,255,.16);border-radius:14px;padding:15px 10px;text-align:center;"><div style="font-size:24px;color:#d6ad45;font-family:Georgia,'Times New Roman',serif;">Sony LIV</div><div style="font-size:11px;line-height:1.5;color:#eaf1ff;">Finals Ecosystem</div></div></td>
          </tr>
        </table>
      </td>
    </tr>
    <tr>
      <td style="padding:0 24px 24px 24px;font-size:13px;line-height:1.8;color:#dfe8ff;">
        Corporate culture, employee wellness, team bonding and employer branding — built through a structured corporate cricket ecosystem.
      </td>
    </tr>
  </table>
</td>
</tr>

<!-- CLOSING COPY -->
<tr>
<td style="padding:18px 36px 12px 36px;background:#ffffff;">
  <div style="font-size:15px;line-height:1.8;color:#24304a;">
    <p style="margin:0 0 14px 0;">Beyond the cricket field, participating organizations benefit through employee engagement initiatives, leadership visibility, employer-brand positioning and company-wide recognition opportunities.</p>

    <p style="margin:0 0 14px 0;">We would be honoured to welcome <?= $safeTeam ?> as part of the founding qualification round of the Corporate World Cup™.</p>

    <p style="margin:0 0 14px 0;">We look forward to hearing from you.</p>

    <p style="margin:18px 0 0 0;">Warm regards,<br><br>Sachin Gupta<br>Founder & CEO<br>Pro Corporate League®<br><br>Corporate World Cup™<br>A Pro Corporate League Initiative<br><br>📧 <a href="mailto:sachin.gupta@procorporateleague.com" style="color:#071d46;text-decoration:none;">sachin.gupta@procorporateleague.com</a><br>🌐 <a href="<?= $cwcUrl ?>" style="color:#071d46;text-decoration:none;">corporateworldcup.com</a></p>
    <img src="<?= $trophyImage ?>" width="110" alt="Corporate World Cup Trophy" style="display:block;width:110px;height:auto;border:0;margin:16px 0 0 0;">
  </div>
</td>
</tr>

<!-- CTA FOOTER -->
<tr>
<td style="background:#071d46;padding:26px 28px;text-align:center;">
  <div style="font-family:Georgia,'Times New Roman',serif;font-size:26px;line-height:1.25;color:#ffffff;margin-bottom:8px;">Begin The Corporate World Cup™ Qualification Journey</div>
  <div style="font-size:14px;line-height:1.7;color:#dfe8ff;margin-bottom:20px;">Review the detailed deliverables and reserve the <?= $safeSector ?> category for <?= $safeCity ?>.</div>

  <a href="<?= $teamDashboardUrl ?>" style="display:inline-block;background:#d6ad45;color:#071d46;text-decoration:none;font-size:13px;letter-spacing:.6px;text-transform:uppercase;padding:13px 22px;border-radius:999px;margin:4px;">View Team Dashboard</a>
  <a href="<?= $reserveUrl ?>" style="display:inline-block;border:1px solid #d6ad45;color:#ffffff;text-decoration:none;font-size:13px;letter-spacing:.6px;text-transform:uppercase;padding:12px 22px;border-radius:999px;margin:4px;">Reserve Category</a>

  <div style="font-size:11px;line-height:1.8;color:#b9c6df;margin-top:20px;">
    15+ Years Building Corporate Cricket • 500+ Corporate Teams • 3000+ Matches Conducted<br>
    Chris Gayle 2026 • Jonty Rhodes 2025 • PCL on Sony LIV<br>
    Pro Corporate League® • India's Corporate Cricket Ecosystem<br><br>

    <a href="https://corporateworldcup.com/" style="color:#d6ad45;text-decoration:none;">corporateworldcup.com</a> &nbsp;|&nbsp;
    <a href="https://www.roadtocorporateworldcup.com/" style="color:#d6ad45;text-decoration:none;">roadtocorporateworldcup.com</a><br>
    <a href="https://www.procorporateleague.com/pcl/media/chris-gayle-universe-boss-corporate-cricket-india/" style="color:#d6ad45;text-decoration:none;">Chris Gayle at PCL</a> &nbsp;|&nbsp;
    <a href="https://www.procorporateleague.com/pcl/media/jonty-rhodes-goat-pcl-corporate-cricket-india/" style="color:#d6ad45;text-decoration:none;">Jonty Rhodes at PCL</a><br>
    <a href="https://www.procorporateleague.com/pcl/media/pcl-legacy-corporate-cricket-india/" style="color:#d6ad45;text-decoration:none;">PCL Legacy</a> &nbsp;|&nbsp;
    <a href="https://www.procorporateleague.com/pcl/media/pcl-print-media-coverage-corporate-cricket-india/" style="color:#d6ad45;text-decoration:none;">Print Media</a> &nbsp;|&nbsp;
    <a href="https://www.procorporateleague.com/pcl/media/pcl-corporate-cricket-electronic-media-coverage/" style="color:#d6ad45;text-decoration:none;">Electronic Media</a><br>
    <a href="https://www.procorporateleague.com/ai/pcl-ai-sports-tech-platform/" style="color:#d6ad45;text-decoration:none;">PCL AI Sports Tech</a> &nbsp;|&nbsp;
    <a href="https://www.procorporateleague.com/ai/corporate-cricket-rankings-india" style="color:#d6ad45;text-decoration:none;">Corporate Cricket Rankings</a><br>
    pcl@procorporateleague.com<br>
    © 2026 Pro Corporate League. This communication is an official Corporate World Cup™ invitation for <?= $safeTeam ?>.
  </div>
</td>
</tr>

</table>

</td>
</tr>
</table>

</body>
</html>
<?php
$emailBody = ob_get_clean();

/* ===============================
   SEND EMAIL
================================ */
$mail = new PHPMailer(true);

$toName = $profile['team_name'] ?? '';

try {
    $mail->isSMTP();
    $mail->Host       = $smtpRow['smtp_host'];
    $mail->SMTPAuth   = true;
    $mail->Username   = $smtpRow['smtp_username'];
    $mail->Password   = $smtpRow['smtp_password'];
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port       = 587;
    $mail->Timeout       = 15;    // ← add karo
    $mail->SMTPKeepAlive = true;  // ← add karo

    $mail->CharSet  = 'UTF-8';
    $mail->Encoding = 'base64';

    $mail->setFrom('sachin.gupta@procorporateleague.com', 'Pro Corporate League');

    $mail->addAddress($email, htmlspecialchars($toName));

    if (!empty($ccList) && is_array($ccList)) {
        foreach ($ccList as $cc) {
            $ccEmail = trim($cc['email'] ?? '');
            $ccName  = trim($cc['name']  ?? '');
            if (!empty($ccEmail)) {
                $mail->addCC($ccEmail, $ccName);
            }
        }
    } else {
        $mail->addCC('sachin.gupta@procorporateleague.com', 'Sachin Gupta');
        $mail->addCC('rifat.saleem@procorporateleague.com', 'Rifat Saleem');
        $mail->addCC('manvi.tanwar@procorporateleague.com', 'Manvi Tanwar');
    }

    $mail->isHTML(true);



   
    /* ── PDF ATTACHMENTS ── */
    $pdf1Path = __DIR__ . '/CorporateWorldCup_X_Team_Invitation.pdf';
    // $pdf2Path = __DIR__ . '/Team_Proposal.pdf'; 

    if (file_exists($pdf1Path)) {
        $mail->addAttachment($pdf1Path, 'PCL_CorporateWorldCup_X_Team_Invitation.pdf');
    }
    // if (file_exists($pdf2Path)) {
    //     $mail->addAttachment($pdf2Path, 'PCL_CWC_Document_2.pdf');
    // }
   /* ── END PDF ATTACHMENTS ── */






    $mail->Subject = !empty($requestData['subject'])
        ? $requestData['subject']
        : $safeTeam . ' Has Been Shortlisted For The Corporate World Cup™';

    $mail->Body = $emailBody;

    $mail->send();

    echo json_encode([
        "status_code" => 200,
        "status"      => "success",
        "message"     => "Email sent successfully"
    ]);

} catch (Exception $e) {
    echo json_encode([
        "status_code" => 500,
        "status"      => "error",
        "message"     => $mail->ErrorInfo,
        "debug"       => [
            "email"     => $email,
            "toName"    => $toName,
            "teamId"    => $teamId,
            "smtp_host" => $smtpRow['smtp_host'],
            "smtp_user" => $smtpRow['smtp_username'],
        ]
    ]);
}
?>