<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}
/**
 * Plugin Name: FluentC Translation
 * Plugin URI: https://www.fluentc.ai
 * Description: A plugin that enables website owners to easily install the FluentC Translation on their WordPress site.
 * Version: 2.8.3
 * Author: FluentC
 * Author URI: https://www.fluentc.ai
 * License: GPL2
 * Text Domain: fluentc-translation
 * Domain Path: /languages
 * Requires at least: 4.6
 */
define( 'FLUENTC_DIR', __DIR__ );
define( 'FLUENTC_SLUG', 'fluentc_translation' );
define( 'FLUENTC_TRANSLATION_VERSION', "2.8.3" );
define( 'POLYLANG_VERSION', "2.5.2" );
define( 'FLUENTC_TRANSLATION_PLUGIN_DIR', plugin_dir_path(__FILE__) );
define( 'FLUENTC_TRANSLATION_PLUGIN_URL', plugin_dir_url(__FILE__) );

// Define Polylang compatibility constants
define( 'PLL_COOKIE', 'pll_language' );
define( 'PLL_LOCALE', 'pll_locale' );
define( 'PLL_OPTION', 'polylang' );
define( 'PLL_ADMIN', 'pll_admin' );
define( 'PLL_FRONTEND', 'pll_frontend' );
define( 'PLL_LANGUAGE', 'language' );


require_once 'fluentc_plugin.php';

$fluentc_plugin = new FluentCPlugin();

function fluentc_activate() {
	/**
	 * Called when the plugin is activated and sets up any necessary database tables or options
	 */
	include_once __DIR__ . '/fluentc_autoload.php';
	include_once __DIR__ . '/vendor/autoload.php';
	include_once __DIR__ . '/bootstrap.php';
	include_once FLUENTC_DIR . '/src/fluentc_language_functions.php';

	// $fluentc_plugin->add_menu();
	FluentC_Context::fluentc_get_context()->activate_plugin();
	do_action( 'fluentc_activation_setup' );

	// ---------------------------------------------------------------------
	// First-run detection: only set when plugin has not been configured yet
	// (i.e., there is no saved API key). Prevents repeated onboarding after
	// users have already completed setup.
	// ---------------------------------------------------------------------
	if ( empty( get_option( 'fluentc_api_key' ) ) ) {
		if ( is_multisite() && is_network_admin() ) {
			set_site_transient( 'fluentc_first_run', 1, WEEK_IN_SECONDS );
		} else {
			set_transient( 'fluentc_first_run', 1, WEEK_IN_SECONDS );
		}
	}

	flush_rewrite_rules();
}

function fluentc_deactivate() {
	/**
	 * Called when the plugin is deactivated and cleans up any database tables or options
	 */
	flush_rewrite_rules();
	// $fluentc_plugin->remove_menu();
}

add_action(
	'admin_menu',
	function () use ( $fluentc_plugin ) {
		add_menu_page(
			'FluentC Settings',
			'FluentC',
			'manage_options',
			'fluentc-settings',
			array( $fluentc_plugin, 'fluentc_settings_page' ),
			'dashicons-translation',
			99
		);
	}
);

function fluentc_init_translation_features() {
    // Initialize language detection, redirects, etc.
    if (function_exists('fluentc_get_language')) {
        $current_language = fluentc_get_language();
        if ($current_language) {
            // Setup translation-specific hooks and features
            do_action('fluentc_init_translation', $current_language);
        }
    }
}
add_filter(
	'template_include',
	function ( $template ) {
		if ( is_page( 'fluentc-settings' ) ) {
			return plugin_dir_path( __FILE__ ) . 'fluentc_settings.php';
		}
		return $template;
	}
);

register_activation_hook( __FILE__, 'fluentc_activate' );
register_deactivation_hook( __FILE__, 'fluentc_deactivate' );


function fluentc_plugin_loaded() {
	include_once 'fluentc_autoload.php';
    include_once __DIR__ . '/vendor/autoload.php';
    include_once __DIR__ . '/bootstrap.php';
    include_once FLUENTC_DIR . '/src/fluentc_language_functions.php';

    // Check for API key before initializing most features
    $api_key = get_option('fluentc_api_key');
    
    // Initialize core plugin regardless of API key
    fluentc_init();
    
    // But only load language switching and translation features when API key exists
    if (!empty($api_key)) {
        add_action('init', 'fluentc_init_translation_features', 5);
    }
}

add_action( 'plugins_loaded', 'fluentc_plugin_loaded', 1 );

/**
 * Plugin Name: FluentC Languages Block
 * Description: Adds a FluentC Languages block to the Gutenberg editor.
 * Version: 1.0
 * Author: FluentC
 */

 function fluentc_languages_block_enqueue() {
    wp_enqueue_script(
        'fluentc-languages-block-editor',
        plugins_url( 'src/blocks/fluentc-languages-block.js', __FILE__ ),
        array( 'wp-blocks', 'wp-element', 'wp-block-editor' )
    );
}
add_action( 'enqueue_block_editor_assets', 'fluentc_languages_block_enqueue' );

function fluentc_languages_block_render( $block_content, $block ) {
    if ( $block['blockName'] === 'fluentc/languages-block' ) {
        return '<div id="fluentc-widget"></div>';
    }
    return $block_content;
}
add_filter( 'render_block', 'fluentc_languages_block_render', 10, 2 );


function fluentc_enqueue_styles() {
    wp_enqueue_style(
        'fluentc-styles',
        plugins_url( 'src/includes/fluentc-styles.css', __FILE__ )
    );
}
add_action( 'wp_enqueue_scripts', 'fluentc_enqueue_styles' );

// -------------------------------------------------------------------------
// Redirect newly-activated administrators to onboarding (one-time only)
// -------------------------------------------------------------------------
function fluentc_maybe_redirect_to_onboarding() {
    // Only in wp-admin and only for administrators.
    if ( ! is_admin() || ! current_user_can( 'manage_options' ) ) {
        return;
    }
    // If plugin already configured (API key present), do nothing
    if ( ! empty( get_option( 'fluentc_api_key' ) ) ) {
        return;
    }
    // Avoid AJAX/CRON contexts.
    if ( ( defined( 'DOING_AJAX' ) && DOING_AJAX ) || wp_doing_cron() ) {
        return;
    }

    // Determine correct transient accessor depending on multisite/network.
    $first_run_flag = is_multisite() && is_network_admin() ? get_site_transient( 'fluentc_first_run' ) : get_transient( 'fluentc_first_run' );

    if ( ! $first_run_flag ) {
        return;
    }

    // Clean up the transient to prevent loops.
    if ( is_multisite() && is_network_admin() ) {
        delete_site_transient( 'fluentc_first_run' );
    } else {
        delete_transient( 'fluentc_first_run' );
    }

    wp_safe_redirect( admin_url( 'admin.php?page=fluentc-settings&onboarding=1' ) );
    exit;
}
add_action( 'admin_init', 'fluentc_maybe_redirect_to_onboarding', 1 );

