/**
 * Cookie Consent Management
 *
 * Handles cookie banner display, user consent preferences, and localStorage persistence.
 * Integrates with analytics loading via applyConsent() callback.
 */

var CONSENT_KEY = "myai_cookie_consent";

/**
 * Parse consent object from localStorage.
 * Expected format: { necessary: true, analytics: bool, ts: iso-timestamp }
 *
 * @returns {object|null} - Parsed consent object or null if not found or invalid JSON
 */
function getConsent() {
	try {
		return JSON.parse(localStorage.getItem(CONSENT_KEY));
	} catch (e) {
		return null;
	}
}

/**
 * Persist consent object to localStorage as JSON.
 * Stores { necessary, analytics, ts } properties.
 *
 * @param {object} consent - Consent object with necessary, analytics, ts properties
 */
function setConsent(consent) {
	localStorage.setItem(CONSENT_KEY, JSON.stringify(consent));
}

/**
 * Apply stored consent preferences.
 * Currently loads Google Tag Manager if analytics consent is true.
 * Hook for extending to other tracking/analytics systems.
 *
 * @param {object} consent - Consent object from localStorage
 */
function applyConsent(consent) {
	if (window.MyAi && window.MyAi.applyConsent) {
		window.MyAi.applyConsent(consent);
	}
}

/**
 * Show cookie consent banner with fade-in animation.
 */
function showBanner() {
	$('#cookieBanner').fadeIn(200);
}

/**
 * Hide cookie consent banner with fade-out animation.
 */
function hideBanner() {
	$('#cookieBanner').fadeOut(200);
}

/**
 * Show "manage cookies" button.
 * Appears after user has made a consent choice.
 */
function showManage() {
	$('#cookieManage').show();
}

/**
 * Initialize cookie consent flow on page load.
 * If user has already made a choice, apply preferences and show manage button.
 * If no choice yet, show consent banner.
 */
function initConsent() {
	var consent = getConsent();
	if (!consent) {
		showBanner();
	} else {
		applyConsent(consent);
		showManage();
	}
}

/**
 * Bind cookie consent button handlers.
 * Reject or Necessary-only button → analytics: false
 * Accept button → analytics: true (and load GTM)
 * Manage button → show banner again
 */
function setupConsentHandlers() {
	$('#cookieReject, #cookieNecessary').on('click', function () {
		setConsent({
			necessary: true,
			analytics: false,
			ts: new Date().toISOString()
		});
		hideBanner();
		showManage();
	});

	$('#cookieAccept').on('click', function () {
		var consent = {
			necessary: true,
			analytics: true,
			ts: new Date().toISOString()
		};
		setConsent(consent);
		applyConsent(consent);
		hideBanner();
		showManage();
	});

	$('#cookieManage').on('click', function (e) {
		e.preventDefault();
		showBanner();
	});
}

// Expose consent utilities on window.MyAi
window.MyAi = window.MyAi || {};
window.MyAi.getConsent = getConsent;
window.MyAi.setConsent = setConsent;
window.MyAi.initConsent = initConsent;
window.MyAi.showBanner = showBanner;
window.MyAi.hideBanner = hideBanner;

// Initialize on DOM ready
$(function() {
	setupConsentHandlers();
	initConsent();
});
