<?php
/**
 * FluentC WordPress Plugin
 *
 * @category Plugin
 * @package  FluentCPlugin
 * @author   FluentC <wp@fluentc.io>
 * @license  http://www.gnu.org/copyleft/gpl.html GNU General Public License
 * @link     https://www.fluentc.ai
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}


class FluentCPlugin {

	/**
	 * Handles the main functionality of the FluentC WordPress Plugin
	 */
	protected $api_key;

	protected $fluentc_connect;


	/**
	 * Constructor for FluentCPlugin.
	 *
	 * Initializes the FluentCPlugin class and sets up any necessary variables.
	 */
	public function __construct() {

		$this->api_key = null;
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );

		if ( ! get_option( 'fluentc_api_key' ) ) {
			
		}
	}
	/**
	 * Adds a menu item to the WordPress admin dashboard for the plugin settings
	 */
	public function fluentc_add_menu() {

		add_menu_page( 'FluentC Settings', 'FluentC', 'manage_options', 'fluentc-settings', array( $this, 'settings_page' ) );
	}
	/**
	 * Removes the menu item from the WordPress admin dashboard for the plugin settings
	 */
	public function fluentc_remove_menu() {

		remove_menu_page( 'fluentc-settings' );
	}

	/**
	 * Displays the plugin settings page in the WordPress admin dashboard
	 */
	public function fluentc_settings_page() {
		include 'fluentc_settings.php';
	}

	/**
	 * Enqueues the necessary assets for the plugin.
	 */
	public function enqueue_assets() {
		// Only load on our plugin page
        if (get_current_screen()->id !== 'toplevel_page_fluentc-settings') {
            return;
        }

		wp_enqueue_script(
			'fluentc-app',
			FLUENTC_TRANSLATION_PLUGIN_URL . 'admin/build/main.js',
			array('react', 'react-dom', 'wp-element'),
			FLUENTC_TRANSLATION_VERSION,
			true
		);
		
		wp_enqueue_style(
			'fluentc-app-css',
			FLUENTC_TRANSLATION_PLUGIN_URL . 'admin/build/main.css',
			array(),
			FLUENTC_TRANSLATION_VERSION
		);
		
		$saved_api_key = get_option('fluentc_api_key');
        $has_user_api_key = !empty($saved_api_key);
        $api_key_to_use = $has_user_api_key ? $saved_api_key : '13c25c70-a4fa-4232-9d2c-1c451e71c3b7';

		$validation_completed_at = (int) get_option('fluentc_validation_completed_at', 0);

		wp_localize_script('fluentc-app', 'fluentcData', array(
			'ajaxUrl' => admin_url('admin-ajax.php'),
			'nonce' => wp_create_nonce('fluentc_nonce'),
			'apiUrl' => rest_url('wp/v2/'),
			'apiKey' => $api_key_to_use,
			'hasUserApiKey' => $has_user_api_key,
			'siteUrl' => home_url(),
			'adminUrl' => admin_url(),
			'siteLanguage' => substr(get_bloginfo('language'), 0, 2),
			'saveNonce' => wp_create_nonce('fluentc_save_settings_action'),
			'dashboardNonce' => wp_create_nonce('fluentc_dashboard_nonce'),
			'pluginUrl' => plugin_dir_url(__FILE__),
			'validationCompletedAt' => $validation_completed_at,
			'hasCompletedValidation' => $validation_completed_at > 0,
		));
	}


	/**
	 * Retrieves the user's FluentC API key from the WordPress database
	 */
	public function fluentc_get_settings() {

		$api_key = get_option( 'fluentc_api_key' );
		return $api_key;
	}

	

    /**
     * Enqueue frontend assets for shortcode
     */
    public function enqueue_frontend_assets() {
        // Enqueue React and ReactDOM
        wp_enqueue_script(
            'react',
            'https://unpkg.com/react@18/umd/react.production.min.js',
            array(),
            '18.2.0',
            true
        );
        
        wp_enqueue_script(
            'react-dom',
            'https://unpkg.com/react-dom@18/umd/react-dom.production.min.js',
            array('react'),
            '18.2.0',
            true
        );
        
        
        // Localize script with frontend data (accessible to public)
        wp_localize_script('fluentc-shortcode', 'fluentcData', array(
            'ajaxUrl' => admin_url('admin-ajax.php'),
            'nonce' => wp_create_nonce('fluentc_nonce'),
            'apiKey' => "13c25c70-a4fa-4232-9d2c-1c451e71c3b7",
            'siteUrl' => home_url(),
            'siteLanguage' => substr(get_bloginfo('language'), 0, 2),
        ));
    }
}
