<?php

declare(strict_types=1);

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

require_once CRTODAY_PLUGIN_DIR . 'includes/trait-crtoday-color-helpers.php';
require_once CRTODAY_PLUGIN_DIR . 'includes/trait-crtoday-fetch-lock.php';
require_once CRTODAY_PLUGIN_DIR . 'includes/trait-crtoday-rate-helpers.php';
require_once CRTODAY_PLUGIN_DIR . 'includes/class-crtoday-widget-template.php';
require_once CRTODAY_PLUGIN_DIR . 'includes/class-crtoday-source-selection.php';
require_once CRTODAY_PLUGIN_DIR . 'includes/class-crtoday-exchange-rates-block.php';
require_once CRTODAY_PLUGIN_DIR . 'includes/class-crtoday-converter-block.php';
require_once CRTODAY_PLUGIN_DIR . 'includes/class-crtoday-plugin-settings.php';
require_once CRTODAY_PLUGIN_DIR . 'includes/class-crtoday-review-banner.php';
require_once CRTODAY_PLUGIN_DIR . 'includes/class-crtoday-storage-installer.php';
require_once CRTODAY_PLUGIN_DIR . 'includes/class-crtoday-raw-snapshot-repository.php';

final class CRToday_Plugin {
	public static function bootstrap(): void {
		add_action('init', [self::class, 'init']);
		add_filter(
			'plugin_action_links_' . plugin_basename(CRTODAY_PLUGIN_FILE),
			[self::class, 'add_settings_link']
		);
		add_filter(
			'plugin_row_meta',
			[self::class, 'add_row_meta_links'],
			10,
			2
		);
		CRToday_Storage_Installer::maybe_install();
		CRToday_Plugin_Settings::register();
		CRToday_Review_Banner::register();
	}

	/**
	 * Add quick Settings link on plugins.php.
	 *
	 * @param array<int,string> $links Existing plugin action links.
	 * @return array<int,string>
	 */
	public static function add_settings_link(array $links): array {
		$settings_url = admin_url('admin.php?page=crtoday-plugin-settings');
		$settings_link = sprintf(
			'<a href="%s">%s</a>',
			esc_url($settings_url),
			esc_html__('Settings', 'currencyrate-today-currency-blocks')
		);

		array_unshift($links, $settings_link);

		return $links;
	}

	/**
	 * Add "Got Question?" and "Rate us" links to plugin row meta on plugins.php.
	 *
	 * @param array<int,string> $meta   Existing row meta.
	 * @param string            $file   Plugin basename.
	 * @return array<int,string>
	 */
	public static function add_row_meta_links(array $meta, string $file): array {
		if ($file !== plugin_basename(CRTODAY_PLUGIN_FILE)) {
			return $meta;
		}

		$meta[] = sprintf(
			'<a href="%s" target="_blank" rel="noopener noreferrer">%s</a>',
			esc_url('https://wordpress.org/support/plugin/currencyrate-today-currency-blocks/'),
			esc_html__('Got Question?', 'currencyrate-today-currency-blocks')
		);

		$meta[] = sprintf(
			'<a href="%s" target="_blank" rel="noopener noreferrer" style="font-weight:bold">%s</a>',
			esc_url('https://wordpress.org/support/plugin/currencyrate-today-currency-blocks/reviews/#new-post'),
			esc_html__('Rate us ★★★★★', 'currencyrate-today-currency-blocks')
		);

		return $meta;
	}

	public static function init(): void {
		CRToday_Exchange_Rates_Block::register();
		CRToday_Converter_Block::register();
	}

	public static function activate(): void {
		CRToday_Storage_Installer::maybe_install();

		if (get_option(CRToday_Plugin_Settings::option_name(), null) === null) {
			add_option(CRToday_Plugin_Settings::option_name(), [
				'retention'      => 2,
				'api_interval'   => 15,
				'rounding_mode'  => 'half_even',
			]);
		}

		CRToday_Plugin_Settings::invalidate_cache();
	}

	public static function deactivate(): void {
		CRToday_Plugin_Settings::invalidate_cache();
	}
}
