/**
 * Dashboard Trend Radar + News (classic admin) without loading full script.js.
 */
(function ($) {
	'use strict';

	if (typeof newsai_vars === 'undefined') {
		return;
	}

	function resolveGeo() {
		var $geo = $('#newsai-trends-geo, #newsai-post-geo').first();
		if ($geo.length) {
			var val = String($geo.val() || '').trim();
			if (val !== '') {
				return val;
			}
		}
		if (newsai_vars.site_geo) {
			return String(newsai_vars.site_geo);
		}
		return newsai_vars.geo ? String(newsai_vars.geo) : 'US';
	}

	function paneNeedsFetch($pane) {
		if (!$pane || !$pane.length) {
			return false;
		}
		if ($pane.attr('data-nros-radar-loaded') === '1') {
			return false;
		}
		var text = ($pane.text() || '').toLowerCase();
		return $pane.find('.nros-radar-idle').length > 0
			|| $pane.find('.nros-skeleton-card').length > 0
			|| text.indexOf('loading') !== -1
			|| text.indexOf('φόρτωση') !== -1
			|| text.indexOf('tap news') !== -1
			|| $pane.is(':empty');
	}

	function renderSkeleton(targets) {
		var html = [
			'<div class="nros-skeleton-card"><span class="nros-skeleton nros-skeleton-line-sm"></span><span class="nros-skeleton nros-skeleton-line-lg"></span></div>',
			'<div class="nros-skeleton-card"><span class="nros-skeleton nros-skeleton-line-sm"></span><span class="nros-skeleton nros-skeleton-line-lg"></span></div>'
		].join('');
		targets.html(html);
	}

	function fetchTrends() {
		var targets = $('#newsai-trends-list');
		if (!targets.length) {
			return;
		}
		renderSkeleton(targets);
		$.post(newsai_vars.ajax_url, {
			action: 'newsai_get_trends',
			geo: resolveGeo(),
			post_id: 0,
			nonce: newsai_vars.nonce
		}, function (response) {
			if (response && response.success) {
				targets.html(response.data);
				targets.attr('data-nros-radar-loaded', '1');
			} else {
				var msg = (response && response.data && response.data.message) ? response.data.message : 'Error';
				targets.html('<div class="nros-radar-idle">' + $('<div/>').text(msg).html() + '</div>');
			}
		}).fail(function () {
			targets.html('<div class="nros-radar-idle">Network error</div>');
		});
	}

	function fetchNews() {
		var targets = $('#newsai-news-list');
		if (!targets.length) {
			return;
		}
		renderSkeleton(targets);
		$.post(newsai_vars.ajax_url, {
			action: 'newsai_get_news',
			geo: resolveGeo(),
			post_id: 0,
			nonce: newsai_vars.nonce
		}, function (response) {
			if (response && response.success) {
				targets.html(response.data);
				targets.attr('data-nros-radar-loaded', '1');
			} else {
				var msg = (response && response.data && response.data.message) ? response.data.message : 'Error';
				targets.html('<div class="nros-radar-idle">' + $('<div/>').text(msg).html() + '</div>');
			}
		}).fail(function () {
			targets.html('<div class="nros-radar-idle">Network error</div>');
		});
	}

	$(document).on('click', '.nros-radar-card .newsai-tab', function (e) {
		e.preventDefault();
		var $tab = $(this);
		var $card = $tab.closest('.nros-radar-card');
		var index = $tab.parent().find('.newsai-tab').index($tab);
		$tab.parent().find('.newsai-tab').removeClass('active');
		$tab.addClass('active');
		var $panes = $card.find('.newsai-tab-content');
		$panes.hide().attr('hidden', 'hidden');
		var $pane = $panes.eq(index);
		$pane.show().removeAttr('hidden');
		var label = ($tab.text() || '').toLowerCase();
		if (label.indexOf('news') !== -1 && paneNeedsFetch($pane)) {
			fetchNews();
		} else if (label.indexOf('trend') !== -1 && paneNeedsFetch($pane)) {
			fetchTrends();
		}
	});

	$(document).on('change', '#newsai-trends-geo', function () {
		$('#newsai-trends-list, #newsai-news-list').removeAttr('data-nros-radar-loaded');
		if ($('#newsai-trends-list').is(':visible')) {
			fetchTrends();
		}
		if ($('#newsai-news-list').is(':visible')) {
			fetchNews();
		}
	});

	$(function () {
		if (newsai_vars.lite_mode) {
			return;
		}
		var $trends = $('#newsai-trends-list');
		if ($trends.length && $trends.is(':visible') && paneNeedsFetch($trends)) {
			fetchTrends();
		}
	});
})(jQuery);
