<?php

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

// الاتصال بقاعدة البيانات والدوال المشتركة
require_once __DIR__ . '/../../connect.php';

// تضمين autoload من Composer (مجلد vendor في الجذر)
require_once __DIR__ . '/../../../vendor/autoload.php';

// استقبال البيانات من الطلب
$passengerName = filterRequest("name");
$passengerEmail = filterRequest("email");
$passengerPhone = filterRequest("phone");
$fee = floatval(filterRequest("fee"));
$startLocation = filterRequest("startLocation");
$endLocation = filterRequest("endLocation");
$startNameLocation = filterRequest("startNameLocation");
$endNameLocation = filterRequest("endNameLocation");
$timeOfTrip = filterRequest("timeOfTrip");
$duration = intval(filterRequest("duration"));
$duration = floor($duration / 60); // تحويل للـ minutes

// حساب الخصم
$discount = 0;
if ($startNameLocation && $endNameLocation) {
    $discount = $fee * 0.12;
    $beforDiscount = $fee;
    $fee -= $discount;
}

// بناء الإيميل
$bodyEmail = "
<html>
<head>
<style>
body { font-family: 'Arial', sans-serif; background-color: #f8f9fa; color: #333; line-height: 1.6; }
table { border-collapse: collapse; width: 100%; background-color: white; margin: 20px 0; box-shadow: 0 4px 6px rgba(0,0,0,0.1); }
th, td { padding: 12px; text-align: left; border-bottom: 1px solid #ddd; }
th { background-color: #007bff; color: white; }
tr:nth-child(even) { background-color: #f2f2f2; }
</style>
</head>
<body>
<img src='https://api.tripz-egypt.com/tripz/images/logo%20(1).png' alt='Tripz Logo' style='width: 150px; margin: 20px auto; display: block;'>
<p>Hi $passengerName,</p>
<p>Thank you for booking your ride with <strong>Tripz</strong>. Here are the details of your recent trip:</p>
<table>
  <tr><th>Detail</th><th>Value</th></tr>
  <tr><td>Passenger</td><td>$passengerName</td></tr>
  <tr><td>Email</td><td>$passengerEmail</td></tr>
  <tr><td>Phone</td><td>$passengerPhone</td></tr>
  <tr><td>Fee</td><td>$$fee</td></tr>
  <tr><td>Start Location</td><td>$startLocation ($startNameLocation)</td></tr>
  <tr><td>End Location</td><td>$endLocation ($endNameLocation)</td></tr>
  <tr><td>Time of Trip</td><td>$timeOfTrip</td></tr>
  <tr><td>Duration</td><td>$duration minutes</td></tr>
</table>";

if ($discount > 0) {
    $bodyEmail .= "<p>You have received a 12% discount on your trip from $startNameLocation to $endNameLocation. The original fee was $$beforDiscount. Your discounted fee is $$fee.</p>";
}

$bodyEmail .= "<p>Thank you for using <strong>Tripz</strong>. We hope you have a great day!</p><p>Best regards,<br>Tripz Team</p></body></html>";

// إعداد البريد
$mail = new PHPMailer(true);

try {
    $mail->isSMTP();
    $mail->Host = 'smtp.hostinger.com';
    $mail->SMTPAuth = true;
    $mail->Username = 'hamzaayed@tripz-egypt.com';
    $mail->Password = $TRIPZ_SMTP_PASSWORD;
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port = 587;

    $mail->setFrom('hamzaayed@tripz-egypt.com', 'Tripz');
    $mail->addAddress($passengerEmail, $passengerName);
    $mail->isHTML(true);
    $mail->Subject = 'Your Tripz Trip Details';
    $mail->Body    = $bodyEmail;

    $mail->send();
    echo json_encode(["status" => "success", "message" => "Email sent successfully"]);
} catch (Exception $e) {
    echo json_encode(["status" => "error", "message" => $mail->ErrorInfo]);
}