<?php
/**
 * Project Synapse admin view.
 *
 * @package Noesia_Smart_Waiter
 *
 * @var Noesia_SWA_Project_Synapse_Service $service Service.
 */

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

$noesia_swa_pack_status     = $service->get_pack_status();
$noesia_swa_settings        = $service->get_settings_summary();
$noesia_swa_scenes          = $service->get_scene_manifest();
$noesia_swa_asset_urls      = isset( $noesia_swa_pack_status['asset_urls'] ) && is_array( $noesia_swa_pack_status['asset_urls'] ) ? $noesia_swa_pack_status['asset_urls'] : array();
$noesia_swa_installed       = ! empty( $noesia_swa_pack_status['installed'] );
$noesia_swa_model_options   = $service->get_supported_openai_models();
$noesia_swa_current_model   = isset( $noesia_swa_settings['openai_model'] ) ? (string) $noesia_swa_settings['openai_model'] : 'gpt-4.1-mini';
$noesia_swa_api_placeholder = ! empty( $noesia_swa_settings['openai_masked'] ) ? $noesia_swa_settings['openai_masked'] : __( 'Paste your OpenAI API key', 'noesia-smart-waiter' );
$noesia_swa_default_key     = isset( $noesia_swa_asset_urls['ask-synapse'] ) ? 'ask-synapse' : ( isset( $noesia_swa_asset_urls['christianity'] ) ? 'christianity' : ( isset( $noesia_swa_asset_urls['aether'] ) ? 'aether' : ( ! empty( $noesia_swa_scenes ) ? (string) array_key_first( $noesia_swa_scenes ) : 'aether' ) ) );
$noesia_swa_default_scene   = isset( $noesia_swa_asset_urls[ $noesia_swa_default_key ] ) ? $noesia_swa_asset_urls[ $noesia_swa_default_key ] : '';
$noesia_swa_default_data    = isset( $noesia_swa_scenes[ $noesia_swa_default_key ] ) ? $noesia_swa_scenes[ $noesia_swa_default_key ] : array();

$noesia_swa_scene_view = array(
	'ask-synapse'  => array(
		'icon'      => '?',
		'kicker'    => __( 'Ask Synapse', 'noesia-smart-waiter' ),
		'title'     => __( 'Ask anything with meaning.', 'noesia-smart-waiter' ),
		'question'  => __( 'Ask about life, faith, philosophy, grief, decisions, or the meaning behind a difficult moment.', 'noesia-smart-waiter' ),
		'microcopy' => __( 'General conversation. Sources when needed. Human tone always.', 'noesia-smart-waiter' ),
	),
	'christianity' => array(
		'icon'      => 'C',
		'kicker'    => __( 'Christianity Sanctuary', 'noesia-smart-waiter' ),
		'title'     => __( 'Ask with reverence. Receive with care.', 'noesia-smart-waiter' ),
		'question'  => __( 'What does Christ say about death and where souls go?', 'noesia-smart-waiter' ),
		'microcopy' => __( 'Gospel, prayer, faith, grief, and hope.', 'noesia-smart-waiter' ),
	),
	'stoicism'     => array(
		'icon'      => 'S',
		'kicker'    => __( 'Stoic Hall', 'noesia-smart-waiter' ),
		'title'     => __( 'Turn fear into judgment, action, and calm.', 'noesia-smart-waiter' ),
		'question'  => __( 'How would the Stoics help me face fear and loss?', 'noesia-smart-waiter' ),
		'microcopy' => __( 'Marcus, Epictetus, courage, discipline.', 'noesia-smart-waiter' ),
	),
	'buddhism'     => array(
		'icon'      => 'B',
		'kicker'    => __( 'Buddhist Sanctuary', 'noesia-smart-waiter' ),
		'title'     => __( 'Meet suffering without becoming it.', 'noesia-smart-waiter' ),
		'question'  => __( 'How can I suffer less without pretending I am not hurt?', 'noesia-smart-waiter' ),
		'microcopy' => __( 'Peace, suffering, attention, release.', 'noesia-smart-waiter' ),
	),
	'islam'        => array(
		'icon'      => 'I',
		'kicker'    => __( 'Islamic Reflection', 'noesia-smart-waiter' ),
		'title'     => __( 'Mercy, surrender, and disciplined hope.', 'noesia-smart-waiter' ),
		'question'  => __( 'How can faith help me when I feel abandoned?', 'noesia-smart-waiter' ),
		'microcopy' => __( 'Mercy, patience, prayer, trust.', 'noesia-smart-waiter' ),
	),
	'hinduism'     => array(
		'icon'      => 'H',
		'kicker'    => __( 'Gita Sanctuary', 'noesia-smart-waiter' ),
		'title'     => __( 'Duty, self, and the courage to act.', 'noesia-smart-waiter' ),
		'question'  => __( 'What should I do when I know my duty but I am afraid?', 'noesia-smart-waiter' ),
		'microcopy' => __( 'Gita, duty, self-knowledge.', 'noesia-smart-waiter' ),
	),
	'taoism'       => array(
		'icon'      => 'T',
		'kicker'    => __( 'Taoist Sanctuary', 'noesia-smart-waiter' ),
		'title'     => __( 'Return to flow when the mind becomes force.', 'noesia-smart-waiter' ),
		'question'  => __( 'What does Taoism say when I am forcing everything?', 'noesia-smart-waiter' ),
		'microcopy' => __( 'Balance, flow, stillness.', 'noesia-smart-waiter' ),
	),
	'aether'       => array(
		'icon'      => 'A',
		'kicker'    => __( 'AETHER - The Digital Phone of the Wind', 'noesia-smart-waiter' ),
		'title'     => __( 'Some words were never given a room.', 'noesia-smart-waiter' ),
		'question'  => __( 'There are words you never got to say. Write them here. No one will read them. They will become light and disappear.', 'noesia-smart-waiter' ),
		'microcopy' => __( 'Private ritual. No storage. No audience. Release only.', 'noesia-smart-waiter' ),
	),
);

$noesia_swa_default_view = isset( $noesia_swa_scene_view[ $noesia_swa_default_key ] ) ? $noesia_swa_scene_view[ $noesia_swa_default_key ] : array(
	'kicker'    => isset( $noesia_swa_default_data['label'] ) ? $noesia_swa_default_data['label'] : __( 'Project Synapse', 'noesia-smart-waiter' ),
	'title'     => __( 'Ask with meaning. Answer with care.', 'noesia-smart-waiter' ),
	'question'  => '',
	'microcopy' => __( 'Choose a world, write, and ask.', 'noesia-smart-waiter' ),
	'icon'      => 'N',
);

$noesia_swa_root_style = $noesia_swa_default_scene ? '--synapse-bg:url(' . esc_url( $noesia_swa_default_scene ) . ');' : '';
?>

<section
	class="noesia-swa-synapse"
	data-synapse-root
	data-installed="<?php echo $noesia_swa_installed ? '1' : '0'; ?>"
	data-current-scene="<?php echo esc_attr( $noesia_swa_default_key ); ?>"
	data-current-mode="<?php echo isset( $noesia_swa_default_data['mode'] ) ? esc_attr( $noesia_swa_default_data['mode'] ) : 'meaning-library'; ?>"
	style="<?php echo esc_attr( $noesia_swa_root_style ); ?>"
>
	<div class="noesia-swa-synapse__bg" aria-hidden="true"></div>
		<div class="noesia-swa-synapse__bg-soft" aria-hidden="true"></div>
		<div class="noesia-swa-synapse__aurora" aria-hidden="true"></div>
		<div class="noesia-swa-synapse__particles" aria-hidden="true"></div>
		<div class="noesia-swa-synapse__ambient" aria-hidden="true">
			<?php for ( $noesia_swa_i = 0; $noesia_swa_i < 28; $noesia_swa_i++ ) : ?>
				<span style="<?php echo esc_attr( '--x:' . ( 4 + ( $noesia_swa_i * 7 ) % 92 ) . '%;--y:' . ( 12 + ( $noesia_swa_i * 13 ) % 82 ) . '%;--s:' . ( 2 + ( $noesia_swa_i % 5 ) ) . 'px;--d:' . ( 8 + ( $noesia_swa_i % 9 ) ) . 's;--delay:-' . ( $noesia_swa_i % 11 ) . 's;' ); ?>"></span>
			<?php endfor; ?>
		</div>
		<div class="noesia-swa-synapse__motion" aria-hidden="true">
			<span></span>
			<span></span>
		<span></span>
		<span></span>
		<span></span>
		<span></span>
	</div>
	<div class="noesia-swa-synapse__release-field" data-synapse-release-field aria-hidden="true"></div>

	<header class="noesia-swa-synapse__topbar">
		<div class="noesia-swa-synapse__brand">
			<div class="noesia-swa-synapse__brand-mark">N</div>
			<div>
				<strong><?php esc_html_e( 'Project Synapse', 'noesia-smart-waiter' ); ?></strong>
				<span><?php esc_html_e( 'The human layer of Noesia Matrix OS', 'noesia-smart-waiter' ); ?></span>
			</div>
		</div>

		<div class="noesia-swa-synapse__top-actions">
			<span class="noesia-swa-synapse__pill <?php echo $noesia_swa_installed ? 'is-ready' : 'is-waiting'; ?>" data-synapse-pack-pill>
				<?php echo $noesia_swa_installed ? esc_html__( 'Knowledge ready', 'noesia-smart-waiter' ) : esc_html__( 'Install pack', 'noesia-smart-waiter' ); ?>
			</span>
			<span class="noesia-swa-synapse__pill <?php echo ! empty( $noesia_swa_settings['has_openai_key'] ) ? 'is-ready' : 'is-waiting'; ?>" data-synapse-openai-pill>
				<?php echo ! empty( $noesia_swa_settings['has_openai_key'] ) ? esc_html__( 'AI ready', 'noesia-smart-waiter' ) : esc_html__( 'API key needed', 'noesia-smart-waiter' ); ?>
			</span>
			<button type="button" class="noesia-swa-synapse__icon-button" data-synapse-open-setup aria-label="<?php esc_attr_e( 'Open Synapse settings', 'noesia-smart-waiter' ); ?>"><?php esc_html_e( 'SET', 'noesia-smart-waiter' ); ?></button>
			<button type="button" class="noesia-swa-synapse__primary is-compact" data-synapse-open-aether><?php esc_html_e( 'AETHER', 'noesia-smart-waiter' ); ?></button>
		</div>
	</header>

	<main class="noesia-swa-synapse__stage">
		<button type="button" class="noesia-swa-synapse__focus-exit" data-synapse-focus-exit><?php esc_html_e( 'Exit Focus', 'noesia-smart-waiter' ); ?></button>

		<div class="noesia-swa-synapse__hero-copy">
			<span class="noesia-swa-synapse__kicker" data-synapse-stage-kicker><?php echo esc_html( $noesia_swa_default_view['kicker'] ); ?></span>
			<h2 data-synapse-stage-title><?php echo esc_html( $noesia_swa_default_view['title'] ); ?></h2>
			<p data-synapse-stage-description><?php echo isset( $noesia_swa_default_data['description'] ) ? esc_html( $noesia_swa_default_data['description'] ) : esc_html__( 'Choose a world, ask with care, and receive a grounded answer.', 'noesia-smart-waiter' ); ?></p>
		</div>

		<div class="noesia-swa-synapse__query-box" data-synapse-query-box>
			<textarea data-synapse-question rows="5" placeholder="<?php echo esc_attr( $noesia_swa_default_view['question'] ); ?>"></textarea>
			<div class="noesia-swa-synapse__query-actions">
				<span class="noesia-swa-synapse__mini-note" data-synapse-mini-note><?php echo esc_html( $noesia_swa_default_view['microcopy'] ); ?></span>
				<div class="noesia-swa-synapse__button-row">
					<button type="button" class="noesia-swa-synapse__ghost" data-synapse-release><?php esc_html_e( 'Release', 'noesia-smart-waiter' ); ?></button>
					<button type="button" class="noesia-swa-synapse__primary" data-synapse-ask><?php esc_html_e( 'Ask Synapse', 'noesia-smart-waiter' ); ?></button>
				</div>
			</div>
			<div class="noesia-swa-synapse__inline-answer" data-synapse-answer-wrap aria-hidden="true">
				<span><?php esc_html_e( 'Synapse', 'noesia-smart-waiter' ); ?></span>
				<div data-synapse-answer></div>
			</div>
		</div>
	</main>

	<nav class="noesia-swa-synapse__worlds" aria-label="<?php esc_attr_e( 'Project Synapse worlds', 'noesia-smart-waiter' ); ?>">
		<div class="noesia-swa-synapse__scene-list">
			<?php foreach ( $noesia_swa_scenes as $noesia_swa_scene_key => $noesia_swa_scene ) : ?>
				<?php
				$noesia_swa_image_url = isset( $noesia_swa_asset_urls[ $noesia_swa_scene_key ] ) ? $noesia_swa_asset_urls[ $noesia_swa_scene_key ] : ( isset( $noesia_swa_asset_urls['aether'] ) ? $noesia_swa_asset_urls['aether'] : '' );
				$noesia_swa_view      = isset( $noesia_swa_scene_view[ $noesia_swa_scene_key ] ) ? $noesia_swa_scene_view[ $noesia_swa_scene_key ] : array(
					'icon'      => strtoupper( substr( (string) $noesia_swa_scene['label'], 0, 1 ) ),
					'kicker'    => isset( $noesia_swa_scene['label'] ) ? $noesia_swa_scene['label'] : '',
					'title'     => isset( $noesia_swa_scene['label'] ) ? $noesia_swa_scene['label'] : '',
					'question'  => '',
					'microcopy' => isset( $noesia_swa_scene['description'] ) ? $noesia_swa_scene['description'] : '',
				);
				?>
				<button
					type="button"
					class="noesia-swa-synapse__scene-btn<?php echo $noesia_swa_default_key === $noesia_swa_scene_key ? ' is-active' : ''; ?>"
					data-synapse-scene
					data-synapse-scene-key="<?php echo esc_attr( $noesia_swa_scene_key ); ?>"
					data-synapse-scene-title="<?php echo esc_attr( $noesia_swa_view['title'] ); ?>"
					data-synapse-scene-kicker="<?php echo esc_attr( $noesia_swa_view['kicker'] ); ?>"
					data-synapse-scene-mode="<?php echo esc_attr( $noesia_swa_scene['mode'] ); ?>"
					data-synapse-scene-description="<?php echo esc_attr( $noesia_swa_scene['description'] ); ?>"
					data-synapse-scene-question="<?php echo esc_attr( $noesia_swa_view['question'] ); ?>"
					data-synapse-scene-microcopy="<?php echo esc_attr( $noesia_swa_view['microcopy'] ); ?>"
					data-synapse-scene-image="<?php echo esc_url( $noesia_swa_image_url ); ?>"
				>
					<span class="noesia-swa-synapse__scene-icon"><?php echo esc_html( $noesia_swa_view['icon'] ); ?></span>
					<span>
						<strong><?php echo esc_html( $noesia_swa_scene['label'] ); ?></strong>
						<em><?php echo esc_html( $noesia_swa_view['microcopy'] ); ?></em>
					</span>
				</button>
			<?php endforeach; ?>
		</div>
	</nav>

	<div class="noesia-swa-synapse__setup-panel" data-synapse-setup aria-hidden="true">
		<div class="noesia-swa-synapse__setup-card">
			<button type="button" class="noesia-swa-synapse__setup-close" data-synapse-close-setup aria-label="<?php esc_attr_e( 'Close setup', 'noesia-smart-waiter' ); ?>">×</button>
			<div class="noesia-swa-synapse__panel-head">
				<span class="noesia-swa-synapse__kicker"><?php esc_html_e( 'Setup', 'noesia-smart-waiter' ); ?></span>
				<h3><?php esc_html_e( 'Project Synapse Core', 'noesia-smart-waiter' ); ?></h3>
				<p><?php esc_html_e( 'Install the knowledge pack and save the OpenAI key used by this WordPress site.', 'noesia-smart-waiter' ); ?></p>
			</div>

			<div class="noesia-swa-synapse__setup-status">
				<div>
					<strong><?php esc_html_e( 'Knowledge Pack', 'noesia-smart-waiter' ); ?></strong>
					<span><?php echo $noesia_swa_installed ? esc_html__( 'Installed and ready', 'noesia-smart-waiter' ) : esc_html__( 'Not installed yet', 'noesia-smart-waiter' ); ?></span>
				</div>
				<div>
					<strong><?php esc_html_e( 'AI Connection', 'noesia-smart-waiter' ); ?></strong>
					<span data-synapse-openai-status-text><?php echo ! empty( $noesia_swa_settings['has_openai_key'] ) ? esc_html__( 'OpenAI key saved', 'noesia-smart-waiter' ) : esc_html__( 'OpenAI key required', 'noesia-smart-waiter' ); ?></span>
				</div>
			</div>

			<button type="button" class="<?php echo $noesia_swa_installed ? 'noesia-swa-synapse__secondary' : 'noesia-swa-synapse__primary'; ?>" data-synapse-install>
				<?php echo $noesia_swa_installed ? esc_html__( 'Refresh Knowledge Pack', 'noesia-smart-waiter' ) : esc_html__( 'Install Knowledge Pack', 'noesia-smart-waiter' ); ?>
			</button>
			<div class="noesia-swa-synapse__install-status" data-synapse-install-status aria-live="polite"></div>
			<p class="noesia-swa-synapse__hint">
				<?php echo $noesia_swa_installed ? esc_html__( 'The Synapse knowledge pack is installed locally on this WordPress site.', 'noesia-smart-waiter' ) : esc_html__( 'Downloads the public Synapse knowledge pack from noesia.io into this WordPress uploads folder.', 'noesia-smart-waiter' ); ?>
			</p>

			<div class="noesia-swa-synapse__settings">
				<label for="noesia-swa-project-synapse-openai-key"><?php esc_html_e( 'OpenAI API Key', 'noesia-smart-waiter' ); ?></label>
				<input
					type="password"
					id="noesia-swa-project-synapse-openai-key"
					name="noesia_swa_options[<?php echo esc_attr( Noesia_SWA_Project_Synapse_Service::OPTION_OPENAI_API_KEY ); ?>]"
					value=""
					placeholder="<?php echo esc_attr( $noesia_swa_api_placeholder ); ?>"
					autocomplete="off"
					data-synapse-openai-key
				/>
				<p><?php esc_html_e( 'Leave blank to keep the saved key. Paste a new key only when you want to replace it.', 'noesia-smart-waiter' ); ?></p>

				<div class="noesia-swa-synapse__key-guide">
					<strong><?php esc_html_e( 'Where do I get the API key?', 'noesia-smart-waiter' ); ?></strong>
					<ol>
						<li><?php esc_html_e( 'Open the official OpenAI API Keys page.', 'noesia-smart-waiter' ); ?></li>
						<li><?php esc_html_e( 'Sign in, create a new secret key, and copy it once.', 'noesia-smart-waiter' ); ?></li>
						<li><?php esc_html_e( 'Paste the key here and press Save Synapse Settings.', 'noesia-smart-waiter' ); ?></li>
					</ol>
					<a href="https://platform.openai.com/api-keys" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Open OpenAI API Keys', 'noesia-smart-waiter' ); ?></a>
					<a href="https://platform.openai.com/settings/organization/billing/overview" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'OpenAI billing', 'noesia-smart-waiter' ); ?></a>
				</div>

				<label for="noesia-swa-project-synapse-openai-model"><?php esc_html_e( 'AI Model', 'noesia-smart-waiter' ); ?></label>
				<select id="noesia-swa-project-synapse-openai-model" name="noesia_swa_options[<?php echo esc_attr( Noesia_SWA_Project_Synapse_Service::OPTION_OPENAI_MODEL ); ?>]" data-synapse-openai-model>
					<?php foreach ( $noesia_swa_model_options as $noesia_swa_model => $noesia_swa_label ) : ?>
						<option value="<?php echo esc_attr( $noesia_swa_model ); ?>" <?php selected( $noesia_swa_current_model, $noesia_swa_model ); ?>><?php echo esc_html( $noesia_swa_label ); ?></option>
					<?php endforeach; ?>
				</select>

				<button type="button" class="noesia-swa-synapse__primary" data-synapse-save-settings><?php esc_html_e( 'Save Synapse Settings', 'noesia-smart-waiter' ); ?></button>
				<div class="noesia-swa-synapse__install-status" data-synapse-settings-status aria-live="polite"></div>
			</div>
		</div>
	</div>
</section>
