/**
 * License Utilities
 *
 * Utilities for checking Pro license status and features.
 * Reads license data from the PHP-provided oppbAdmin object.
 *
 * @package OpusPriceBuilder
 */

// Type declarations for oppbAdmin license data
interface LicenseData {
  isPro: boolean;
  plan: "free" | "trial" | "pro";
  upgradeUrl: string;
}

/**
 * Pro feature identifiers
 */
export type ProFeature = "ai_helper" | "advanced_presets" | "countup_animation";

/**
 * Get license data from oppbAdmin
 */
export function getLicenseData(): LicenseData {
  if (typeof window !== "undefined" && window.oppbAdmin?.license) {
    return window.oppbAdmin.license;
  }

  // Default to free if not available
  return {
    isPro: false,
    plan: "free",
    upgradeUrl: "",
  };
}

/**
 * Check if user has Pro license (paying or trial)
 */
export function isPro(): boolean {
  return getLicenseData().isPro;
}

/**
 * Check if user is on trial
 */
export function isTrial(): boolean {
  return getLicenseData().plan === "trial";
}

/**
 * Check if user is on free plan
 */
export function isFree(): boolean {
  return !getLicenseData().isPro;
}

/**
 * Get the current plan name
 */
export function getPlan(): "free" | "trial" | "pro" {
  return getLicenseData().plan;
}

/**
 * Get upgrade URL for Freemius checkout
 */
export function getUpgradeUrl(): string {
  return getLicenseData().upgradeUrl;
}

/**
 * Check if a specific Pro feature is available
 */
export function hasProFeature(feature: ProFeature): boolean {
  // All Pro features require Pro license
  return isPro();
}

/**
 * Pro features with their labels and descriptions
 */
export const PRO_FEATURES: Record<
  ProFeature,
  {
    label: string;
    description: string;
  }
> = {
  ai_helper: {
    label: "AI Layout Helper",
    description: "Generate optimized HTML, CSS, and JavaScript with copyable PHP filter snippets and live preview.",
  },
  advanced_presets: {
    label: "Advanced Presets",
    description: "Access animated presets with custom CSS animations and JavaScript effects.",
  },
  countup_animation: {
    label: "Count Up Animation",
    description: "Animate prices counting up from zero using the CountUp.js library with customizable easing.",
  },
};

/**
 * Get all Pro features as an array
 */
export function getAllProFeatures(): Array<{ id: ProFeature; label: string; description: string }> {
  return Object.entries(PRO_FEATURES).map(([id, data]) => ({
    id: id as ProFeature,
    ...data,
  }));
}
