<?php
/**
 * Project Synapse module.
 *
 * @package Noesia_Smart_Waiter
 */

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

if ( ! class_exists( 'Noesia_Swa_Module_Project_Synapse' ) ) {
	/**
	 * Free Project Synapse module.
	 */
	class Noesia_Swa_Module_Project_Synapse implements Noesia_SWA_Module_Interface {

		/**
		 * Service instance.
		 *
		 * @var Noesia_SWA_Project_Synapse_Service|null
		 */
		private $service = null;

		/**
		 * Ajax instance.
		 *
		 * @var Noesia_SWA_Project_Synapse_Ajax|null
		 */
		private $ajax = null;

		/**
		 * Get module id.
		 *
		 * @return string
		 */
		public function get_id() {
			return 'project-synapse';
		}

		/**
		 * Get module label.
		 *
		 * @return string
		 */
		public function get_label() {
			return __( 'Project Synapse', 'noesia-smart-waiter' );
		}

		/**
		 * Get module tier.
		 *
		 * @return string
		 */
		public function get_tier() {
			return 'free';
		}

		/**
		 * Whether the module is enabled.
		 *
		 * @return bool
		 */
		public function is_enabled() {
			return (bool) apply_filters( 'noesia_swa_module_project_synapse_enabled', true, $this );
		}

		/**
		 * Register hooks.
		 *
		 * @return void
		 */
		public function register_hooks() {
			$this->boot_dependencies();

			if ( ! $this->service instanceof Noesia_SWA_Project_Synapse_Service ) {
				return;
			}

			add_action( 'noesia_swa_register_admin_sections', array( $this, 'register_admin_section' ) );
			add_filter( 'noesia_swa_option_defaults', array( $this, 'filter_option_defaults' ), 10, 3 );
			add_filter( 'noesia_swa_sanitize_options', array( $this, 'sanitize_options' ), 10, 4 );
			add_filter( 'noesia_swa_localized_options', array( $this, 'filter_localized_options' ), 10, 2 );
			add_filter( 'noesia_swa_admin_strings', array( $this, 'filter_admin_strings' ), 10, 2 );

			if ( $this->ajax instanceof Noesia_SWA_Project_Synapse_Ajax ) {
				$this->ajax->register_hooks();
			}
		}

		/**
		 * Boot dependencies.
		 *
		 * @return void
		 */
		private function boot_dependencies() {
			$files = array(
				'Noesia_SWA_Project_Synapse_Service' => __DIR__ . '/includes/class-noesia-swa-project-synapse-service.php',
				'Noesia_SWA_Project_Synapse_Ajax'    => __DIR__ . '/includes/class-noesia-swa-project-synapse-ajax.php',
			);

			foreach ( $files as $class_name => $file ) {
				if ( ! class_exists( $class_name ) && file_exists( $file ) ) {
					require_once $file;
				}
			}

			if ( ! $this->service instanceof Noesia_SWA_Project_Synapse_Service && class_exists( 'Noesia_SWA_Project_Synapse_Service' ) ) {
				$this->service = new Noesia_SWA_Project_Synapse_Service();
			}

			if ( ! $this->ajax instanceof Noesia_SWA_Project_Synapse_Ajax && $this->service instanceof Noesia_SWA_Project_Synapse_Service && class_exists( 'Noesia_SWA_Project_Synapse_Ajax' ) ) {
				$this->ajax = new Noesia_SWA_Project_Synapse_Ajax( $this->service );
			}
		}

		/**
		 * Register admin section.
		 *
		 * @param mixed $registry Admin section registry.
		 * @return void
		 */
		public function register_admin_section( $registry ) {
			if ( ! $registry instanceof Noesia_SWA_Admin_Sections ) {
				return;
			}

			$registry->register_section(
				array(
					'id'          => 'project-synapse',
					'label'       => $this->get_label(),
					'nav_label'   => $this->get_label(),
					'nav_sub'     => __( 'Human layer', 'noesia-smart-waiter' ),
					'icon'        => 'dashicons-lightbulb',
					'priority'    => 84,
					'tier'        => 'free',
					'owner'       => 'project-synapse',
					'hero'        => false,
					'is_visible'  => true,
					'nav_visible' => true,
					'callback'    => array( $this, 'render_admin' ),
				)
			);
		}

		/**
		 * Inject default settings.
		 *
		 * @param array<string,mixed> $defaults Existing defaults.
		 * @return array<string,mixed>
		 */
		public function filter_option_defaults( $defaults ) {
			if ( ! is_array( $defaults ) ) {
				$defaults = array();
			}

			$defaults[ Noesia_SWA_Project_Synapse_Service::OPTION_OPENAI_API_KEY ] = '';
			$defaults[ Noesia_SWA_Project_Synapse_Service::OPTION_OPENAI_MODEL ]   = 'gpt-4.1-mini';

			return $defaults;
		}

		/**
		 * Sanitize module-owned settings.
		 *
		 * @param array<string,mixed> $new_input Sanitized settings output.
		 * @param array<string,mixed> $input     Raw input.
		 * @param array<string,mixed> $existing  Existing settings.
		 * @return array<string,mixed>
		 */
		public function sanitize_options( $new_input, $input, $existing ) {
			if ( ! $this->service instanceof Noesia_SWA_Project_Synapse_Service ) {
				return is_array( $new_input ) ? $new_input : array();
			}

			return $this->service->sanitize_options( $new_input, $input, $existing );
		}

		/**
		 * Inject localized options.
		 *
		 * @param array<string,mixed> $options Existing options.
		 * @param mixed               $admin   Admin shell.
		 * @return array<string,mixed>
		 */
		public function filter_localized_options( $options, $admin ) {
			if ( ! is_array( $options ) ) {
				$options = array();
			}

			if ( $this->service instanceof Noesia_SWA_Project_Synapse_Service ) {
				$options['project_synapse'] = $this->service->get_public_payload();
			}

			return $options;
		}

		/**
		 * Inject admin strings.
		 *
		 * @param array<string,string> $strings Existing strings.
		 * @param mixed                $admin   Admin shell.
		 * @return array<string,string>
		 */
		public function filter_admin_strings( $strings, $admin ) {
			if ( ! is_array( $strings ) ) {
				$strings = array();
			}

			$strings['project_synapse_request_failed']               = __( 'Project Synapse request failed.', 'noesia-smart-waiter' );
			$strings['project_synapse_default_kicker']               = __( 'PROJECT SYNAPSE', 'noesia-smart-waiter' );
			$strings['project_synapse_aether_kicker']                = __( 'AETHER', 'noesia-smart-waiter' );
			$strings['project_synapse_default_question_placeholder'] = __( 'Write what you need to ask, remember, understand, or release.', 'noesia-smart-waiter' );
			$strings['project_synapse_aether_release_first']         = __( 'Write the words you want to release first.', 'noesia-smart-waiter' );
			$strings['project_synapse_aether_released']              = __( 'The words were released into the silence. Nothing was stored. Nothing returns.', 'noesia-smart-waiter' );
			$strings['project_synapse_install_missing_endpoint']     = __( 'Install cannot start because the admin AJAX endpoint or nonce is missing. Reload the Noesia admin page.', 'noesia-smart-waiter' );
			$strings['project_synapse_installing']                   = __( 'Installing the Synapse knowledge pack...', 'noesia-smart-waiter' );
			$strings['project_synapse_install_failed']               = __( 'Install failed.', 'noesia-smart-waiter' );
			$strings['project_synapse_install_failed_detailed']      = __( 'Could not install the Project Synapse knowledge pack.', 'noesia-smart-waiter' );
			$strings['project_synapse_install_success']              = __( 'Knowledge pack installed.', 'noesia-smart-waiter' );
			$strings['project_synapse_knowledge_ready']              = __( 'Knowledge ready', 'noesia-smart-waiter' );
			$strings['project_synapse_settings_missing_endpoint']    = __( 'Settings cannot be saved because the AJAX endpoint or nonce is missing. Reload the page.', 'noesia-smart-waiter' );
			$strings['project_synapse_settings_saving']              = __( 'Saving Synapse settings...', 'noesia-smart-waiter' );
			$strings['project_synapse_settings_save_failed']         = __( 'Settings save failed.', 'noesia-smart-waiter' );
			$strings['project_synapse_settings_save_failed_detailed'] = __( 'Could not save Synapse settings.', 'noesia-smart-waiter' );
			$strings['project_synapse_settings_saved']               = __( 'Synapse settings saved.', 'noesia-smart-waiter' );
			$strings['project_synapse_openai_key_saved']             = __( 'OpenAI key saved', 'noesia-smart-waiter' );
			$strings['project_synapse_ai_ready']                     = __( 'AI ready', 'noesia-smart-waiter' );
			$strings['project_synapse_empty_question']               = __( 'Write your question or message first.', 'noesia-smart-waiter' );
			$strings['project_synapse_thinking']                     = __( 'Synapse is preparing the answer...', 'noesia-smart-waiter' );
			$strings['project_synapse_answer_failed']                = __( 'Project Synapse could not answer this request.', 'noesia-smart-waiter' );

			return $strings;
		}

		/**
		 * Render admin.
		 *
		 * @return void
		 */
		public function render_admin() {
			if ( ! $this->service instanceof Noesia_SWA_Project_Synapse_Service ) {
				return;
			}

			$view_file = __DIR__ . '/views/admin.php';
			if ( file_exists( $view_file ) ) {
				$module  = $this;
				$service = $this->service;
				require $view_file;
			}
		}
	}
}
