// First-visit upgrade nag for Starter users. Non-blocking, dismissible,
// session-scoped — once dismissed, stays dismissed until the tab closes.
// Pro and Venue tiers get nothing; they're paying customers.

import {pushToast, dismissToast, registerToastAction} from './toasts.js';
import {pricingUrlWithContext} from '../js/utils.js';

const DISMISS_KEY = 'conductor.starter_cta.dismissed';
const TOAST_KEY = 'starter-upgrade-cta';

function markDismissed() {
    try { sessionStorage.setItem(DISMISS_KEY, '1'); } catch (_) {}
}

export function initStarterCta() {
    if (window.CONDUCTOR_TIER !== 'starter') return;
    try {
        if (sessionStorage.getItem(DISMISS_KEY) === '1') return;
    } catch (_) { /* private-mode: just show it */ }

    // "See Pro" is an <a href target="_blank"> so pywebview hands it off to
    // the external browser. The handler still fires alongside the click to
    // mark dismissed + close the toast.
    registerToastAction('starter-cta-upgrade', () => {
        markDismissed();
        dismissToast(TOAST_KEY);
    });
    registerToastAction('starter-cta-later', () => {
        markDismissed();
        dismissToast(TOAST_KEY);
    });

    // Small delay so the toast lands after the initial render settles and
    // doesn't compete with connection/notification toasts.
    setTimeout(() => {
        pushToast({
            key: TOAST_KEY,
            level: 'info',
            title: 'You\'re on Starter',
            detail: 'Pro unlocks DMX, AI Director, and unlimited fixtures.',
            actions: [
                {id: 'starter-cta-upgrade', label: 'See Pro', href: pricingUrlWithContext('starter_first_visit_toast'), target: '_blank'},
                {id: 'starter-cta-later', label: 'Not now'},
            ],
            dismissible: false,
        });
    }, 2500);
}
