<?php
/**
 * Falselogics Theme Support Module
 *
 * Drop this file into a theme (for example: /inc/falselogics-support/admin-support.php)
 * and require it from that theme's functions.php.
 */

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

if ( ! defined( 'FALSELOGICS_SUPPORT_EMAIL' ) ) {
	define( 'FALSELOGICS_SUPPORT_EMAIL', 'contact@falselogics.com' );
}

if ( ! defined( 'FALSELOGICS_SUPPORT_VERSION' ) ) {
	define( 'FALSELOGICS_SUPPORT_VERSION', '1.0.0' );
}

/**
 * Build asset URI depending on where this module lives.
 *
 * Expected default location in a theme:
 * /inc/falselogics-support/admin-support.php
 * /inc/falselogics-support/admin-support.js
 * /inc/falselogics-support/admin-support.css
 *
 * @param string $file Asset filename.
 * @return string
 */
function falselogics_support_asset_url( $file ) {
	if ( function_exists( 'get_theme_file_uri' ) ) {
		return get_theme_file_uri( '/inc/falselogics-support/' . ltrim( $file, '/' ) );
	}

	return '';
}

/**
 * Register support page under Appearance.
 */
function falselogics_support_register_page() {
	add_theme_page(
		esc_html__( 'Theme Support', 'digital-marketing-blog' ),
		esc_html__( 'Theme Support', 'digital-marketing-blog' ),
		'edit_theme_options',
		'falselogics-support',
		'falselogics_support_render_page'
	);
}
add_action( 'admin_menu', 'falselogics_support_register_page' );

/**
 * Enqueue support assets in wp-admin.
 */
function falselogics_support_enqueue_assets() {
	if ( ! current_user_can( 'edit_theme_options' ) ) {
		return;
	}

	wp_enqueue_style(
		'falselogics-admin-support',
		falselogics_support_asset_url( 'admin-support.css' ),
		array(),
		FALSELOGICS_SUPPORT_VERSION
	);

	wp_enqueue_script(
		'falselogics-admin-support',
		falselogics_support_asset_url( 'admin-support.js' ),
		array( 'jquery' ),
		FALSELOGICS_SUPPORT_VERSION,
		true
	);

	wp_localize_script(
		'falselogics-admin-support',
		'falselogicsSupport',
		array(
			'ajaxUrl'     => admin_url( 'admin-ajax.php' ),
			'nonce'       => wp_create_nonce( 'falselogics_support_nonce' ),
			'sendingText' => esc_html__( 'Sending...', 'digital-marketing-blog' ),
			'failedText'  => esc_html__( 'Failed to send. Please try again.', 'digital-marketing-blog' ),
		)
	);
}
add_action( 'admin_enqueue_scripts', 'falselogics_support_enqueue_assets' );

/**
 * Render support page.
 */
function falselogics_support_render_page() {
	?>
	<div class="wrap falselogics-support-page">
		<h1><?php esc_html_e( 'Theme Support', 'digital-marketing-blog' ); ?></h1>
		<div class="falselogics-support-intro">
			<p class="description">
				<?php esc_html_e( 'Need help or customization? Contact the author using the form below or email us directly.', 'digital-marketing-blog' ); ?>
			</p>
			<p class="falselogics-support-email">
				<strong><?php esc_html_e( 'Direct Email:', 'digital-marketing-blog' ); ?></strong>
				<a href="mailto:<?php echo esc_attr( FALSELOGICS_SUPPORT_EMAIL ); ?>">
					<?php echo esc_html( FALSELOGICS_SUPPORT_EMAIL ); ?>
				</a>
			</p>
			<button type="button" class="button button-primary falselogics-open-support">
				<?php esc_html_e( 'Contact Support', 'digital-marketing-blog' ); ?>
			</button>
		</div>
	</div>
	<?php
}

/**
 * Render global support launcher and modal in admin footer.
 */
function falselogics_support_render_modal() {
	if ( ! current_user_can( 'edit_theme_options' ) ) {
		return;
	}
	?>
	<div class="falselogics-support-launcher-wrap">
		<button type="button" class="falselogics-support-launcher falselogics-open-support" aria-label="<?php esc_attr_e( 'Open support form', 'digital-marketing-blog' ); ?>">
			<span class="dashicons dashicons-sos" aria-hidden="true"></span>
			<span><?php esc_html_e( 'Support', 'digital-marketing-blog' ); ?></span>
		</button>
	</div>

	<div id="falselogics-support-modal" class="falselogics-support-modal" aria-hidden="true">
		<div class="falselogics-support-modal__backdrop" data-falselogics-support-close></div>
		<div class="falselogics-support-modal__dialog" role="dialog" aria-modal="true" aria-labelledby="falselogics-support-title" aria-describedby="falselogics-support-help" tabindex="-1">
			<div class="falselogics-support-modal__header">
				<h2 id="falselogics-support-title"><?php esc_html_e( 'Contact Support', 'digital-marketing-blog' ); ?></h2>
				<button type="button" class="falselogics-support-modal__close" aria-label="<?php esc_attr_e( 'Close', 'digital-marketing-blog' ); ?>" data-falselogics-support-close>&times;</button>
			</div>
			<form id="falselogics-support-form">
				<div class="falselogics-support-modal__body">
					<p id="falselogics-support-help" class="description">
						<?php esc_html_e( 'Please include enough detail so we can reproduce and solve your issue quickly.', 'digital-marketing-blog' ); ?>
					</p>
					<div class="falselogics-support-grid">
						<div>
							<label for="falselogics-support-name"><?php esc_html_e( 'Name', 'digital-marketing-blog' ); ?></label>
							<input type="text" id="falselogics-support-name" name="name" class="regular-text" autocomplete="name" required>
						</div>
						<div>
							<label for="falselogics-support-email"><?php esc_html_e( 'Email', 'digital-marketing-blog' ); ?></label>
							<input type="email" id="falselogics-support-email" name="email" class="regular-text" autocomplete="email" required>
						</div>
					</div>

					<label for="falselogics-support-subject"><?php esc_html_e( 'Subject', 'digital-marketing-blog' ); ?></label>
					<input type="text" id="falselogics-support-subject" name="subject" class="regular-text" required>

					<label for="falselogics-support-message"><?php esc_html_e( 'Message', 'digital-marketing-blog' ); ?></label>
					<textarea id="falselogics-support-message" name="message" rows="6" class="large-text" required></textarea>

					<div class="falselogics-support-modal__status" id="falselogics-support-status" aria-live="polite" role="status"></div>
				</div>
				<div class="falselogics-support-modal__footer">
					<button type="submit" class="button button-primary" id="falselogics-support-submit">
						<?php esc_html_e( 'Send Message', 'digital-marketing-blog' ); ?>
					</button>
					<button type="button" class="button" data-falselogics-support-close>
						<?php esc_html_e( 'Cancel', 'digital-marketing-blog' ); ?>
					</button>
				</div>
			</form>
		</div>
	</div>
	<?php
}
add_action( 'admin_footer', 'falselogics_support_render_modal' );

/**
 * AJAX: Send support message.
 */
function falselogics_support_send_message() {
	check_ajax_referer( 'falselogics_support_nonce', 'nonce' );

	if ( ! current_user_can( 'edit_theme_options' ) ) {
		wp_send_json_error( array( 'message' => esc_html__( 'Insufficient permissions.', 'digital-marketing-blog' ) ), 403 );
	}

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

	if ( empty( $name ) || empty( $email ) || empty( $subject ) || empty( $message ) ) {
		wp_send_json_error( array( 'message' => esc_html__( 'All fields are required.', 'digital-marketing-blog' ) ), 400 );
	}
	if ( ! is_email( $email ) ) {
		wp_send_json_error( array( 'message' => esc_html__( 'Please enter a valid email address.', 'digital-marketing-blog' ) ), 400 );
	}

	$current_user = wp_get_current_user();
	$site_name    = wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES );
	$home_url = home_url( '/' );
	$theme        = wp_get_theme();
	$wp_version   = get_bloginfo( 'version' );
	$php_version  = PHP_VERSION;
	$locale       = get_locale();
	$admin_email  = get_option( 'admin_email' );

	$full_subject = sprintf( '[%s] %s', $site_name, $subject );
	$body         = "Name: {$name}\n";
	$body        .= "Email: {$email}\n";
	$body        .= "User: {$current_user->user_login}\n";
	$body        .= "Site: {$home_url}\n\n";
	$body        .= "Theme: {$theme->get( 'Name' )} {$theme->get( 'Version' )}\n";
	$body        .= "WordPress: {$wp_version}\n";
	$body        .= "PHP: {$php_version}\n";
	$body        .= "Locale: {$locale}\n";
	$body        .= "Admin Email: {$admin_email}\n\n";
	$body        .= "Message:\n{$message}\n";

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

	$sent = wp_mail( FALSELOGICS_SUPPORT_EMAIL, $full_subject, $body, $headers );

	if ( ! $sent ) {
		wp_send_json_error( array( 'message' => esc_html__( 'Failed to send. Please try again later.', 'digital-marketing-blog' ) ), 500 );
	}

	wp_send_json_success( array( 'message' => esc_html__( 'Message sent successfully.', 'digital-marketing-blog' ) ) );
}
add_action( 'wp_ajax_falselogics_support_send', 'falselogics_support_send_message' );
