<?php
/**
 * Truebe "Getting Started" admin page. First menu item. Orients a new trial user:
 * the problem, the solution, and the action.
 */

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

class Truebe_Admin_Welcome {

	const MENU_SLUG = 'truebe-welcome';

	public static function init() {
		add_action( 'admin_menu', array( __CLASS__, 'add_menu' ), 10 );
	}

	public static function add_menu() {
		add_submenu_page(
			'truebe',
			__( 'Getting Started', 'truebe-fact-based-ai-chatbot' ),
			__( 'Getting Started', 'truebe-fact-based-ai-chatbot' ),
			'manage_options',
			self::MENU_SLUG,
			array( __CLASS__, 'render' )
		);
	}

	public static function render() {
		if ( ! current_user_can( 'manage_options' ) ) { return; }
		$ready      = ( class_exists( 'Truebe_Admin_Upload' ) && method_exists( 'Truebe_Admin_Upload', 'keys_ready' ) ) ? Truebe_Admin_Upload::keys_ready() : false;
		$upload_url = $ready ? admin_url( 'admin.php?page=truebe-upload' ) : admin_url( 'admin.php?page=truebe' );

		$current_user  = wp_get_current_user();
		$first_name    = ! empty( $current_user->first_name ) ? $current_user->first_name : $current_user->display_name;
		$hour          = (int) wp_date( 'G' );
		if ( $hour >= 5 && $hour < 12 ) {
			$greeting = '🚀 Good morning';
		} elseif ( $hour >= 12 && $hour < 17 ) {
			$greeting = '☀️ Good afternoon';
		} else {
			$greeting = '🦉 Good evening';
		}
		?>
		<div class="wrap truebe-settings">
			<h1><?php esc_html_e( 'Getting started', 'truebe-fact-based-ai-chatbot' ); ?></h1>
			<div class="truebe-welcome">
				<span class="truebe-welcome-greeting"><strong><?php echo esc_html( $greeting ); ?></strong><?php echo esc_html( ', ' . $first_name . '.' ); ?></span>
			</div>

			<div class="truebe-card">
				<div class="truebe-field">
					<span class="truebe-label" style="color:#0f6e56;"><?php esc_html_e( 'Confident. Wrong. Liable.', 'truebe-fact-based-ai-chatbot' ); ?></span>
					<p class="truebe-gs-lead">
						<?php
						/* translators: %1$s = Charlotin case-database link, %2$s = Stanford RegLab study link */
						$liability_copy = __( 'Companies and professionals in regulated industries are facing <a href="%1$s" target="_blank" rel="noopener">sanctions and lawsuits</a> for trusting AI. Regulators are enforcing tighter standards on what AI can say without verifiable proof. Even the tools built to prevent it still <a href="%2$s" target="_blank" rel="noopener">hallucinate 17&ndash;33%% of the time</a>. That is a liability.', 'truebe-fact-based-ai-chatbot' );
						printf(
							wp_kses(
								$liability_copy,
								array( 'a' => array( 'href' => array(), 'target' => array(), 'rel' => array() ) )
							),
							esc_url( 'https://www.damiencharlotin.com/hallucinations/' ),
							esc_url( 'https://dho.stanford.edu/wp-content/uploads/Legal_RAG_Hallucinations.pdf' )
						);
						?>
					</p>
				</div>

				<div class="truebe-field" style="border-bottom:none;margin-bottom:0;padding-bottom:0;">
					<span class="truebe-label" style="color:#0f6e56;"><?php esc_html_e( 'AI that can\'t make things up', 'truebe-fact-based-ai-chatbot' ); ?></span>
					<p class="truebe-gs-lead" style="margin-bottom:0;"><?php esc_html_e( 'Truebe.io is different. Upload your documents and Q&A pairs are drafted for you to review and approve. Paste one shortcode to place Truebe anywhere on your site. Truebe now only responds using your facts. Anything it can\'t answer gets logged so it can be answered the next time. Every approval and answer is recorded in an immutable audit trail.', 'truebe-fact-based-ai-chatbot' ); ?></p>
				</div>
			</div>

			<div class="truebe-save-row">
				<a href="<?php echo esc_url( $upload_url ); ?>" class="truebe-btn truebe-save"><?php esc_html_e( 'Try it', 'truebe-fact-based-ai-chatbot' ); ?></a>
			</div>
		</div>
		<?php
	}
}