<?php
// email/sendTripEmail.php — نسخة مؤمنة

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/../core/bootstrap.php';

// 1. فرض المصادقة (Auth Required)
$jwtService = new JwtService($redis);
$user = $jwtService->authenticate();
$EMAIL_ADDRESS = 'hamzaayed@intaleqapp.com';

// 2. استقبال البيانات وتطهيرها (Sanitization)
$passengerName     = htmlspecialchars(filterRequest('name') ?? 'User', ENT_QUOTES, 'UTF-8');
$passengerEmail    = filter_var(filterRequest('email'), FILTER_SANITIZE_EMAIL);
$passengerPhone    = htmlspecialchars(filterRequest('phone') ?? '', ENT_QUOTES, 'UTF-8');
$fee               = floatval(filterRequest('fee') ?? 0);
$startNameLocation = htmlspecialchars(filterRequest('startNameLocation') ?? '', ENT_QUOTES, 'UTF-8');
$endNameLocation   = htmlspecialchars(filterRequest('endNameLocation') ?? '', ENT_QUOTES, 'UTF-8');
$timeOfTrip        = htmlspecialchars(filterRequest('timeOfTrip') ?? date('Y-m-d H:i:s'), ENT_QUOTES, 'UTF-8');

if (!$passengerEmail || !filter_var($passengerEmail, FILTER_VALIDATE_EMAIL)) {
    jsonError("Invalid email address");
}

$SIRO_SMTP_PASSWORD = getenv('SIRO_SMTP_PASSWORD');

// بناء محتوى الإيميل بتصميم عصري وبريميوم
$bodyEmail = "
<!DOCTYPE html>
<html lang='en'>
<head>
    <meta charset='UTF-8'>
    <style>
        body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background-color: #f4f7f9; color: #333; margin: 0; padding: 0; }
        .container { max-width: 600px; margin: 20px auto; background: #ffffff; border-radius: 12px; overflow: hidden; box-shadow: 0 10px 30px rgba(0,0,0,0.1); }
        .header { background: linear-gradient(135deg, #0052D4, #4364F7, #6FB1FC); color: #ffffff; padding: 40px 20px; text-align: center; }
        .header h1 { margin: 0; font-size: 28px; letter-spacing: 2px; text-transform: uppercase; }
        .content { padding: 30px; }
        .greeting { font-size: 20px; font-weight: bold; margin-bottom: 10px; color: #0052D4; }
        .trip-card { background: #f9f9f9; border-left: 5px solid #0052D4; padding: 20px; border-radius: 8px; margin: 20px 0; }
        .detail-row { display: flex; justify-content: space-between; padding: 10px 0; border-bottom: 1px solid #eee; }
        .detail-row:last-child { border-bottom: none; }
        .label { font-weight: 600; color: #666; }
        .value { font-weight: bold; color: #333; }
        .fee-section { text-align: center; margin-top: 30px; padding: 20px; background: #eef2f7; border-radius: 8px; }
        .fee-label { font-size: 14px; color: #666; text-transform: uppercase; }
        .fee-amount { font-size: 32px; font-weight: 900; color: #0052D4; }
        .footer { background: #333; color: #999; text-align: center; padding: 20px; font-size: 12px; }
        .footer p { margin: 5px 0; }
    </style>
</head>
<body>
    <div class='container'>
        <div class='header'>
            <h1>SIRO</h1>
            <p>Your journey, our priority</p>
        </div>
        <div class='content'>
            <div class='greeting'>Hello, $passengerName!</div>
            <p>Thank you for choosing <strong>SIRO</strong>. Your trip has been successfully confirmed. Here is your digital receipt:</p>
            
            <div class='trip-card'>
                <div class='detail-row'>
                    <span class='label'>From:</span>
                    <span class='value'>$startNameLocation</span>
                </div>
                <div class='detail-row'>
                    <span class='label'>To:</span>
                    <span class='value'>$endNameLocation</span>
                </div>
                <div class='detail-row'>
                    <span class='label'>Date & Time:</span>
                    <span class='value'>$timeOfTrip</span>
                </div>
                <div class='detail-row'>
                    <span class='label'>Phone:</span>
                    <span class='value'>$passengerPhone</span>
                </div>
            </div>

            <div class='fee-section'>
                <div class='fee-label'>Total Amount</div>
                <div class='fee-amount'>$$fee</div>
            </div>

            <p style='margin-top: 30px;'>If you have any questions, feel free to contact our support team at any time.</p>
        </div>
        <div class='footer'>
            <p>&copy; " . date('Y') . " SIRO. All rights reserved.</p>
            <p>Smart Transportation Solutions</p>
        </div>
    </div>
</body>
</html>";

$mail = new PHPMailer(true);
try {
    $mail->isSMTP();
    $mail->Host       = 'smtp.hostinger.com';
    $mail->SMTPAuth   = true;
    $mail->Username   = $EMAIL_ADDRESS;
    $mail->Password   = $SIRO_SMTP_PASSWORD;
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port       = 587;

    $mail->setFrom($EMAIL_ADDRESS, 'SIRO');
    $mail->addAddress($passengerEmail, $passengerName);
    $mail->isHTML(true);
    $mail->Subject = 'Your SIRO Trip Details';
    $mail->Body    = $bodyEmail;

    $mail->send();
    jsonSuccess(null, "Email sent successfully");
} catch (Exception $e) {
    jsonError("Failed to send email: " . $mail->ErrorInfo);
}