<?php
/**
 * Plugin Name:       Maca Tjatt
 * Plugin URI:        https://maca.se/maca-tjatt-info/
 * Description:       AI chat for your website using the WordPress AI Client and a local search index.
 * Version:           1.2.3
 * Requires at least: 7.0
 * Requires PHP:      7.4
 * Tested up to:      7.0
 * Author:            Maca Development
 * Author URI:        https://github.com/maca59
 * License:           GPL-2.0-or-later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       maca-tjatt
 * Domain Path:       /languages
 *
 * @package Maca_Tjatt
 */

defined( 'ABSPATH' ) || exit;

define( 'MACA_TJATT_VERSION', '1.2.3' );
define( 'MACA_TJATT_PLUGIN_FILE', __FILE__ );
define( 'MACA_TJATT_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'MACA_TJATT_PLUGIN_URL', plugin_dir_url( __FILE__ ) );

$maca_tjatt_bootstrap = MACA_TJATT_PLUGIN_DIR . 'includes/class-maca-tjatt.php';

if ( ! is_readable( $maca_tjatt_bootstrap ) ) {
	register_activation_hook(
		__FILE__,
		static function () {
			deactivate_plugins( plugin_basename( __FILE__ ) );
			wp_die(
				esc_html__( 'Maca Tjatt is missing required files. Please upload maca-tjatt.zip again.', 'maca-tjatt' ),
				esc_html__( 'Plugin activation failed', 'maca-tjatt' ),
				array( 'response' => 500 )
			);
		}
	);

	if ( is_admin() ) {
		add_action(
			'admin_notices',
			static function () {
				echo '<div class="notice notice-error"><p>' . esc_html__( 'Maca Tjatt is missing required files. Please upload maca-tjatt.zip again.', 'maca-tjatt' ) . '</p></div>';
			}
		);
	}

	return;
}

require_once $maca_tjatt_bootstrap;

if ( ! function_exists( 'wp_ai_client_prompt' ) ) {
	if ( is_admin() ) {
		add_action(
			'admin_notices',
			static function () {
				echo '<div class="notice notice-error"><p>' . esc_html__( 'Maca Tjatt requires WordPress 7.0 or newer with the built-in AI Client.', 'maca-tjatt' ) . '</p></div>';
			}
		);
	}

	return;
}

/**
 * @return Maca_Tjatt
 */
function maca_tjatt() {
	return Maca_Tjatt::instance();
}

add_action(
	'plugins_loaded',
	static function () {
		maca_tjatt();
	}
);

register_activation_hook(
	__FILE__,
	static function () {
		$plugin_dir = plugin_dir_path( __FILE__ );
		$db_file    = $plugin_dir . 'includes/class-maca-tjatt-db.php';

		if ( ! file_exists( $db_file ) ) {
			deactivate_plugins( plugin_basename( __FILE__ ) );
			wp_die(
				esc_html__( 'Maca Tjatt could not be activated because a required file is missing. Reinstall the plugin from maca-tjatt.zip.', 'maca-tjatt' ),
				esc_html__( 'Plugin activation failed', 'maca-tjatt' ),
				array( 'response' => 500 )
			);
		}

		require_once $db_file;

		if ( ! function_exists( 'dbDelta' ) ) {
			require_once ABSPATH . 'wp-admin/includes/upgrade.php';
		}

		Maca_Tjatt_DB::create_tables();

		require_once $plugin_dir . 'includes/maca-api.php';
		maca_tjatt_api_on_activate();
	}
);

register_deactivation_hook(
	__FILE__,
	static function () {
		wp_clear_scheduled_hook( 'maca_tjatt_daily_reindex' );
		wp_clear_scheduled_hook( 'maca_tjatt_reindex_event' );

		require_once plugin_dir_path( __FILE__ ) . 'includes/maca-api.php';
		maca_tjatt_api_on_deactivate();
	}
);
