<?php

declare(strict_types=1);

header('Content-Type: application/json; charset=utf-8');

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    http_response_code(405);
    echo json_encode(['ok' => false, 'message' => 'Method not allowed']);
    exit;
}

$name = trim((string)($_POST['name'] ?? ''));
$taxpayerType = trim((string)($_POST['taxpayer_type'] ?? ''));
$incomeComponents = trim((string)($_POST['income_components'] ?? ''));
$filingWhen = trim((string)($_POST['filing_when'] ?? ''));
$phone = trim((string)($_POST['phone'] ?? ''));

if ($name === '' || $taxpayerType === '' || $filingWhen === '' || $phone === '') {
    http_response_code(422);
    echo json_encode(['ok' => false, 'message' => 'Please fill all required fields.']);
    exit;
}

if (!preg_match('/^[0-9+()\-\s]{8,20}$/', $phone)) {
    http_response_code(422);
    echo json_encode(['ok' => false, 'message' => 'Please enter a valid phone number.']);
    exit;
}

require __DIR__ . '/config.php';
require __DIR__ . '/vendor/autoload.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;

try {
    $mail = new PHPMailer(true);

    $mail->isSMTP();
    $mail->Host = $SMTP_HOST;
    $mail->SMTPAuth = true;
    $mail->Username = $SMTP_USER;
    $mail->Password = $SMTP_PASS;
    $mail->SMTPSecure = $SMTP_SECURE;
    $mail->Port = $SMTP_PORT;

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

    $mail->setFrom($MAIL_FROM, $MAIL_FROM_NAME);
    $mail->addAddress($MAIL_TO);

    $mail->isHTML(true);
    $mail->Subject = $MAIL_SUBJECT_PREFIX . ' - ITR Consultation';

    $safeName = htmlspecialchars($name, ENT_QUOTES, 'UTF-8');
    $safeTaxpayerType = htmlspecialchars($taxpayerType, ENT_QUOTES, 'UTF-8');
    $safeIncomeComponents = htmlspecialchars($incomeComponents !== '' ? $incomeComponents : 'N/A', ENT_QUOTES, 'UTF-8');
    $safeFilingWhen = htmlspecialchars($filingWhen, ENT_QUOTES, 'UTF-8');
    $safePhone = htmlspecialchars($phone, ENT_QUOTES, 'UTF-8');

    $mail->Body = '<div style="background:#f3f4f6;padding:24px 12px;font-family:Arial,Helvetica,sans-serif;">'
        . '<div style="max-width:640px;margin:0 auto;background:#ffffff;border:1px solid #e5e7eb;border-radius:16px;overflow:hidden;">'
        . '<div style="background:linear-gradient(90deg,#cfd600,#aab400);padding:18px 20px;">'
        . '<div style="font-size:18px;font-weight:800;color:#111827;">New ITR Consultation Request</div>'
        . '<div style="margin-top:4px;font-size:12px;font-weight:700;color:rgba(17,24,39,.75);">AMC TAXHub Lead</div>'
        . '</div>'
        . '<div style="padding:18px 20px;color:#111827;">'
        . '<div style="font-size:14px;line-height:1.6;color:#374151;">A new user has requested a free ITR consultation. Here are the details:</div>'
        . '<div style="margin-top:14px;border:1px solid #e5e7eb;border-radius:14px;overflow:hidden;">'
        . '<div style="display:flex;gap:12px;padding:12px 14px;border-bottom:1px solid #e5e7eb;background:#f9fafb;">'
        . '<div style="width:170px;font-size:12px;font-weight:800;color:#6b7280;">Name</div>'
        . '<div style="flex:1;font-size:13px;font-weight:800;color:#111827;">' . $safeName . '</div>'
        . '</div>'
        . '<div style="display:flex;gap:12px;padding:12px 14px;border-bottom:1px solid #e5e7eb;">'
        . '<div style="width:170px;font-size:12px;font-weight:800;color:#6b7280;">Taxpayer Type</div>'
        . '<div style="flex:1;font-size:13px;font-weight:800;color:#111827;">' . $safeTaxpayerType . '</div>'
        . '</div>'
        . '<div style="display:flex;gap:12px;padding:12px 14px;border-bottom:1px solid #e5e7eb;background:#f9fafb;">'
        . '<div style="width:170px;font-size:12px;font-weight:800;color:#6b7280;">Income Components</div>'
        . '<div style="flex:1;font-size:13px;font-weight:800;color:#111827;">' . $safeIncomeComponents . '</div>'
        . '</div>'
        . '<div style="display:flex;gap:12px;padding:12px 14px;border-bottom:1px solid #e5e7eb;">'
        . '<div style="width:170px;font-size:12px;font-weight:800;color:#6b7280;">Filing Timeline</div>'
        . '<div style="flex:1;font-size:13px;font-weight:800;color:#111827;">' . $safeFilingWhen . '</div>'
        . '</div>'
        . '<div style="display:flex;gap:12px;padding:12px 14px;background:#f9fafb;">'
        . '<div style="width:170px;font-size:12px;font-weight:800;color:#6b7280;">Phone / WhatsApp</div>'
        . '<div style="flex:1;font-size:13px;font-weight:800;color:#111827;">' . $safePhone . '</div>'
        . '</div>'
        . '</div>'
        . '<div style="margin-top:14px;padding:12px 14px;border-radius:14px;background:rgba(22,163,74,.08);border:1px solid rgba(22,163,74,.25);color:#166534;font-size:13px;font-weight:800;">'
        . 'Next step: Call/WhatsApp the user and confirm documents (AIS/TIS, Form 16, 26AS, etc.).'
        . '</div>'
        . '</div>'
        . '<div style="padding:14px 20px;border-top:1px solid #e5e7eb;background:#ffffff;">'
        . '<div style="font-size:12px;color:#6b7280;font-weight:700;">This lead was generated from the homepage form: <span style="color:#111827;font-weight:800;">Start your ITR in 30 seconds</span>.</div>'
        . '</div>'
        . '</div>'
        . '</div>';

    $mail->AltBody = "New ITR Consultation Request\n\n"
        . "Name: {$name}\n"
        . "Taxpayer Type: {$taxpayerType}\n"
        . "Income Components: " . ($incomeComponents !== '' ? $incomeComponents : 'N/A') . "\n"
        . "When to file: {$filingWhen}\n"
        . "Phone: {$phone}\n";

    $mail->send();

    echo json_encode(['ok' => true]);
} catch (Throwable $e) {
    http_response_code(500);
    echo json_encode(['ok' => false, 'message' => 'Failed to send. Please try again.']);
}
