import { create } from 'zustand';

/**
 * Global trigger for the in-dashboard upgrade modal. Entry points scattered
 * across the app (AI / project-limit gates, premium-kit gates, workspace seat
 * banners, the AppNav Upgrade button) call `open(reason)`; a single
 * `<UpgradeModal />` mounted in AppNav renders it. Keeps the upgrade UX in the
 * dashboard instead of a full-page trip to /pricing.
 */
type UpgradeReason =
    | 'projects'
    | 'ai'
    | 'premium_kits'
    | 'seats'
    | 'export'
    | 'general';

/**
 * `upgrade` (default) lists only tiers ABOVE the current plan — the upsell used
 * by gates + the AppNav button. `change` lists ALL other active plans (up AND
 * down) — used by the account page's "Change plan" so downgrades work in-modal
 * without a trip to /pricing.
 */
type UpgradeMode = 'upgrade' | 'change';

type UpgradeModalState = {
    open: boolean;
    reason: UpgradeReason;
    mode: UpgradeMode;
    openModal: (reason?: UpgradeReason, mode?: UpgradeMode) => void;
    closeModal: () => void;
};

export const useUpgradeModal = create<UpgradeModalState>((set) => ({
    open: false,
    reason: 'general',
    mode: 'upgrade',
    openModal: (reason = 'general', mode = 'upgrade') =>
        set({ open: true, reason, mode }),
    closeModal: () => set({ open: false }),
}));
