<?php

/**
 * The file that defines the core plugin class
 *
 * A class definition that includes attributes and functions used across both the
 * public-facing side of the site and the admin area.
 *
 * @link       https://ristrettoapps.com
 * @since      1.0.0
 *
 * @package    Spawnster
 * @subpackage Spawnster/includes
 */

use RistrettoApps\Spawnster\Helpers\TemplateView;

/**
 * The core plugin class.
 *
 * This is used to define internationalization, admin-specific hooks, and
 * public-facing site hooks.
 *
 * Also maintains the unique identifier of this plugin as well as the current
 * version of the plugin.
 *
 * @since      1.0.0
 * @package    Spawnster
 * @subpackage Spawnster/includes
 * @author     Ristretto Apps
 */
class Spawnster {

	/**
	 * The loader that's responsible for maintaining and registering all hooks that power
	 * the plugin.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      Spawnster_Loader    $loader    Maintains and registers all hooks for the plugin.
	 */
	protected $loader;

	/**
	 * The unique identifier of this plugin.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      string    $plugin_name    The string used to uniquely identify this plugin.
	 */
	protected $plugin_name;

	/**
	 * The current version of the plugin.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      string    $version    The current version of the plugin.
	 */
	protected $version;

	/**
	 * The prefix used for the tables created by the plugin.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      string    $table_prefix    The prefix used for the tables created by the plugin.
	 */
	protected static $table_prefix = 'spawnster_';

	/**
	 * The name of the table used to store the vector data for posts.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      string    $post_vector_table_name    The name of the table used to store the vector data for posts.
	 */
	protected static $post_vector_table_name = 'post_vectors';

	/**
	 * The name of the table used to store the vector data.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      string    $vector_table_name    The name of the table used to store the vector data.
	 */
	protected static $vector_table_name = 'vectors';

	/**
	 * The name of the table used to store the post summaries.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      string    $post_summary_table_name    The name of the table used to store the post summaries.
	 */
	protected static $post_summary_table_name = 'post_summaries';

	/**
	 * The dimension of the vectors.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      int    $vector_dimension    The dimension of the vectors.
	 */
	protected static $vector_dimension = 384;

	/**
	 * The storage_engine used for the tables.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      string    $storage_engine    The storage_engine used for the tables.
	 */
	protected static $storage_engine = 'InnoDB';

	/**
	 * Define the core functionality of the plugin.
	 *
	 * Set the plugin name and the plugin version that can be used throughout the plugin.
	 * Load the dependencies, define the locale, and set the hooks for the admin area and
	 * the public-facing side of the site.
	 *
	 * @since    1.0.0
	 */
	public function __construct() {
		if ( defined( 'SPAWNSTER_VERSION' ) ) {
			$this->version = SPAWNSTER_VERSION;
		} else {
			$this->version = '1.0.0';
		}
		$this->plugin_name = 'spawnster';

		$this->load_dependencies();
		$this->set_locale();
		$this->define_admin_hooks();
		$this->define_public_hooks();

	}

	/**
	 * Load the required dependencies for this plugin.
	 *
	 * Include the following files that make up the plugin:
	 *
	 * - Spawnster_Loader. Orchestrates the hooks of the plugin.
	 * - Spawnster_i18n. Defines internationalization functionality.
	 * - Spawnster_Admin. Defines all hooks for the admin area.
	 * - Spawnster_Public. Defines all hooks for the public side of the site.
	 *
	 * Create an instance of the loader which will be used to register the hooks
	 * with WordPress.
	 *
	 * @since    1.0.0
	 * @access   private
	 */
	private function load_dependencies() {

		/**
		 * The class responsible for orchestrating the actions and filters of the
		 * core plugin.
		 */
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-spawnster-loader.php';

		/**
		 * The class responsible for defining internationalization functionality
		 * of the plugin.
		 */
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-spawnster-i18n.php';

		/**
		 * The class responsible for defining all actions that occur in the admin area.
		 */
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-spawnster-admin.php';

		/**
		 * The class responsible for defining all actions that occur in the public-facing
		 * side of the site.
		 */
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'public/class-spawnster-public.php';

		$this->loader = new Spawnster_Loader();

	}

	/**
	 * Define the locale for this plugin for internationalization.
	 *
	 * Uses the Spawnster_i18n class in order to set the domain and to register the hook
	 * with WordPress.
	 *
	 * @since    1.0.0
	 * @access   private
	 */
	private function set_locale() {

		$plugin_i18n = new Spawnster_i18n();

		$this->loader->add_action( 'plugins_loaded', $plugin_i18n, 'load_plugin_textdomain' );

	}

	/**
	 * Register all of the hooks related to the admin area functionality
	 * of the plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 */
	private function define_admin_hooks() {
		$plugin_admin = new Spawnster_Admin( $this->get_plugin_name(), $this->get_version(), new TemplateView() );

		// Register styles and scripts
		$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' );
		$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' );

		// Register admin menu
		$this->loader->add_action( 'admin_menu', $plugin_admin, 'add_plugin_admin_menu' );
		
		// Register post type and initialization
		$this->loader->add_action( 'init', $plugin_admin, 'register_writing_job_post_type' );
		$this->loader->add_action( 'init', $plugin_admin, 'init_writing_jobs' );

		// Register AJAX handlers
		$this->loader->add_action( 'wp_ajax_create_content_job', $plugin_admin, 'ajax_create_content_job' );
		$this->loader->add_action( 'wp_ajax_update_content_job', $plugin_admin, 'ajax_update_content_job' );
		$this->loader->add_action( 'wp_ajax_delete_content_job', $plugin_admin, 'ajax_delete_content_job' );
		$this->loader->add_action( 'wp_ajax_generate_content_now', $plugin_admin, 'ajax_generate_content_now' );
		$this->loader->add_action( 'wp_ajax_search_similar_content', $plugin_admin, 'ajax_search_similar_content' );

		// Register other existing AJAX handlers
		$this->loader->add_action( 'wp_ajax_generate_content', $plugin_admin, 'ajax_generate_content' );
		$this->loader->add_action( 'wp_ajax_generate_description', $plugin_admin, 'ajax_generate_description' );
		$this->loader->add_action( 'wp_ajax_view_content', $plugin_admin, 'ajax_view_content' );
		$this->loader->add_action( 'wp_ajax_update_content', $plugin_admin, 'ajax_update_content' );
		$this->loader->add_action( 'wp_ajax_publish_pages', $plugin_admin, 'ajax_publish_pages' );
		$this->loader->add_action( 'wp_ajax_get_progress', $plugin_admin, 'ajax_get_progress' );
		$this->loader->add_action( 'wp_ajax_enhance_content', $plugin_admin, 'ajax_enhance_content' );
		$this->loader->add_action( 'wp_ajax_accept_content', $plugin_admin, 'ajax_accept_content' );
		$this->loader->add_action( 'wp_ajax_reject_content', $plugin_admin, 'ajax_reject_content' );
		$this->loader->add_action( 'wp_ajax_edit_menu', $plugin_admin, 'ajax_edit_menu' );
		$this->loader->add_action( 'wp_ajax_toggle_logging', $plugin_admin, 'ajax_toggle_logging' );

		// Register other hooks
		$this->loader->add_action( 'init', 'RistrettoApps\Spawnster\BaseController', 'shouldRedirect' );
		$this->loader->add_action( 'admin_post_setup_website_form_submit', 'RistrettoApps\Spawnster\BaseController', 'handle_setup_your_website_submission' );
	}

	/**
	 * Register all of the hooks related to the public-facing functionality
	 * of the plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 */
	private function define_public_hooks() {
		// Initialize the public class but don't add any hooks since we don't need them
		$plugin_public = new Spawnster_Public( $this->get_plugin_name(), $this->get_version() );
	}

	/**
	 * Run the loader to execute all of the hooks with WordPress.
	 *
	 * @since    1.0.0
	 */
	public function run() {
		$this->loader->run();
	}

	/**
	 * The name of the plugin used to uniquely identify it within the context of
	 * WordPress and to define internationalization functionality.
	 *
	 * @since     1.0.0
	 * @return    string    The name of the plugin.
	 */
	public function get_plugin_name() {
		return $this->plugin_name;
	}

	/**
	 * The reference to the class that orchestrates the hooks with the plugin.
	 *
	 * @since     1.0.0
	 * @return    Spawnster_Loader    Orchestrates the hooks of the plugin.
	 */
	public function get_loader() {
		return $this->loader;
	}

	/**
	 * Retrieve the version number of the plugin.
	 *
	 * @since     1.0.0
	 * @return    string    The version number of the plugin.
	 */
	public function get_version() {
		return $this->version;
	}

	/**
	 * Get the table prefix used by the plugin.
	 *
	 * @since    1.0.0
	 * @return   string    The table prefix.
	 */
	public static function get_table_prefix(): string {
		return self::$table_prefix;
	}

	/**
	 * Get the name of the post vector table.
	 *
	 * @since    1.0.0
	 * @return   string    The name of the post vector table.
	 */
	public static function get_post_vector_table_name(): string {
		global $wpdb;
		return $wpdb->prefix . self::$table_prefix . self::$post_vector_table_name;
	}

	/**
	 * Get the name of the vector table.
	 *
	 * @since    1.0.0
	 * @return   string    The name of the vector table.
	 */
	public static function get_vector_table_name(): string {
		global $wpdb;
		return $wpdb->prefix . self::$table_prefix . self::$vector_table_name;
	}

	/**
	 * Get the name of the post summary table.
	 *
	 * @since    1.0.0
	 * @return   string    The name of the post summary table.
	 */
	public static function get_post_summary_table_name(): string {
		global $wpdb;
		return $wpdb->prefix . self::$table_prefix . self::$post_summary_table_name;
	}

	/**
	 * Get the dimension of the vectors.
	 *
	 * @since    1.0.0
	 * @return   int    The dimension of the vectors.
	 */
	public static function get_vector_dimension(): int {
		return self::$vector_dimension;
	}

	/**
	 * Get the storage engine used for the tables.
	 *
	 * @since    1.0.0
	 * @return   string    The storage engine used for the tables.
	 */
	public static function get_storage_engine(): string {
		return self::$storage_engine;
	}

	/**
	 * Get a plugin option.
	 *
	 * @since    1.0.0
	 * @param    string $option_name   The name of the option.
	 * @param    mixed  $default_value The default value if the option doesn't exist.
	 * @return   mixed                 The option value.
	 */
	public static function get_option( $option_name, $default_value = '' ): mixed {
		$options = get_option( 'spawnster_settings', array() );
		return isset( $options[ $option_name ] ) ? $options[ $option_name ] : $default_value;
	}

	/**
	 * Check if the plugin is ready to use.
	 *
	 * @since    1.0.0
	 * @return   bool    True if the plugin is ready, false otherwise.
	 */
	public static function is_plugin_ready(): bool {
		$openai_api_key = get_option( \RistrettoApps\Spawnster\Base\Constants::SPAWNSTER_OPTION_OPENAPI_KEY );
		return ! empty( $openai_api_key );
	}

}
