/**
 * BrikPanel — Ad Platforms admin page JS.
 *
 * Vanilla JS; no jQuery. Single AJAX helper that wraps fetch() with the
 * nonce + admin-ajax conventions, then per-action handlers wired by data
 * attributes on the rendered cards.
 */
(function () {
	'use strict';

	if (typeof window.BrikpanelAds !== 'object') {
		return;
	}
	var BP = window.BrikpanelAds;
	var $root = document.getElementById('bp-ads');
	if (!$root) { return; }

	// ---------- helpers ----------
	function ajax(action, data) {
		var fd = new FormData();
		fd.append('action', action);
		fd.append('_ajax_nonce', BP.nonce);
		if (data && typeof data === 'object') {
			Object.keys(data).forEach(function (k) {
				if (data[k] !== undefined && data[k] !== null) {
					fd.append(k, data[k]);
				}
			});
		}
		return fetch(BP.ajaxUrl, {
			method: 'POST',
			credentials: 'same-origin',
			body: fd
		}).then(function (r) { return r.json(); })
		.then(function (json) {
			if (!json || typeof json !== 'object') {
				throw new Error(BP.i18n.generic_error);
			}
			if (!json.success) {
				var msg = (json.data && json.data.message) || BP.i18n.generic_error;
				var err = new Error(msg);
				err.data = json.data || {};
				throw err;
			}
			return json.data || {};
		});
	}

	function toast(message, tone) {
		var $t = document.getElementById('bp-ads-toast');
		if (!$t) { return; }
		$t.textContent = message;
		$t.className = 'bp-ads-toast ' + (tone === 'error' ? 'is-error' : 'is-success');
		$t.hidden = false;
		// reflow then animate in
		void $t.offsetWidth;
		$t.classList.add('is-visible');
		clearTimeout(toast._timer);
		toast._timer = setTimeout(function () {
			$t.classList.remove('is-visible');
			setTimeout(function () { $t.hidden = true; }, 320);
		}, 3500);
	}

	function busy($btn, on) {
		if (!$btn) { return; }
		if (on) { $btn.classList.add('is-loading'); $btn.disabled = true; }
		else    { $btn.classList.remove('is-loading'); $btn.disabled = false; }
	}

	function cardForButton($btn) {
		return $btn.closest('.bp-ads-card');
	}

	function platformOfCard($card) {
		return $card ? $card.getAttribute('data-platform') : '';
	}

	// ---------- flash on load ----------
	(function () {
		var tone = $root.getAttribute('data-flash-tone');
		var msg  = $root.getAttribute('data-flash-message');
		if (tone && msg) {
			toast(msg, tone === 'success' ? 'success' : 'error');
			// Strip the flash params from the URL so refresh doesn't re-show.
			if (window.history && window.history.replaceState) {
				var url = new URL(window.location.href);
				url.searchParams.delete('brikpanel_ads_flash');
				url.searchParams.delete('brikpanel_msg');
				window.history.replaceState({}, '', url.toString());
			}
		}
	})();

	// ---------- click delegation ----------
	$root.addEventListener('click', function (e) {
		var $btn = e.target.closest('[data-action]');
		if (!$btn) { return; }
		var action = $btn.getAttribute('data-action');
		var $card  = cardForButton($btn);
		var platform = platformOfCard($card);

		switch (action) {
			case 'connect':
			case 'reconnect':
				return handleConnect($btn, platform);
			case 'disconnect':
				return handleDisconnect($btn, platform);
			case 'load-accounts':
				return handleLoadAccounts($btn, platform);
			case 'save-primary':
				return handleSavePrimary($btn, platform);
			case 'save-mcc':
				return handleSaveMcc($btn);
			case 'sync-now':
				return handleSyncNow($btn, platform);
			case 'view-log':
				return handleViewLog($btn);
			case 'clear-log':
				return handleClearLog($btn);
		}
	});

	// Enable Save when the selection changes.
	$root.addEventListener('change', function (e) {
		var $sel = e.target.closest('[data-role="primary-select"]');
		if (!$sel) { return; }
		var $card = $sel.closest('.bp-ads-card');
		var $save = $card.querySelector('[data-action="save-primary"]');
		if ($save) { $save.disabled = !$sel.value; }
	});

	// ---------- action handlers ----------
	function handleConnect($btn, platform) {
		if (!platform) { return; }
		busy($btn, true);
		ajax('brikpanel_ads_oauth_start', { platform: platform })
			.then(function (data) {
				if (data && data.authorize_url) {
					window.location.href = data.authorize_url;
				} else {
					busy($btn, false);
					toast(BP.i18n.generic_error, 'error');
				}
			})
			.catch(function (err) {
				busy($btn, false);
				toast(err.message || BP.i18n.generic_error, 'error');
			});
	}

	function handleDisconnect($btn, platform) {
		if (!platform) { return; }
		if (!window.confirm(BP.i18n.disconnect_confirm)) { return; }
		busy($btn, true);
		ajax('brikpanel_ads_oauth_disconnect', { platform: platform })
			.then(function () {
				toast(BP.i18n.saved, 'success');
				setTimeout(function () { window.location.reload(); }, 400);
			})
			.catch(function (err) {
				busy($btn, false);
				toast(err.message || BP.i18n.generic_error, 'error');
			});
	}

	function handleLoadAccounts($btn, platform) {
		if (!platform) { return; }
		var $card = cardForButton($btn);
		var $sel  = $card.querySelector('[data-role="primary-select"]');
		var $save = $card.querySelector('[data-action="save-primary"]');
		busy($btn, true);
		ajax('brikpanel_ads_list_accounts', { platform: platform })
			.then(function (data) {
				var accounts = (data && data.accounts) || [];
				if (!accounts.length) {
					toast(BP.i18n.no_accounts, 'error');
					busy($btn, false);
					return;
				}
				var current = $sel.value;
				$sel.innerHTML = '';
				accounts.forEach(function (acc) {
					var opt = document.createElement('option');
					if (platform === 'google_ads') {
						opt.value = acc.id;
						opt.textContent = (acc.name || acc.id) +
							(acc.currency ? ' (' + acc.currency + ')' : '') +
							(acc.is_manager ? ' — Manager' : '');
					} else {
						opt.value = acc.id || ('act_' + (acc.account_id || ''));
						opt.textContent = (acc.name || acc.id) +
							(acc.currency ? ' (' + acc.currency + ')' : '');
					}
					if (opt.value === current) { opt.selected = true; }
					$sel.appendChild(opt);
				});
				if ($save) { $save.disabled = !$sel.value; }
				busy($btn, false);
			})
			.catch(function (err) {
				busy($btn, false);
				toast(err.message || BP.i18n.generic_error, 'error');
			});
	}

	function handleSavePrimary($btn, platform) {
		if (!platform) { return; }
		var $card = cardForButton($btn);
		var $sel  = $card.querySelector('[data-role="primary-select"]');
		var accountId = $sel ? $sel.value : '';
		if (!accountId) {
			toast(BP.i18n.pick_account_first, 'error');
			return;
		}
		busy($btn, true);
		ajax('brikpanel_ads_save_primary', { platform: platform, account_id: accountId })
			.then(function (data) {
				toast((data && data.message) || BP.i18n.saved, 'success');
				busy($btn, false);
			})
			.catch(function (err) {
				busy($btn, false);
				toast(err.message || BP.i18n.generic_error, 'error');
			});
	}

	function handleSaveMcc($btn) {
		var $card = cardForButton($btn);
		var $inp  = $card.querySelector('#bp-ads-mcc');
		var val   = $inp ? $inp.value.replace(/[^0-9]/g, '') : '';
		busy($btn, true);
		ajax('brikpanel_ads_save_login_customer', { login_customer_id: val })
			.then(function () {
				toast(BP.i18n.saved, 'success');
				busy($btn, false);
			})
			.catch(function (err) {
				busy($btn, false);
				toast(err.message || BP.i18n.generic_error, 'error');
			});
	}

	function handleSyncNow($btn, platform) {
		if (!platform) { return; }
		busy($btn, true);
		ajax('brikpanel_ads_sync_now', { platform: platform })
			.then(function (data) {
				toast((data && data.message) || BP.i18n.saved, 'success');
				busy($btn, false);
				// Update last-sync line without a full page reload.
				var $card = cardForButton($btn);
				var $last = $card.querySelector('[data-role="last-sync"]');
				if ($last) { $last.textContent = '0 ' + (BP.i18n.now || 'seconds') + ' ago'; }
				// Trigger a status refresh so backfill bar can update too.
				refreshStatus();
			})
			.catch(function (err) {
				busy($btn, false);
				toast(err.message || BP.i18n.generic_error, 'error');
			});
	}

	function handleViewLog($btn) {
		var $log = document.getElementById('bp-ads-log');
		if (!$log) { return; }
		if ($log.hidden === false && $log.dataset.loaded === '1') {
			$log.hidden = true;
			$log.dataset.loaded = '';
			return;
		}
		busy($btn, true);
		ajax('brikpanel_ads_view_log', {})
			.then(function (data) {
				renderLog($log, (data && data.entries) || []);
				$log.hidden = false;
				$log.dataset.loaded = '1';
				busy($btn, false);
			})
			.catch(function (err) {
				busy($btn, false);
				toast(err.message || BP.i18n.generic_error, 'error');
			});
	}

	function handleClearLog($btn) {
		busy($btn, true);
		ajax('brikpanel_ads_clear_log', {})
			.then(function () {
				var $log = document.getElementById('bp-ads-log');
				if ($log) {
					$log.innerHTML = '<div class="bp-ads-log-empty">' + BP.i18n.log_empty + '</div>';
				}
				busy($btn, false);
			})
			.catch(function (err) {
				busy($btn, false);
				toast(err.message || BP.i18n.generic_error, 'error');
			});
	}

	function renderLog($log, entries) {
		if (!entries || !entries.length) {
			$log.innerHTML = '<div class="bp-ads-log-empty">' + BP.i18n.log_empty + '</div>';
			return;
		}
		var html = '';
		entries.forEach(function (e) {
			html += '<div class="bp-ads-log-entry">'
				+   '<div class="bp-ads-log-meta">'
				+     '<span class="bp-ads-log-flow">' + escapeHtml(e.flow || '') + '</span>'
				+     '<span>' + escapeHtml(e.ts_display || '') + '</span>'
				+     (e.code ? '<span class="bp-ads-log-code">HTTP ' + escapeHtml(String(e.code)) + '</span>' : '')
				+   '</div>'
				+   '<div class="bp-ads-log-msg">' + escapeHtml(e.message || '') + '</div>'
				+ '</div>';
		});
		$log.innerHTML = html;
	}

	function escapeHtml(s) {
		return String(s).replace(/[&<>"']/g, function (c) {
			return { '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' }[c];
		});
	}

	// ---------- background status polling (backfill progress) ----------
	var pollTimer = null;
	function refreshStatus() {
		ajax('brikpanel_ads_status', {})
			.then(function (data) {
				if (!data) { return; }
				Object.keys(data).forEach(function (platform) {
					var $card = $root.querySelector('.bp-ads-card[data-platform="' + platform + '"]');
					if (!$card) { return; }
					var state = data[platform];
					var $back = $card.querySelector('.bp-ads-backfill');
					var total     = state.backfill && state.backfill.total     || 0;
					var completed = state.backfill && state.backfill.completed || 0;
					if (total > 0 && completed < total) {
						if (!$back) {
							var fragment = document.createElement('div');
							fragment.className = 'bp-ads-backfill';
							fragment.innerHTML =
								'<div class="bp-ads-backfill-label"></div>' +
								'<div class="bp-ads-backfill-bar"><div class="bp-ads-backfill-fill" style="width:0%"></div></div>';
							var $body = $card.querySelector('.bp-ads-card-body');
							if ($body) { $body.appendChild(fragment); }
							$back = fragment;
						}
						var pct = Math.min(100, Math.round((completed / Math.max(1, total)) * 100));
						$back.querySelector('.bp-ads-backfill-fill').style.width = pct + '%';
						$back.querySelector('.bp-ads-backfill-label').textContent =
							'Loading history… ' + completed + ' of ' + total + ' 90-day chunks done';
					} else if ($back) {
						$back.remove();
					}
				});
			})
			.catch(function () { /* silent */ });
	}

	// Only poll when at least one card is connected (otherwise nothing to update).
	var anyConnected = !!$root.querySelector('.bp-ads-card[data-connected="1"]');
	if (anyConnected) {
		pollTimer = setInterval(refreshStatus, 8000);
		// Pause polling when the page is hidden to avoid pointless work.
		document.addEventListener('visibilitychange', function () {
			if (document.hidden) {
				clearInterval(pollTimer);
				pollTimer = null;
			} else if (!pollTimer) {
				pollTimer = setInterval(refreshStatus, 8000);
				refreshStatus();
			}
		});
	}
})();
