<?php

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

/**
 * Free: GSC summary (7 days), top 5 queries, dashboard preview.
 * PRO: GA4, 90-day history, monthly SEO trend (up to ~16 months GSC), keyword watchlist, PDF + email report, branding, auto sync.
 */
final class IComm_SM_Features {

	public const FREE_HISTORY_DAYS = 7;
	public const PRO_HISTORY_DAYS = 90;
	/** GSC Search Analytics retention ceiling (~16 months). Used for trend charts + daily sync. */
	public const PRO_TREND_HISTORY_DAYS = 486;
	public const FREE_TOP_QUERIES = 5;
	public const PRO_TOP_QUERIES = 50;
	public const PRO_MAX_WATCH_KEYWORDS = 10;

	/** @var array<string, string> */
	private const MAP = [
		'dashboard_basic' => 'free',
		'gsc_connect' => 'free',
		'gsc_summary' => 'free',
		'ga4_connect' => 'pro',
		'ga4_metrics' => 'pro',
		'keyword_watch' => 'pro',
		'client_pdf' => 'pro',
		'client_email' => 'pro',
		'report_branding' => 'pro',
		'auto_sync' => 'pro',
		'history_90d' => 'pro',
		'alerts' => 'pro',
		'recommendations' => 'pro',
		'opportunities' => 'pro',
		'gsc_indexing' => 'pro',
		'seo_trend' => 'pro',
	];

	public static function is_pro(): bool {
		return IComm_SM_License::is_active() || IComm_SM_License::is_trial_active();
	}

	public static function history_days(): int {
		return self::is_pro() ? self::PRO_HISTORY_DAYS : self::FREE_HISTORY_DAYS;
	}

	/** Days of GSC daily data kept/synced for SEO trend charts (PRO: ~16 months). */
	public static function trend_history_days(): int {
		return self::is_pro() ? self::PRO_TREND_HISTORY_DAYS : self::FREE_HISTORY_DAYS;
	}

	/** Sync window sent to license server (PRO pulls long GSC daily history). */
	public static function sync_days(): int {
		return self::trend_history_days();
	}

	public static function top_queries_limit(): int {
		return self::is_pro() ? self::PRO_TOP_QUERIES : self::FREE_TOP_QUERIES;
	}

	public static function can_use(string $feature): bool {
		$tier = self::MAP[$feature] ?? 'pro';
		if ($tier === 'free') {
			return true;
		}
		return self::is_pro();
	}

	public static function pro_badge_html(): string {
		if (self::is_pro()) {
			return '';
		}
		return '<span class="icomm-sm-pro-badge">PRO</span>';
	}

	public static function locked_notice_html(string $feature_label = ''): string {
		if (self::is_pro()) {
			return '';
		}
		$label = $feature_label !== '' ? $feature_label : __('מודול PRO', 'icomm-metrix');
		$url = admin_url('admin.php?page=icomm-metrix-settings#icomm-sm-license');
		return '<div class="icomm-sm-locked-notice notice notice-warning inline" dir="rtl" lang="he" style="direction:rtl;text-align:right;"><p>'
			. esc_html(sprintf(__('%s, נדרש רישיון PRO או תקופת ניסיון.', 'icomm-metrix'), $label))
			. ' <a href="' . esc_url($url) . '">' . esc_html__('רכישה / הפעלה', 'icomm-metrix') . '</a>'
			. '</p></div>';
	}
}
