<?php
/**
 * Bulk delete plugin transients by option_name prefix (prepared LIKE, no user input in SQL).
 *
 * @package Newsroom_AI_Assistant
 */

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

if ( ! function_exists( 'newsai_delete_transients_by_prefix' ) ) {
	/**
	 * Delete `_transient_{prefix}*` and matching timeout rows.
	 *
	 * @param string $name_prefix Transient name prefix (without `_transient_`).
	 * @return int Rows deleted (both queries combined).
	 */
	function newsai_delete_transients_by_prefix( $name_prefix ) {
		global $wpdb;

		$name_prefix = (string) $name_prefix;
		if ( $name_prefix === '' ) {
			return 0;
		}

		$like_value   = $wpdb->esc_like( '_transient_' . $name_prefix ) . '%';
		$like_timeout = $wpdb->esc_like( '_transient_timeout_' . $name_prefix ) . '%';
		$deleted      = 0;

		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
		$deleted += (int) $wpdb->query(
			$wpdb->prepare(
				"DELETE FROM {$wpdb->options} WHERE option_name LIKE %s",
				$like_value
			)
		);
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
		$deleted += (int) $wpdb->query(
			$wpdb->prepare(
				"DELETE FROM {$wpdb->options} WHERE option_name LIKE %s",
				$like_timeout
			)
		);

		return $deleted;
	}
}

if ( ! function_exists( 'newsai_delete_transients_by_prefixes' ) ) {
	/**
	 * @param list<string> $prefixes Transient name prefixes.
	 * @return int Total rows deleted.
	 */
	function newsai_delete_transients_by_prefixes( array $prefixes ) {
		$total = 0;
		foreach ( $prefixes as $prefix ) {
			$total += newsai_delete_transients_by_prefix( (string) $prefix );
		}
		return $total;
	}
}

if ( ! function_exists( 'newsai_get_plugin_transient_prefixes' ) ) {
	/**
	 * Known transient name prefixes used by Newsroom OS (most specific first).
	 *
	 * @return list<string>
	 */
	function newsai_get_plugin_transient_prefixes() {
		return [
			'newsai_trends_fail_',
			'newsai_trends_circuit_',
			'newsai_trends_neg_',
			'newsai_trends_items_stale_',
			'newsai_trends_logged_',
			'nros_trends_html_stale_v1_',
			'newsai_news_fail_',
			'newsai_news_circuit_',
			'newsai_news_neg_',
			'newsai_news_items_stale_',
			'newsai_news_logged_',
			'nros_news_html_stale_v1_',
			'newsai_ai_count_',
			'newsai_ai_',
			'newsai_task_unread_total_',
			'newsai_pro_author_perf_',
			'newsai_pro_authors_grid_',
			'newsai_chief_insights_',
			'nros_timeline_html_',
			'nros_schema_',
			'nros_tasks_user_',
			'nros_trend_keyword_refresh_lock',
			'newsai_',
		];
	}
}

if ( ! function_exists( 'newsai_clear_all_plugin_transients' ) ) {
	/**
	 * Remove plugin transients (uninstall / full reset). Uses prefix deletes + known singletons.
	 *
	 * @return void
	 */
	function newsai_clear_all_plugin_transients() {
		delete_transient( 'nros_google_update_status' );
		delete_transient( 'newsai_usage_registry_gdpr_notice' );
		newsai_delete_transients_by_prefixes( newsai_get_plugin_transient_prefixes() );
	}
}

if ( ! function_exists( 'newsai_clear_settings_save_transients' ) ) {
	/**
	 * Invalidate caches after settings/profile save (timeline, schema, general newsai_* caches).
	 *
	 * @return void
	 */
	function newsai_clear_settings_save_transients() {
		newsai_delete_transients_by_prefixes(
			[
				'nros_timeline_html_',
				'nros_schema_',
				'newsai_',
			]
		);
	}
}
