<?php
/**
 * Built-in forms – contact (all tiers), booking & quote (bundle).
 *
 * @package EasySite_Pro
 */

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

/**
 * Render contact form shortcode.
 *
 * @return string
 */
function easysite_contact_form_shortcode() {
	ob_start();
	easysite_render_form( 'contact' );
	return ob_get_clean();
}
add_shortcode( 'easysite_contact_form', 'easysite_contact_form_shortcode' );

/**
 * Booking form shortcode (bundle).
 */
function easysite_booking_form_shortcode() {
	if ( ! easysite_is_bundle() ) {
		return '';
	}
	ob_start();
	easysite_render_form( 'booking' );
	return ob_get_clean();
}
add_shortcode( 'easysite_booking_form', 'easysite_booking_form_shortcode' );

/**
 * Quote form shortcode (bundle).
 */
function easysite_quote_form_shortcode() {
	if ( ! easysite_is_bundle() ) {
		return '';
	}
	ob_start();
	easysite_render_form( 'quote' );
	return ob_get_clean();
}
add_shortcode( 'easysite_quote_form', 'easysite_quote_form_shortcode' );

/**
 * Render a form by type.
 *
 * @param string $type Form type.
 */
function easysite_render_form( $type = 'contact' ) {
	$success = isset( $_GET['easysite_sent'] ) && '1' === $_GET['easysite_sent']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
	$error   = isset( $_GET['easysite_error'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
	$label_name    = easysite_get_mod( 'easysite_form_label_name', __( 'Your name', 'easysite-pro' ) );
	$label_email   = easysite_get_mod( 'easysite_form_label_email', __( 'Email', 'easysite-pro' ) );
	$label_message = easysite_get_mod( 'easysite_form_label_message', __( 'Message', 'easysite-pro' ) );
	$label_submit  = easysite_get_mod( 'easysite_form_label_submit', __( 'Send Message', 'easysite-pro' ) );
	$ve            = easysite_ve_active();
	?>
	<div class="easysite-form-wrap">
		<?php if ( $success ) : ?>
			<p class="easysite-form-notice easysite-success"><?php esc_html_e( 'Thank you! Your message has been sent.', 'easysite-pro' ); ?></p>
		<?php elseif ( $error ) : ?>
			<p class="easysite-form-notice easysite-error"><?php esc_html_e( 'Please fill in all required fields correctly.', 'easysite-pro' ); ?></p>
		<?php endif; ?>

		<form class="easysite-form easysite-form-<?php echo esc_attr( $type ); ?>" method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
			<?php wp_nonce_field( 'easysite_form_' . $type, 'easysite_form_nonce' ); ?>
			<input type="hidden" name="action" value="easysite_submit_form" />
			<input type="hidden" name="form_type" value="<?php echo esc_attr( $type ); ?>" />
			<input type="hidden" name="redirect_to" value="<?php echo esc_url( get_permalink() ); ?>" />

			<p>
				<label for="easysite_name"<?php echo $ve ? ' contenteditable="true" data-esb-region-field="form_label_name" data-esb-type="text"' : ''; ?>><?php echo esc_html( $label_name ); ?> <span class="required">*</span></label>
				<input type="text" name="easysite_name" id="easysite_name" required />
			</p>
			<p>
				<label for="easysite_email"<?php echo $ve ? ' contenteditable="true" data-esb-region-field="form_label_email" data-esb-type="text"' : ''; ?>><?php echo esc_html( $label_email ); ?> <span class="required">*</span></label>
				<input type="email" name="easysite_email" id="easysite_email" required />
			</p>

			<?php if ( in_array( $type, array( 'booking', 'quote' ), true ) ) : ?>
				<p>
					<label for="easysite_phone"><?php esc_html_e( 'Phone', 'easysite-pro' ); ?></label>
					<input type="tel" name="easysite_phone" id="easysite_phone" />
				</p>
			<?php endif; ?>

			<?php if ( 'booking' === $type ) : ?>
				<p>
					<label for="easysite_date"><?php esc_html_e( 'Preferred date/time', 'easysite-pro' ); ?></label>
					<input type="text" name="easysite_date" id="easysite_date" placeholder="<?php esc_attr_e( 'e.g. Monday 2pm', 'easysite-pro' ); ?>" />
				</p>
				<p>
					<label for="easysite_service"><?php esc_html_e( 'Service type', 'easysite-pro' ); ?></label>
					<select name="easysite_service" id="easysite_service">
						<option value="consultation"><?php esc_html_e( 'Consultation', 'easysite-pro' ); ?></option>
						<option value="standard"><?php esc_html_e( 'Standard service', 'easysite-pro' ); ?></option>
						<option value="premium"><?php esc_html_e( 'Premium service', 'easysite-pro' ); ?></option>
					</select>
				</p>
			<?php endif; ?>

			<?php if ( 'quote' === $type ) : ?>
				<p>
					<label for="easysite_project"><?php esc_html_e( 'Project type', 'easysite-pro' ); ?></label>
					<input type="text" name="easysite_project" id="easysite_project" />
				</p>
				<p>
					<label for="easysite_budget"><?php esc_html_e( 'Budget range', 'easysite-pro' ); ?></label>
					<select name="easysite_budget" id="easysite_budget">
						<option value="under-500"><?php esc_html_e( 'Under £500', 'easysite-pro' ); ?></option>
						<option value="500-2000"><?php esc_html_e( '£500 – £2,000', 'easysite-pro' ); ?></option>
						<option value="2000-plus"><?php esc_html_e( '£2,000+', 'easysite-pro' ); ?></option>
					</select>
				</p>
			<?php endif; ?>

			<p>
				<label for="easysite_message"<?php echo $ve ? ' contenteditable="true" data-esb-region-field="form_label_message" data-esb-type="text"' : ''; ?>><?php echo esc_html( $label_message ); ?> <span class="required">*</span></label>
				<textarea name="easysite_message" id="easysite_message" rows="5" required></textarea>
			</p>

			<p><button type="submit" class="easysite-btn"<?php echo $ve ? ' contenteditable="true" data-esb-region-field="form_label_submit" data-esb-type="text"' : ''; ?>><?php echo esc_html( $label_submit ); ?></button></p>
		</form>
	</div>
	<?php
}

/**
 * Handle form submission.
 */
function easysite_handle_form_submit() {
	$type = isset( $_POST['form_type'] ) ? sanitize_key( wp_unslash( $_POST['form_type'] ) ) : 'contact';

	if ( ! isset( $_POST['easysite_form_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['easysite_form_nonce'] ) ), 'easysite_form_' . $type ) ) {
		wp_die( esc_html__( 'Security check failed.', 'easysite-pro' ) );
	}

	if ( in_array( $type, array( 'booking', 'quote' ), true ) && ! easysite_is_bundle() ) {
		wp_die( esc_html__( 'This form is not available.', 'easysite-pro' ) );
	}

	$name    = isset( $_POST['easysite_name'] ) ? sanitize_text_field( wp_unslash( $_POST['easysite_name'] ) ) : '';
	$email   = isset( $_POST['easysite_email'] ) ? sanitize_email( wp_unslash( $_POST['easysite_email'] ) ) : '';
	$message = isset( $_POST['easysite_message'] ) ? sanitize_textarea_field( wp_unslash( $_POST['easysite_message'] ) ) : '';
	$redirect = isset( $_POST['redirect_to'] ) ? esc_url_raw( wp_unslash( $_POST['redirect_to'] ) ) : home_url( '/' );

	if ( ! $name || ! is_email( $email ) || ! $message ) {
		wp_safe_redirect( add_query_arg( 'easysite_error', '1', $redirect ) );
		exit;
	}

	$subject = sprintf( '[%s] %s form submission', get_bloginfo( 'name' ), ucfirst( $type ) );
	$body    = "Name: {$name}\nEmail: {$email}\n\n{$message}\n";

	if ( 'booking' === $type ) {
		$body .= "\nDate/Time: " . sanitize_text_field( wp_unslash( $_POST['easysite_date'] ?? '' ) );
		$body .= "\nService: " . sanitize_text_field( wp_unslash( $_POST['easysite_service'] ?? '' ) );
	}
	if ( 'quote' === $type ) {
		$body .= "\nProject: " . sanitize_text_field( wp_unslash( $_POST['easysite_project'] ?? '' ) );
		$body .= "\nBudget: " . sanitize_text_field( wp_unslash( $_POST['easysite_budget'] ?? '' ) );
	}

	$admin_email = get_option( 'admin_email' );
	$headers     = array( 'Content-Type: text/plain; charset=UTF-8', 'Reply-To: ' . $name . ' <' . $email . '>' );

	$sent = wp_mail( $admin_email, $subject, $body, $headers );

	wp_safe_redirect( add_query_arg( 'easysite_sent', $sent ? '1' : '0', $redirect ) );
	exit;
}
add_action( 'admin_post_nopriv_easysite_submit_form', 'easysite_handle_form_submit' );
add_action( 'admin_post_easysite_submit_form', 'easysite_handle_form_submit' );
