/**
 * Classic admin micro-interactions (Phase 5).
 */
(function () {
	'use strict';

	function initCompetitorButtons() {
		document.querySelectorAll('.nros-comp-add-btn').forEach(function (btn) {
			if (btn.querySelector('.nros-comp-add-btn__label')) {
				return;
			}
			var icon = document.createElement('span');
			icon.className = 'nros-comp-add-btn__icon';
			icon.setAttribute('aria-hidden', 'true');
			icon.textContent = '+';
			var label = document.createElement('span');
			label.className = 'nros-comp-add-btn__label';
			label.textContent = 'Brief';
			btn.textContent = '';
			btn.appendChild(icon);
			btn.appendChild(label);
			if (!btn.getAttribute('aria-label')) {
				btn.setAttribute('aria-label', 'Add to brief');
			}
		});
	}

	function initKanbanCardLinks() {
		document.querySelectorAll('.nros-kanban-card__title').forEach(function (titleEl) {
			var card = titleEl.closest('.nros-kanban-card');
			if (!card || titleEl.closest('a')) {
				return;
			}
			var taskId = card.getAttribute('data-task-id');
			if (!taskId) {
				return;
			}
			var href = 'admin.php?page=newsai-settings&tab=alerts&edit=' + encodeURIComponent(taskId);
			var link = document.createElement('a');
			link.href = href;
			link.className = 'nros-kanban-card__title-link';
			link.textContent = titleEl.textContent;
			titleEl.textContent = '';
			titleEl.appendChild(link);
		});
	}

	document.addEventListener('mousedown', function (e) {
		if (e.target.closest('.nros-kanban-card__title-link')) {
			e.stopPropagation();
		}
	});

	document.addEventListener('DOMContentLoaded', function () {
		initCompetitorButtons();
		initKanbanCardLinks();
	});
})();
