<?php
session_start();

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

require 'vendor/autoload.php';

header("Content-Type: application/json");

if ($_SERVER["REQUEST_METHOD"] !== "POST") {
    echo json_encode(["status" => "failed", "message" => "Invalid request method."]);
    exit;
}

$required_fields = ['name', 'phone', 'email', 'service', 'description'];
foreach ($required_fields as $field) {
    if (empty($_POST[$field])) {
        echo json_encode(["status" => "failed", "message" => "All fields are required."]);
        exit;
    }
}

$name = htmlspecialchars(strip_tags(trim($_POST['name'])));
$phone = htmlspecialchars(strip_tags(trim($_POST['phone'])));
$email = filter_var(trim($_POST['email']), FILTER_SANITIZE_EMAIL);
$service = htmlspecialchars(strip_tags(trim($_POST['service'])));
$description = nl2br(htmlspecialchars(trim($_POST['description'])));

$mail = new PHPMailer(true);

try {
    $mail->isSMTP();
    $mail->Host       = 'mail.clouddreamstech.in';
    $mail->SMTPAuth   = true;
    $mail->Username   = 'info@clouddreamstech.in';
    $mail->Password   = 'y&Zvy.r2BFX8EQTn';
    $mail->SMTPSecure = 'ssl';
    $mail->Port       = 465;

    $mail->setFrom('info@clouddreamstech.in', 'ilampirai');
    // $mail->addAddress('gokul@clouddreamstech.in', 'Ilampirai Admin');
    $mail->addAddress('info@ilampirai.in', 'Ilampirai Admin');

    $mail->addReplyTo($email, $name);

    $mail->isHTML(true);
    $mail->Subject = "New Service Request $name";
    $mail->Body = "
    <html>
    <body style='background:#eef2f7; margin:0; padding:0; font-family:Arial, sans-serif;'>
        <table width='100%' cellpadding='0' cellspacing='0' style='padding:20px 0;'>
            <tr>
                <td align='center'>
                    <table width='600' cellpadding='0' cellspacing='0' style='background:#fff; border-radius:12px; overflow:hidden; box-shadow:0 4px 15px rgba(0,0,0,0.08);'>
                        <tr>
                            <td style='background:#0250a3; padding:20px; text-align:center;'>
                                <h2 style='color:#fff; margin:0;'>New Service Request</h2>
                            </td>
                        </tr>
                        <tr>
                            <td style='padding:30px;'>
                                <p><strong>Name:</strong> {$name}</p>
                                <p><strong>Phone:</strong> {$phone}</p>
                                <p><strong>Email:</strong> {$email}</p>
                                <p><strong>Service Needed:</strong> {$service}</p>
                                <p style='margin-top:20px;'><strong>Description:</strong></p>
                                <div style='background:#f7f9fc; padding:15px; border-left:4px solid #0250a3; border-radius:6px;'>
                                    {$description}
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td style='background:#fafafa; padding:15px; text-align:center; border-top:1px solid #eee;'>
                                <p style='font-size:12px; color:#777; margin:0;'>
                                    Sent from the <strong>Ilampirai Website</strong>
                                </p>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </body>
    </html>
    ";

    $mail->SMTPOptions = [
        'ssl' => [
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true
        ]
    ];

    $mail->send();

    echo json_encode([
        "status" => "success",
        "message" => "Your request has been sent successfully!"
    ]);
} catch (Exception $e) {
    echo json_encode([
        "status" => "failed",
        "message" => "Mailer Error: {$mail->ErrorInfo}"
    ]);
}
