<?php
session_start();

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

require 'vendor/autoload.php';

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

// Check if POST request and all fields are set
if (
    $_SERVER["REQUEST_METHOD"] === "POST" &&
    !empty($_POST['name']) &&
    !empty($_POST['phone']) &&
    !empty($_POST['subject']) &&
    !empty($_POST['message'])
) {
    // Sanitize Inputs
    $name    = htmlspecialchars(strip_tags($_POST['name']));
    $phone   = htmlspecialchars(strip_tags($_POST['phone']));
    $email   = isset($_POST['email']) ? htmlspecialchars(strip_tags($_POST['email'])) : 'N/A';
    $subject = htmlspecialchars(strip_tags($_POST['subject']));
    $message = htmlspecialchars(strip_tags($_POST['message']));
    $date    = isset($_POST['date']) ? htmlspecialchars(strip_tags($_POST['date'])) : 'N/A';

    $mail = new PHPMailer(true);

    try {
        // SMTP Configuration
        $mail->isSMTP();
        $mail->Host       = 'newdedi.clouddreams.in';
        $mail->SMTPAuth   = true;
        $mail->Username   = 'tester@clouddreams.in';
        $mail->Password   = 'OUHbP$W2$';
        $mail->SMTPSecure = 'tls';
        $mail->Port       = 587;

        // Sender & Recipient
        $mail->setFrom('tester@clouddreams.in', 'Starcare Website');
        $mail->addAddress('gokulnath@clouddreams.in', 'Recipient Name');

        // Email Content
        $mail->isHTML(true);
        $mail->Subject = 'New Enquiry from Starcare Website - ' . $subject;
        $mail->Body = "
           <html>
           <body style='font-family: Arial, sans-serif; background-color: #f4f9fc; padding: 20px;'>
               <div style='max-width: 600px; margin: auto; background: #ffffff; padding: 30px; border-radius: 10px; box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);'>
                   <h2 style='color: #135CAA; margin-bottom: 20px;'>New Enquiry from Starcare Website</h2>
                   <p><strong style='color: #135CAA;'>Name:</strong> $name</p>
                   <p><strong style='color: #135CAA;'>Phone:</strong> $phone</p>
                   <p><strong style='color: #135CAA;'>Email:</strong> $email</p>
                   <p><strong style='color: #135CAA;'>Preferred Date:</strong> $date</p>
                   <p><strong style='color: #135CAA;'>Service Type:</strong> $subject</p>
                   <p><strong style='color: #135CAA;'>Message:</strong></p>
                   <p style='background: #f1f7fb; padding: 15px; border-radius: 5px; color: #333;'>$message</p>
                   <hr style='margin: 30px 0; border: none; border-top: 1px solid #e0e0e0;'>
                   <p style='font-size: 12px; color: #999; text-align: center;'>This message was sent from the Starcare website contact form.</p>
               </div>
           </body>
           </html>";

        // Allow self-signed SSL
        $mail->SMTPOptions = [
            'ssl' => [
                'verify_peer'       => false,
                'verify_peer_name'  => false,
                'allow_self_signed' => true,
            ],
        ];

        $mail->send();
        echo json_encode([
            "status" => "success",
            "message" => "Your message has been sent successfully!"
        ]);
    } catch (Exception $e) {
        echo json_encode([
            "status" => "failed",
            "message" => "Mailer Error: {$mail->ErrorInfo}"
        ]);
    }
} else {
    echo json_encode([
        "status" => "failed",
        "message" => "Please fill in all required fields."
    ]);
}
