<?php
/**
 * Plugin Name: OurNetHelps Mortgage Q&A Assistant
 * Plugin URI:  https://ournethelps.com/mortgage-qa-assistant/
 * Description: Mortgage chat widget for brokers with NMLS, broker profile, quick questions, and unlimited custom Q&A.
 * Version:     1.0.0
 * Author:      Sanjeev Kumar
 * Author URI:  https://ournethelps.com/
 * License:     GPL-2.0+
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: ournethelps-mortgage-qa-assistant
 * Domain Path: /languages
 * Requires at least: 5.0
 * Requires PHP:      7.4
 */

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

define( 'ONHMQA_VERSION',     '1.0.0' );
define( 'ONHMQA_PLUGIN_DIR',  plugin_dir_path( __FILE__ ) );
define( 'ONHMQA_PLUGIN_URL',  plugin_dir_url( __FILE__ ) );
define( 'ONHMQA_PLUGIN_FILE', __FILE__ );

// ── Activation ────────────────────────────────────────────────────────────────

register_activation_hook( __FILE__, 'onhmqa_activate' );
function onhmqa_activate() {
	if ( false === get_option( 'onhmqa_enabled' )        ) update_option( 'onhmqa_enabled',       '1' );
	if ( false === get_option( 'onhmqa_popup_enabled' )  ) update_option( 'onhmqa_popup_enabled', '1' );
	if ( false === get_option( 'onhmqa_popup_text' )     ) update_option( 'onhmqa_popup_text',    'Have mortgage questions?' );
	if ( false === get_option( 'onhmqa_popup_delay' )    ) update_option( 'onhmqa_popup_delay',   '6' );
}

register_deactivation_hook( __FILE__, 'onhmqa_deactivate' );
function onhmqa_deactivate() {}

// ── Load includes ─────────────────────────────────────────────────────────────

require_once ONHMQA_PLUGIN_DIR . 'includes/admin-settings.php';
require_once ONHMQA_PLUGIN_DIR . 'includes/widget-output.php';

// ── Register settings ─────────────────────────────────────────────────────────

add_action( 'admin_init', 'onhmqa_register_settings' );
function onhmqa_register_settings() {

	// General
	register_setting( 'onhmqa_group', 'onhmqa_enabled',       array( 'sanitize_callback' => 'sanitize_text_field',     'default' => '1' ) );
	register_setting( 'onhmqa_group', 'onhmqa_position',      array( 'sanitize_callback' => 'sanitize_text_field',     'default' => 'bottom-right' ) );
	register_setting( 'onhmqa_group', 'onhmqa_accent',        array( 'sanitize_callback' => 'sanitize_hex_color',      'default' => '#1a56db' ) );
	register_setting( 'onhmqa_group', 'onhmqa_welcome',       array( 'sanitize_callback' => 'sanitize_textarea_field', 'default' => 'Hi! Ask me anything about mortgages. Tap a question below or type your own.' ) );
	register_setting( 'onhmqa_group', 'onhmqa_urgency',       array( 'sanitize_callback' => 'sanitize_text_field',     'default' => '' ) );
	register_setting( 'onhmqa_group', 'onhmqa_exclude_pages', array( 'sanitize_callback' => 'sanitize_textarea_field', 'default' => '' ) );
	register_setting( 'onhmqa_group', 'onhmqa_fallback_msg',  array( 'sanitize_callback' => 'sanitize_textarea_field', 'default' => 'Great question. Please call me directly or use the contact form and I will get back to you as soon as possible.' ) );
	register_setting( 'onhmqa_group', 'onhmqa_contact_url',   array( 'sanitize_callback' => 'esc_url_raw',             'default' => '' ) );

	// Popup
	register_setting( 'onhmqa_group', 'onhmqa_popup_enabled', array( 'sanitize_callback' => 'sanitize_text_field', 'default' => '1' ) );
	register_setting( 'onhmqa_group', 'onhmqa_popup_text',    array( 'sanitize_callback' => 'sanitize_text_field', 'default' => 'Have mortgage questions?' ) );
	register_setting( 'onhmqa_group', 'onhmqa_popup_delay',   array( 'sanitize_callback' => 'absint',              'default' => '6' ) );

	// Broker profile
	register_setting( 'onhmqa_group', 'onhmqa_agent_name',      array( 'sanitize_callback' => 'sanitize_text_field', 'default' => '' ) );
	register_setting( 'onhmqa_group', 'onhmqa_agent_title',     array( 'sanitize_callback' => 'sanitize_text_field', 'default' => 'Mortgage Broker' ) );
	register_setting( 'onhmqa_group', 'onhmqa_agent_license',   array( 'sanitize_callback' => 'sanitize_text_field', 'default' => '' ) );
	register_setting( 'onhmqa_group', 'onhmqa_agent_phone',     array( 'sanitize_callback' => 'sanitize_text_field', 'default' => '' ) );
	register_setting( 'onhmqa_group', 'onhmqa_agent_email',     array( 'sanitize_callback' => 'sanitize_email',      'default' => '' ) );
	register_setting( 'onhmqa_group', 'onhmqa_agent_photo_url', array( 'sanitize_callback' => 'esc_url_raw',         'default' => '' ) );

	// Q&A
	register_setting( 'onhmqa_group', 'onhmqa_qa_pairs', array( 'sanitize_callback' => 'onhmqa_sanitize_qa_pairs', 'default' => '' ) );

	// Branding (opt-in, off by default)
	register_setting( 'onhmqa_group', 'onhmqa_show_branding', array( 'sanitize_callback' => 'sanitize_text_field', 'default' => '0' ) );
}

// ── Sanitize Q&A pairs ────────────────────────────────────────────────────────

function onhmqa_sanitize_qa_pairs( $raw ) {
	if ( is_string( $raw ) ) {
		$raw = json_decode( $raw, true );
	}
	if ( ! is_array( $raw ) ) return '';
	$clean = array();
	foreach ( $raw as $pair ) {
		if ( ! is_array( $pair ) ) continue;
		$question = sanitize_text_field( wp_unslash( $pair['question'] ?? '' ) );
		if ( empty( $question ) ) continue;
		$clean[] = array(
			'question' => $question,
			'keywords' => sanitize_text_field( wp_unslash( $pair['keywords'] ?? '' ) ),
			'answer'   => wp_kses( wp_unslash( $pair['answer'] ?? '' ), array(
				'a'      => array( 'href' => array(), 'target' => array(), 'rel' => array() ),
				'b'      => array(),
				'strong' => array(),
				'br'     => array(),
			) ),
		);
	}
	return wp_json_encode( $clean );
}
