<?php

namespace Simple_History\Services;

/**
 * Service for handling Simple History update details.
 * Provides version-specific information about new features when Simple History is updated.
 */
class Simple_History_Updates extends Service {

	/**
	 * Called when service is loaded.
	 */
	public function loaded() {
		// Hook into the plugin update details filter for Simple History.
		add_filter( 'simple_history/pluginlogger/plugin_updated_details/simple-history/5.14.0', [ $this, 'on_plugin_updated_details_5_14_0' ] );
		add_filter( 'simple_history/pluginlogger/plugin_updated_details/simple-history/5.15.0', [ $this, 'on_plugin_updated_details_5_15_0' ] );
		add_filter( 'simple_history/pluginlogger/plugin_updated_details/simple-history/5.18.0', [ $this, 'on_plugin_updated_details_5_18_0' ] );
		add_filter( 'simple_history/pluginlogger/plugin_updated_details/simple-history/5.19.0', [ $this, 'on_plugin_updated_details_5_19_0' ] );
		add_filter( 'simple_history/pluginlogger/plugin_updated_details/simple-history/5.21.0', [ $this, 'on_plugin_updated_details_5_21_0' ] );
		add_filter( 'simple_history/pluginlogger/plugin_updated_details/simple-history/5.22.0', [ $this, 'on_plugin_updated_details_5_22_0' ] );
		add_filter( 'simple_history/pluginlogger/plugin_updated_details/simple-history/5.23.0', [ $this, 'on_plugin_updated_details_5_23_0' ] );
		add_filter( 'simple_history/pluginlogger/plugin_updated_details/simple-history/5.24.0', [ $this, 'on_plugin_updated_details_5_24_0' ] );
		add_filter( 'simple_history/pluginlogger/plugin_updated_details/simple-history/5.25.0', [ $this, 'on_plugin_updated_details_5_25_0' ] );
		add_filter( 'simple_history/pluginlogger/plugin_updated_details/simple-history/5.27.0', [ $this, 'on_plugin_updated_details_5_27_0' ] );
		add_filter( 'simple_history/pluginlogger/plugin_updated_details/simple-history/5.28.0', [ $this, 'on_plugin_updated_details_5_28_0' ] );
		add_filter( 'simple_history/pluginlogger/plugin_updated_details/simple-history/5.29.0', [ $this, 'on_plugin_updated_details_5_29_0' ] );
		add_filter( 'simple_history/pluginlogger/plugin_updated_details/simple-history/5.30.0', [ $this, 'on_plugin_updated_details_5_30_0' ] );


		// To test the output of a specific version, you can enable it for any just recently updated plugin that is visible in the GUI.
		// phpcs:ignore Squiz.Commenting.InlineComment.InvalidEndChar
		// add_filter( 'simple_history/pluginlogger/plugin_updated_details/<pluginslug>', [ $this, 'on_plugin_updated_details_5_18_0' ] );
	}

	/**
	 * Format new features as an HTML list.
	 *
	 * @param string $custom_title Optional custom title for the section.
	 * @param array  $features Array of feature descriptions.
	 * @param string $release_link Optional link to release post.
	 * @return string Formatted HTML list.
	 */
	private function format_new_features_list( $custom_title = '', $features = [], $release_link = '' ) {
		// Bail if no features.
		if ( empty( $features ) ) {
			return '';
		}

		$output = '<div class="sh-PluginUpdateDetails">';

		// Use custom title if provided, otherwise use default.
		$title   = empty( $custom_title ) ? __( "What's new in this version", 'simple-history' ) : $custom_title;
		$output .= '<h4 class="sh-PluginUpdateDetails-title">' . esc_html( $title ) . '</h4>';

		$output .= '<ul class="sh-PluginUpdateDetails-features">';

		foreach ( $features as $feature ) {
			$output .= '<li class="sh-PluginUpdateDetails-feature">' . esc_html( $feature ) . '</li>';
		}

		$output .= '</ul>';

		// Add release link if provided.
		if ( ! empty( $release_link ) ) {
			$output .= '<p class="sh-PluginUpdateDetails-releaseLink sh-ExternalLink"><a href="' . esc_url( $release_link ) . '" target="_blank">' .
				__( 'Read full release notes', 'simple-history' ) . '</a></p>';
		}

		$output .= '</div>';

		return $output;
	}

	/**
	 * Handle update details for Simple History version 5.14.0.
	 *
	 * @param string $extra_details Extra HTML to output after the changelog link.
	 * @return string Extra HTML to output after the changelog link.
	 */
	public function on_plugin_updated_details_5_14_0( $extra_details ) {
		$title = __( 'What\'s new in this version', 'simple-history' );

		$new_features = [
			'This release adds more filtering options, both in the frontend and in the REST API and WP-CLI. It also contains some bug fixes – and for the adventurous users we have two new experimental features',
		];

		$release_link = 'https://simple-history.com/2025/simple-history-5-14-0-released/';

		return $this->format_new_features_list( $title, $new_features, $release_link );
	}

	/**
	 * Handle update details for Simple History version 5.15.0.
	 *
	 * @param string $extra_details Extra HTML to output after the changelog link.
	 * @return string Extra HTML to output after the changelog link.
	 */
	public function on_plugin_updated_details_5_15_0( $extra_details ) {
		$title = __( 'New features in this version', 'simple-history' );

		$new_features = [
			'Email reports are now available for all users. Go to the settings page to enable!',
			'New Core Files Integrity Logger that detects modifications to WordPress core files through daily checksum verification',
		];

		$release_link = 'https://simple-history.com/2025/simple-history-5-15-0-released/';

		return $this->format_new_features_list( $title, $new_features, $release_link );
	}

	/**
	 * Handle update details for Simple History version 5.18.0.
	 *
	 * @param string $extra_details Extra HTML to output after the changelog link.
	 * @return string Extra HTML to output after the changelog link.
	 */
	public function on_plugin_updated_details_5_18_0( $extra_details ) {
		$title = __( 'Highlights in this version', 'simple-history' );

		$new_features = [
			'Context search added to log GUI filters for more precise filtering',
			'Interactive sidebar stats: click on avatars, user names, or chart dates to filter events instantly',
			'User names now shown in most active users list for better visibility',
		];

		$release_link = 'https://simple-history.com/2025/simple-history-5-18-0-released/';

		return $this->format_new_features_list( $title, $new_features, $release_link );
	}

	/**
	 * Handle update details for Simple History version 5.19.0.
	 *
	 * @param string $extra_details Extra HTML to output after the changelog link.
	 * @return string Extra HTML to output after the changelog link.
	 */
	public function on_plugin_updated_details_5_19_0( $extra_details ) {
		$title = __( 'Highlights in this version', 'simple-history' );

		$new_features = [
			__( 'WordPress 6.9 Notes logging – track when notes are added, edited, or removed', 'simple-history' ),
			__( '"Hide my own events" checkbox – focus on what others are doing', 'simple-history' ),
			__( '"Yesterday" date filter – quick access to recent activity', 'simple-history' ),
			__( 'Premium: Unlimited backfill – import your complete site history, even years back', 'simple-history' ),
		];

		$release_link = 'https://simple-history.com/2025/simple-history-5-19-0-released/';

		return $this->format_new_features_list( $title, $new_features, $release_link );
	}

	/**
	 * Handle update details for Simple History version 5.21.0.
	 *
	 * @param string $extra_details Extra HTML to output after the changelog link.
	 * @return string Extra HTML to output after the changelog link.
	 */
	public function on_plugin_updated_details_5_21_0( $extra_details ) {
		$title = __( 'Debug smarter, log everywhere', 'simple-history' );

		$new_features = [
			__( 'Surrounding Events – See exactly what happened before and after any event. Perfect for debugging!', 'simple-history' ),
			__( 'Log Forwarding (Beta) – Send events to log files, Syslog servers, or external databases for backup and compliance', 'simple-history' ),
			__( 'Auto-recovery – Database tables now self-heal if missing', 'simple-history' ),
		];

		$release_link = 'https://simple-history.com/2025/simple-history-5-21-0-released/';

		return $this->format_new_features_list( $title, $new_features, $release_link );
	}

	/**
	 * Handle update details for Simple History version 5.22.0.
	 * Also includes 5.21.0 highlights since those features are significant.
	 *
	 * @param string $extra_details Extra HTML to output after the changelog link.
	 * @return string Extra HTML to output after the changelog link.
	 */
	public function on_plugin_updated_details_5_22_0( $extra_details ) {
		$title = __( 'Debug smarter, log everywhere', 'simple-history' );

		$new_features = [
			__( 'Surrounding Events – See exactly what happened before and after any event. Perfect for debugging!', 'simple-history' ),
			__( 'Log Forwarding (Beta) – Send events to log files, Syslog servers, or external databases for backup and compliance', 'simple-history' ),
			__( 'Auto-recovery – Database tables now self-heal if missing', 'simple-history' ),
		];

		$release_link = 'https://simple-history.com/2025/simple-history-5-22-0-released/';

		return $this->format_new_features_list( $title, $new_features, $release_link );
	}

	/**
	 * Handle update details for Simple History version 5.23.0.
	 *
	 * @param string $extra_details Extra HTML to output after the changelog link.
	 * @return string Extra HTML to output after the changelog link.
	 */
	public function on_plugin_updated_details_5_23_0( $extra_details ) {
		$title = __( 'Highlights in this version', 'simple-history' );

		$new_features = [
			__( 'Alerts settings page – configure real-time notifications with Simple History Premium', 'simple-history' ),
			__( 'Notes stats – see notes activity in weekly email reports and History Insights (WordPress 6.9+)', 'simple-history' ),
			__( 'Security update detection – forced security updates from WordPress.org are now logged with details', 'simple-history' ),
			__( 'Performance – faster loading, optimized database queries, and improved RSS feeds', 'simple-history' ),
		];

		return $this->format_new_features_list( $title, $new_features );
	}

	/**
	 * Handle update details for Simple History version 5.24.0.
	 *
	 * @param string $extra_details Extra HTML to output after the changelog link.
	 * @return string Extra HTML to output after the changelog link.
	 */
	public function on_plugin_updated_details_5_24_0( $extra_details ) {
		$title = __( 'Highlights in this version', 'simple-history' );

		$new_features = [
			__( 'User details on click – click any avatar or name to instantly see their role, email, and profile', 'simple-history' ),
			__( 'Redesigned dashboard widget – more compact and simplified for a faster overview of recent activity', 'simple-history' ),
			__( 'Menu change logging – track item renames, moves, and location changes in your navigation menus', 'simple-history' ),
			__( 'Site Health monitoring – get a record of when WordPress flags new issues, marks them resolved, or changes their severity', 'simple-history' ),
			__( 'Copy as image – save any event as a shareable image, useful for bug reports, Slack messages, or sharing with clients', 'simple-history' ),
		];

		$release_link = 'https://simple-history.com/2026/simple-history-5-24-0-released/';

		return $this->format_new_features_list( $title, $new_features, $release_link );
	}

	/**
	 * Update details for version 5.25.0.
	 *
	 * @param string $extra_details Extra details.
	 * @return string
	 */
	public function on_plugin_updated_details_5_25_0( $extra_details ) {
		$title = __( 'Highlights in this version', 'simple-history' );

		$new_features = [
			__( 'Leaner database – smarter defaults reduce stored data over time, keeping your database smaller automatically', 'simple-history' ),
			__( 'Faster, more accurate search – results load more quickly and match only what you\'re looking for', 'simple-history' ),
			__( 'Keyboard shortcut – press / to jump straight to search from anywhere on the log page', 'simple-history' ),
			__( 'Easier settings navigation – Email Reports now has its own sub-tab so it\'s quicker to find', 'simple-history' ),
		];

		$release_link = 'https://simple-history.com/2026/simple-history-5-25-0-released/';

		return $this->format_new_features_list( $title, $new_features, $release_link );
	}

	/**
	 * Update details for version 5.27.0.
	 *
	 * @param string $extra_details Extra details.
	 * @return string
	 */
	public function on_plugin_updated_details_5_27_0( $extra_details ) {
		$title = __( 'Highlights in this version', 'simple-history' );

		$new_features = [
			__( 'AI agent attribution – see when an event was triggered through Claude Code, ChatGPT, or other AI tools, with the agent name shown next to the user', 'simple-history' ),
			__( 'Action links front and center – jump straight from the log to edit a media item, plugin, user, or menu, or open the error message for a failed plugin install', 'simple-history' ),
			__( 'Copy as JSON – copy the full event payload, including all context data, for scripting and debugging', 'simple-history' ),
		];

		$release_link = 'https://simple-history.com/2026/simple-history-5-27-0-released/';

		return $this->format_new_features_list( $title, $new_features, $release_link );
	}

	/**
	 * Update details for version 5.28.0.
	 *
	 * @param string $extra_details Extra details.
	 * @return string
	 */
	public function on_plugin_updated_details_5_28_0( $extra_details ) {
		$title = __( 'Highlights in this version', 'simple-history' );

		$new_features = [
			__( 'Ready for WordPress 7.0! We have tested the plugin on the latest version of WordPress and have found no issues.', 'simple-history' ),
			__( 'Added support for the new WordPress 7.0 AI Connectors Screen.', 'simple-history' ),
			__( 'Improved support for changes made using WP-CLI and REST API. Actions performed by for example AI agents are now visible in the log.', 'simple-history' ),
		];

		$release_link = 'https://simple-history.com/2026/simple-history-5-28-0-released/';

		return $this->format_new_features_list( $title, $new_features, $release_link );
	}

	/**
	 * Update details for version 5.29.0.
	 *
	 * @param string $extra_details Extra details.
	 * @return string
	 */
	public function on_plugin_updated_details_5_29_0( $extra_details ) {
		$title = __( 'Highlights in this version', 'simple-history' );

		$new_features = [
			__( "A person's activity log is now included in WordPress personal-data exports (Tools → Export Personal Data).", 'simple-history' ),
			__( 'New "Privacy & Data" settings tab explaining how Simple History works with WordPress privacy tools.', 'simple-history' ),
			__( 'Overview action links across user, plugin, post, and media events for quicker navigation.', 'simple-history' ),
			__( "Premium: See any user's last login, IP, browser, and event counts — right in the log.", 'simple-history' ),
		];

		$release_link = 'https://simple-history.com/2026/simple-history-5-29-0-released/';

		return $this->format_new_features_list( $title, $new_features, $release_link );
	}

	/**
	 * Update details for version 5.30.0.
	 *
	 * @param string $extra_details Extra details.
	 * @return string
	 */
	public function on_plugin_updated_details_5_30_0( $extra_details ) {
		$title = __( 'Highlights in this version', 'simple-history' );

		$new_features = [
			__( 'You can now react to events with a 👍.', 'simple-history' ),
			__( 'New header bar shows admins brief information about the current settings.', 'simple-history' ),
		];

		$release_link = 'https://simple-history.com/2026/simple-history-5-30-0-released/';

		return $this->format_new_features_list( $title, $new_features, $release_link );
	}
}
