<?php

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

require_once 'db.php';

require_once __DIR__ . '/PHPMailer/src/Exception.php';
require_once __DIR__ . '/PHPMailer/src/PHPMailer.php';
require_once __DIR__ . '/PHPMailer/src/SMTP.php';

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

try {

    $data = json_decode(file_get_contents("php://input"), true);

    $name       = trim($data['name'] ?? '');
    $phone      = trim($data['phone'] ?? '');
    $salon      = trim($data['salon'] ?? '');
    $services   = trim($data['services'] ?? '');
    $date       = trim($data['date'] ?? '');
    $time       = trim($data['time'] ?? '');
    $notes      = trim($data['notes'] ?? '');

    $consent1   = (int)($data['consent1'] ?? 0);
    $consent2   = (int)($data['consent2'] ?? 0);
    $consent3   = (int)($data['consent3'] ?? 0);
    $consent4   = (int)($data['consent4'] ?? 0);

    $signature  = $data['signature'] ?? '';

    if (empty($name) || empty($phone) || empty($salon)) {

        echo json_encode([
            "success" => false,
            "message" => "Please fill all required fields."
        ]);
        exit;
    }

    $stmt = $conn->prepare("
        INSERT INTO appointment_requests
        (
            customer_name,
            phone,
            salon,
            services,
            preferred_date,
            preferred_time,
            notes,
            consent_1,
            consent_2,
            consent_3,
            consent_4,
            signature
        )
        VALUES
        (?,?,?,?,?,?,?,?,?,?,?,?)
    ");

    $stmt->bind_param(
        "sssssssiiiis",
        $name,
        $phone,
        $salon,
        $services,
        $date,
        $time,
        $notes,
        $consent1,
        $consent2,
        $consent3,
        $consent4,
        $signature
    );

    if (!$stmt->execute()) {

        echo json_encode([
            "success" => false,
            "message" => $stmt->error
        ]);
        exit;
    }

    $subject = "New Appointment Booking - Maharaani Salon";

    $message = "

NEW APPOINTMENT REQUEST

--------------------------------------------

Customer Name:
$name

Phone Number:
$phone

Preferred Salon:
$salon

Services Selected:
$services

Preferred Date:
$date

Preferred Time:
$time

Additional Notes:
$notes

--------------------------------------------

Consent 1:
" . ($consent1 ? "YES" : "NO") . "

Consent 2:
" . ($consent2 ? "YES" : "NO") . "

Consent 3:
" . ($consent3 ? "YES" : "NO") . "

Consent 4:
" . ($consent4 ? "YES" : "NO") . "

--------------------------------------------

Submitted On:
" . date('d M Y h:i A') . "

";



$mail = new PHPMailer(true);

$mail->isSMTP();

$mail->Host = 'misfitamigos.com';

$mail->SMTPAuth = true;

$mail->Username = 'fortestmail@misfitamigos.com';

$mail->Password = 'OSd^uRNO^*hd*8?C';

$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;

$mail->Port = 465;

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

$mail->setFrom(
    'fortestmail@misfitamigos.com',
    'Maharaani Salon'
);

$mail->addAddress('galhotra_pooja@yahoo.com');

    $mail->Subject = $subject;

    $mail->Body = $message;
    
    $mail->SMTPDebug = 0;

    $mail->send();

    echo json_encode([
        "success" => true,
        "message" => "Appointment submitted successfully."
    ]);

} catch (Exception $e) {

    echo json_encode([
        "success" => false,
        "message" => $e->getMessage()
    ]);

}
?>
