<?php
/**
 * Plugin Name: Coco Beauty Bar Contact Form Handler
 * Description: Handles form submissions and sends emails via MailService.
 */

add_action('wp_ajax_coco_contact_submit', 'coco_contact_submit_handler');
add_action('wp_ajax_nopriv_coco_contact_submit', 'coco_contact_submit_handler');

function coco_contact_submit_handler() {
    $name = isset($_POST['name']) ? sanitize_text_field($_POST['name']) : '';
    $email = isset($_POST['email']) ? sanitize_email($_POST['email']) : '';
    $phone = isset($_POST['phone']) ? sanitize_text_field($_POST['phone']) : '';
    $service = isset($_POST['service']) ? sanitize_text_field($_POST['service']) : '';
    $message = isset($_POST['message']) ? sanitize_textarea_field($_POST['message']) : '';

    if (empty($name) || empty($email)) {
        wp_send_json_error(['message' => 'Please fill in all required fields.']);
    }

    require_once dirname(__DIR__, 2) . '/mail/MailService.php';

    $to = getenv('MAIL_TO') ?: get_option('admin_email');
    $subject = "New Booking Request: $service from $name";
    
    $body = "Name: $name\n";
    $body .= "Email: $email\n";
    $body .= "Phone: $phone\n";
    $body .= "Service: $service\n";
    $body .= "Message: $message\n";

    $res = MailService::sendMail($to, $subject, nl2br($body), $body, ['reply_to' => $email]);

    if (!empty($res['success'])) {
        wp_send_json_success(['message' => coco_i18n()->t('form_success')]);
    } else {
        wp_send_json_error(['message' => coco_i18n()->t('form_error')]);
    }
}

// Add the form shortcode
add_shortcode('coco_contact_form', function() {
    ob_start();
    ?>
    <form id="coco-contact-form" style="max-width: 600px; margin: 0 auto; background: white; padding: 30px; border-radius: 15px; box-shadow: 0 10px 30px rgba(0,0,0,0.05);">
        <div style="margin-bottom: 15px;">
            <label style="display: block; margin-bottom: 5px; font-weight: 600;"><?php echo coco_i18n()->t('form_name'); ?> *</label>
            <input type="text" name="name" required style="width: 100%; padding: 10px; border: 1px solid #ddd; border-radius: 5px;">
        </div>
        <div style="margin-bottom: 15px;">
            <label style="display: block; margin-bottom: 5px; font-weight: 600;"><?php echo coco_i18n()->t('form_email'); ?> *</label>
            <input type="email" name="email" required style="width: 100%; padding: 10px; border: 1px solid #ddd; border-radius: 5px;">
        </div>
        <div style="margin-bottom: 15px;">
            <label style="display: block; margin-bottom: 5px; font-weight: 600;"><?php echo coco_i18n()->t('form_phone'); ?></label>
            <input type="text" name="phone" style="width: 100%; padding: 10px; border: 1px solid #ddd; border-radius: 5px;">
        </div>
        <div style="margin-bottom: 15px;">
            <label style="display: block; margin-bottom: 5px; font-weight: 600;"><?php echo coco_i18n()->t('form_service'); ?></label>
            <select name="service" style="width: 100%; padding: 10px; border: 1px solid #ddd; border-radius: 5px;">
                <option value="Hair Styling"><?php echo coco_i18n()->t('service_hair'); ?></option>
                <option value="Manicure/Pedicure"><?php echo coco_i18n()->t('service_nails'); ?></option>
                <option value="Facial Treatment"><?php echo coco_i18n()->t('service_facial'); ?></option>
                <option value="Massage">Massage</option>
                <option value="Other">Other</option>
            </select>
        </div>
        <div style="margin-bottom: 15px;">
            <label style="display: block; margin-bottom: 5px; font-weight: 600;"><?php echo coco_i18n()->t('form_message'); ?></label>
            <textarea name="message" rows="4" style="width: 100%; padding: 10px; border: 1px solid #ddd; border-radius: 5px;"></textarea>
        </div>
        <button type="submit" class="wp-block-button__link" style="width: 100%; border: none; cursor: pointer;"><?php echo coco_i18n()->t('form_submit'); ?></button>
        <div id="form-response" style="margin-top: 15px; padding: 10px; border-radius: 5px; display: none;"></div>
    </form>

    <script>
    document.getElementById('coco-contact-form').addEventListener('submit', async function(e) {
        e.preventDefault();
        const form = this;
        const responseDiv = document.getElementById('form-response');
        const formData = new FormData(form);
        formData.append('action', 'coco_contact_submit');

        responseDiv.style.display = 'block';
        responseDiv.style.background = '#f0f0f0';
        responseDiv.textContent = 'Sending...';

        try {
            const response = await fetch('/wp-admin/admin-ajax.php', {
                method: 'POST',
                body: formData
            });
            const data = await response.json();
            
            if (data.success) {
                responseDiv.style.background = '#d4edda';
                responseDiv.style.color = '#155724';
                responseDiv.textContent = data.data.message;
                form.reset();
            } else {
                responseDiv.style.background = '#f8d7da';
                responseDiv.style.color = '#721c24';
                responseDiv.textContent = data.data.message;
            }
        } catch (error) {
            responseDiv.style.background = '#f8d7da';
            responseDiv.style.color = '#721c24';
            responseDiv.textContent = 'An error occurred. Please try again.';
        }
    });
    </script>
    <?php
    return ob_get_clean();
});
