<?php
/**
 * Plugin Name:       Archivist AI
 * Plugin URI:        https://archivist-ai.com
 * Description:       AI chat search for your article archive. Visitors ask questions, get source-backed answers — no login required.
 * Version:           0.5.0
 * Requires at least: 6.0
 * Requires PHP:      7.4
 * Author:            Titusz Magyar
 * License:           GPL-2.0-or-later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       archivist-ai
 */

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

define( 'ARCAI_VERSION', '0.5.0' );
define( 'ARCAI_PLUGIN_FILE', __FILE__ );
define( 'ARCAI_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'ARCAI_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'ARCAI_FREE_POST_CAP', 100 );

if ( ! function_exists( 'arcai_fs' ) ) {
	/**
	 * Freemius SDK accessor.
	 */
	function arcai_fs() {
		global $arcai_fs;

		if ( ! isset( $arcai_fs ) ) {
			require_once dirname( __FILE__ ) . '/vendor/freemius/start.php';

			$arcai_fs = fs_dynamic_init(
				array(
					'id'               => '31736',
					'slug'             => 'archivist-ai',
					'type'             => 'plugin',
					'public_key'       => 'pk_1428a0e8d5c69556d58e347ac4e65',
					'is_premium'       => false,
					'has_addons'       => false,
					'has_paid_plans'   => true,
					'is_org_compliant' => true,
					'menu'             => array(
						'slug'    => 'archivist-ai',
						'account' => true,
						'support' => false,
					),
				)
			);
		}

		return $arcai_fs;
	}

	arcai_fs();
	do_action( 'arcai_fs_loaded' );
}

/**
 * Whether Pro features are unlocked — answered by the Freemius license.
 * Define ARCAI_FORCE_PRO in wp-config.php to unlock on dev/staging sites
 * without a license.
 */
function arcai_is_pro() {
	if ( defined( 'ARCAI_FORCE_PRO' ) && ARCAI_FORCE_PRO ) {
		return true;
	}
	$pro = function_exists( 'arcai_fs' ) ? arcai_fs()->can_use_premium_code() : false;
	return (bool) apply_filters( 'arcai_is_pro', $pro );
}

require_once ARCAI_PLUGIN_DIR . 'includes/class-arcai-activator.php';
require_once ARCAI_PLUGIN_DIR . 'includes/class-arcai-settings.php';
require_once ARCAI_PLUGIN_DIR . 'includes/interface-arcai-provider.php';
require_once ARCAI_PLUGIN_DIR . 'includes/class-arcai-provider-openai.php';
require_once ARCAI_PLUGIN_DIR . 'includes/class-arcai-usage.php';
require_once ARCAI_PLUGIN_DIR . 'includes/class-arcai-chunker.php';
require_once ARCAI_PLUGIN_DIR . 'includes/class-arcai-vector-store.php';
require_once ARCAI_PLUGIN_DIR . 'includes/class-arcai-indexer.php';
require_once ARCAI_PLUGIN_DIR . 'includes/class-arcai-rate-limiter.php';
require_once ARCAI_PLUGIN_DIR . 'includes/class-arcai-chat-controller.php';
require_once ARCAI_PLUGIN_DIR . 'includes/class-arcai-frontend.php';

if ( is_admin() ) {
	require_once ARCAI_PLUGIN_DIR . 'admin/class-arcai-admin.php';
}

register_activation_hook( __FILE__, array( 'Arcai_Activator', 'activate' ) );
register_deactivation_hook( __FILE__, array( 'Arcai_Activator', 'deactivate' ) );

add_action( 'plugins_loaded', 'arcai_init' );

function arcai_init() {
	Arcai_Activator::maybe_upgrade();

	$indexer = new Arcai_Indexer();
	$indexer->register_hooks();

	$chat = new Arcai_Chat_Controller();
	add_action( 'rest_api_init', array( $chat, 'register_routes' ) );

	$frontend = new Arcai_Frontend();
	$frontend->register_hooks();

	if ( is_admin() ) {
		$admin = new Arcai_Admin( $indexer );
		$admin->register_hooks();
	}
}
