<?php
// Artistry Cake Stylist contact and booking handler.
// The receiving address can be changed below if required.
$recipient = 'info@artistrycakestylist.com';
$fromAddress = 'website@artistrycakestylist.com';

function clean_line($value) {
    $value = trim((string)$value);
    return str_replace(["\r", "\n"], ' ', $value);
}
function clean_text($value) {
    return trim(strip_tags((string)$value));
}
function post_value($key, $default = '') {
    return isset($_POST[$key]) ? clean_text($_POST[$key]) : $default;
}
function render_result($success, $title, $message) {
    $colour = $success ? '#df4b62' : '#9b2c3d';
    $button = $success ? 'index.html' : 'javascript:history.back()';
    $buttonText = $success ? 'Return to Home' : 'Go Back';
    echo '<!doctype html><html lang="en-AU"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>'.htmlspecialchars($title).'</title><style>body{margin:0;min-height:100vh;display:grid;place-items:center;background:linear-gradient(135deg,#fff8f7,#f8dfe1);font-family:Arial,sans-serif;color:#2d2929}.card{max-width:650px;margin:24px;background:#fff;padding:48px;border-radius:24px;box-shadow:0 24px 70px rgba(112,48,59,.14);text-align:center}.mark{width:64px;height:64px;border-radius:50%;display:grid;place-items:center;background:'.$colour.';color:#fff;font-size:30px;margin:0 auto 22px}h1{font-family:Georgia,serif;font-size:42px;color:'.$colour.'}p{line-height:1.7;color:#6f6464}.btn{display:inline-block;margin-top:20px;padding:14px 24px;border-radius:999px;background:'.$colour.';color:#fff;text-decoration:none;font-weight:bold}</style></head><body><div class="card"><div class="mark">'.($success?'♥':'!').'</div><h1>'.htmlspecialchars($title).'</h1><p>'.htmlspecialchars($message).'</p><a class="btn" href="'.$button.'">'.$buttonText.'</a></div></body></html>';
    exit;
}

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    render_result(false, 'Invalid request', 'Please return to the website and submit the form.');
}
if (!empty($_POST['website'])) {
    render_result(true, 'Thank you', 'Your message has been received.');
}

$formType = post_value('form_type', 'contact');
$recipient = $formType === 'booking' ? 'bookings@artistrycakestylist.com' : 'info@artistrycakestylist.com';
$name = clean_line(post_value('full_name'));
$email = clean_line(post_value('email'));
$phone = clean_line(post_value('phone'));
if ($name === '' || $phone === '' || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
    render_result(false, 'Please check your details', 'A valid name, phone number and email address are required.');
}

$subject = $formType === 'booking' ? 'New cake booking request - '.$name : 'New website enquiry - '.$name;
$fields = [];
foreach ($_POST as $key => $value) {
    if (in_array($key, ['website','form_type'], true)) continue;
    $label = ucwords(str_replace('_', ' ', $key));
    if (is_array($value)) {
        $value = implode(', ', array_map('clean_text', $value));
    } else {
        $value = clean_text($value);
    }
    if ($value !== '') $fields[] = $label.":\n".$value;
}
$body = "A new ".($formType === 'booking' ? 'cake booking request' : 'website enquiry')." has been submitted.\n\n".implode("\n\n", $fields)."\n\nSubmitted from: ".($_SERVER['HTTP_HOST'] ?? 'website');
$headers = [];
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-Type: text/plain; charset=UTF-8';
$headers[] = 'From: Artistry Website <'.$fromAddress.'>';
$headers[] = 'Reply-To: '.$name.' <'.$email.'>';
$sent = @mail($recipient, $subject, $body, implode("\r\n", $headers));

if ($sent) {
    // Optional customer acknowledgement.
    $customerSubject = $formType === 'booking' ? 'We received your Artistry cake booking request' : 'We received your Artistry enquiry';
    $customerBody = "Hi ".$name.",\n\nThank you for contacting Artistry Cake Stylist. Your ".($formType === 'booking' ? 'booking request' : 'message')." has been received. Karen will review the details and respond as soon as possible.\n\nPlease note that a booking is not secured until availability is confirmed and the required deposit has been received.\n\nArtistry Cake Stylist\n0433 015 037";
    $customerHeaders = 'From: Artistry Cake Stylist <'.$recipient.">\r\nContent-Type: text/plain; charset=UTF-8";
    @mail($email, $customerSubject, $customerBody, $customerHeaders);
    render_result(true, 'Thank you', $formType === 'booking' ? 'Your cake booking request has been sent. Karen will review your date, design and estimate before confirming availability and final pricing.' : 'Your message has been sent. Karen will respond as soon as possible.');
}
render_result(false, 'Message not sent', 'The server could not send the email. Please contact Karen directly on 0433 015 037 or email info@artistrycakestylist.com.');
?>
