<?php
session_start();

if ($_SERVER['REQUEST_METHOD'] != 'POST') {
    header("Location: index.php");
    exit;
}

$errors = [];
$old = [];


/*
|--------------------------------------------------------------------------
| Get & Clean Data
|--------------------------------------------------------------------------
*/

$ownerName   = trim($_POST['ownerName'] ?? '');
$petName     = trim($_POST['petName'] ?? '');
$phoneNum    = trim($_POST['phoneNum'] ?? '');
$concernType = trim($_POST['concernType'] ?? '');
$notes       = trim($_POST['notes'] ?? '');

$old = [
    'ownerName'   => $ownerName,
    'petName'     => $petName,
    'phoneNum'    => $phoneNum,
    'concernType' => $concernType,
    'notes'       => $notes
];


/*
|--------------------------------------------------------------------------
| Validation
|--------------------------------------------------------------------------
*/

if ($ownerName == '') {

    $errors['ownerName'] = "Owner name is required.";

} elseif (!preg_match("/^[a-zA-Z\s.'-]{2,50}$/", $ownerName)) {

    $errors['ownerName'] = "Please enter a valid name.";
}


if ($petName == '') {

    $errors['petName'] = "Pet name is required.";

} elseif (strlen($petName) < 2) {

    $errors['petName'] = "Pet name is too short.";
}


if ($phoneNum == '') {

    $errors['phoneNum'] = "Phone number is required.";

} elseif (!preg_match('/^[0-9+\-\s()]{7,20}$/', $phoneNum)) {

    $errors['phoneNum'] = "Enter a valid phone number.";
}


$allowed = [
    "Pain or mobility",
    "General senior wellness",
    "Nutrition or weight",
    "Cognitive or behavior change",
    "Hospice / end-of-life support"
];

if (!in_array($concernType, $allowed)) {

    $errors['concernType'] = "Please select a valid concern.";
}


if (!empty($notes) && strlen($notes) > 1000) {

    $errors['notes'] = "Notes cannot exceed 1000 characters.";
}


/*
|--------------------------------------------------------------------------
| Validation Failed
|--------------------------------------------------------------------------
*/

if (!empty($errors)) {

    $_SESSION['errors'] = $errors;
    $_SESSION['old'] = $old;

    header("Location: index.php#contact");
    exit;
}


/*
|--------------------------------------------------------------------------
| Mail Code
|--------------------------------------------------------------------------
*/

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

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

$mail = new PHPMailer(true);

try{

    $mail->isSMTP();
    $mail->Host = 'mail.convirzon.com';
    $mail->SMTPAuth = true;

    $mail->Username = 'sujal@convirzon.com';
    $mail->Password = 'P0iuytrewq@2026#$';   

    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // SSL
    $mail->Port = 465;

    $mail->setFrom('sujal@convirzon.com', 'Pet Care');

    $mail->addAddress('rittwik@convirzon.com');
    $mail->isHTML(true);

    $mail->Subject = "New Appointment Request";

    $mail->Body = "

    <h2>New Appointment</h2>

    <strong>Owner:</strong> {$ownerName}<br><br>

    <strong>Pet:</strong> {$petName}<br><br>

    <strong>Phone:</strong> {$phoneNum}<br><br>

    <strong>Concern:</strong> {$concernType}<br><br>

    <strong>Notes:</strong><br>

    ".nl2br(htmlspecialchars($notes));

    $mail->send();

    $_SESSION['success'] = "Appointment request submitted successfully.";

}
catch (Exception $e) {

    $_SESSION['errors']['mail'] = $mail->ErrorInfo;
    $_SESSION['old'] = $old;

    header("Location: index.php#contact");
    exit;
}

header("Location: index.php#contact");
exit;