<?php
/**
 * Odysseo Core — front-end contact form AJAX handler.
 *
 * Validates + stores the inquiry (see contact-inquiries.php) and emails the site
 * owner. Honeypot-protected. The contact form markup lives in the theme template.
 *
 * @package    OdysseoCore
 * @author     Chandrabhan Shekhawat
 * @company    Gigai Kripa Services
 * @link       https://gigaikripaservices.com/
 * @copyright  Copyright (c) 2026, Gigai Kripa Services
 */

if ( ! defined( 'ABSPATH' ) ) exit;

/**
 * AJAX Contact Form Handler
 */
function odysseo_handle_contact_form() {
    check_ajax_referer( 'odysseo_nonce', 'nonce' );

    // Honeypot: a hidden field no human ever fills. If it has a value, it's a
    // bot — silently pretend success so the bot gets no useful signal.
    if ( ! empty( $_POST['odysseo_website'] ) ) {
        wp_send_json_success( array( 'message' => __( 'Thank you! Your message has been sent successfully.', 'odysseo-core' ) ) );
    }

    $name    = sanitize_text_field( wp_unslash( $_POST['name'] ?? '' ) );
    $email   = sanitize_email( wp_unslash( $_POST['email'] ?? '' ) );
    $phone   = sanitize_text_field( wp_unslash( $_POST['phone'] ?? '' ) );
    $subject = sanitize_text_field( wp_unslash( $_POST['subject'] ?? '' ) );
    $message = sanitize_textarea_field( wp_unslash( $_POST['message'] ?? '' ) );

    if ( empty( $name ) || empty( $email ) || empty( $message ) ) {
        wp_send_json_error( array( 'message' => __( 'Please fill all required fields.', 'odysseo-core' ) ) );
    }

    // Store the inquiry first so it is never lost, even if the email fails.
    odysseo_store_inquiry( compact( 'name', 'email', 'phone', 'subject', 'message' ) );

    // Send to the Customizer "Email Address" if the owner has set a real one;
    // otherwise fall back to the WordPress admin email.
    $contact_email = sanitize_email( get_theme_mod( 'contact_email', '' ) );
    $is_placeholder = ( '' === $contact_email ) || ! is_email( $contact_email ) || ( false !== strpos( $contact_email, 'example.com' ) );
    $to      = $is_placeholder ? get_option( 'admin_email' ) : $contact_email;
    $headers = array(
        'Content-Type: text/html; charset=UTF-8',
        'From: ' . get_bloginfo( 'name' ) . ' <' . get_option( 'admin_email' ) . '>',
        'Reply-To: ' . $name . ' <' . $email . '>',
    );

    /* translators: %s: site name. */
    $body  = '<h2>' . esc_html( sprintf( __( 'New Inquiry from %s', 'odysseo-core' ), get_bloginfo( 'name' ) ) ) . '</h2>';
    $body .= '<p><strong>Name:</strong> ' . esc_html( $name ) . '</p>';
    $body .= '<p><strong>Email:</strong> ' . esc_html( $email ) . '</p>';
    $body .= '<p><strong>Phone:</strong> ' . esc_html( $phone ) . '</p>';
    $body .= '<p><strong>Subject:</strong> ' . esc_html( $subject ) . '</p>';
    $body .= '<p><strong>Message:</strong><br>' . nl2br( esc_html( $message ) ) . '</p>';

    $sent = wp_mail( $to, 'New Inquiry: ' . $subject, $body, $headers );

    if ( $sent ) {
        wp_send_json_success( array( 'message' => 'Thank you! Your message has been sent successfully.' ) );
    } else {
        wp_send_json_error( array( 'message' => 'Sorry, something went wrong. Please try again.' ) );
    }
}
add_action( 'wp_ajax_odysseo_contact', 'odysseo_handle_contact_form' );
add_action( 'wp_ajax_nopriv_odysseo_contact', 'odysseo_handle_contact_form' );
