import type { BillingCycle, SubscriptionPlanId } from './types.js';

export type SubscriptionFeature =
  | 'compliance_core'
  | 'reports'
  | 'assistant'
  | 'portals'
  | 'api_access';

export interface SubscriptionPlan {
  id: SubscriptionPlanId;
  name: string;
  description: string;
  seatLimit: number;
  siteLimit: number;
  features: SubscriptionFeature[];
  pricing: {
    monthlyZar: number | null;
    annualZar: number | null;
  };
  highlighted?: boolean;
  contactSales?: boolean;
}

export const TRIAL_DURATION_DAYS = 14;

/** PayFast checkout amounts — annual pricing includes 2 months free (10× monthly). */
export const PLAN_CHECKOUT_PRICING_ZAR = {
  starter: { monthlyZar: 2499, annualZar: 24990 },
  professional: { monthlyZar: 5999, annualZar: 59990 },
} as const;

export const SUBSCRIPTION_PLANS: Record<SubscriptionPlanId, SubscriptionPlan> = {
  trial: {
    id: 'trial',
    name: 'Trial',
    description: 'Full platform access for evaluation',
    seatLimit: 5,
    siteLimit: 1,
    features: ['compliance_core', 'reports', 'assistant'],
    pricing: { monthlyZar: null, annualZar: null },
  },
  starter: {
    id: 'starter',
    name: 'Starter',
    description: 'Core FSSC compliance for small teams and single-site operations',
    seatLimit: 10,
    siteLimit: 2,
    features: ['compliance_core'],
    pricing: {
      monthlyZar: PLAN_CHECKOUT_PRICING_ZAR.starter.monthlyZar,
      annualZar: PLAN_CHECKOUT_PRICING_ZAR.starter.annualZar,
    },
  },
  professional: {
    id: 'professional',
    name: 'Professional',
    description: 'Full compliance suite with AI assistant, reporting, and portals',
    seatLimit: 25,
    siteLimit: 5,
    features: ['compliance_core', 'reports', 'assistant', 'portals'],
    pricing: {
      monthlyZar: PLAN_CHECKOUT_PRICING_ZAR.professional.monthlyZar,
      annualZar: PLAN_CHECKOUT_PRICING_ZAR.professional.annualZar,
    },
    highlighted: true,
  },
  enterprise: {
    id: 'enterprise',
    name: 'Enterprise',
    description: 'Unlimited scale with dedicated support',
    seatLimit: Number.POSITIVE_INFINITY,
    siteLimit: Number.POSITIVE_INFINITY,
    features: ['compliance_core', 'reports', 'assistant', 'portals', 'api_access'],
    pricing: { monthlyZar: null, annualZar: null },
    contactSales: true,
  },
};

export const UPGRADEABLE_PLAN_IDS: SubscriptionPlanId[] = [
  'starter',
  'professional',
  'enterprise',
];

export function getSubscriptionPlan(planId: SubscriptionPlanId): SubscriptionPlan {
  return SUBSCRIPTION_PLANS[planId];
}

export function formatPlanPrice(
  plan: SubscriptionPlan,
  cycle: BillingCycle = 'monthly',
): string {
  if (plan.contactSales) {
    return 'Contact us';
  }

  const amount = cycle === 'monthly' ? plan.pricing.monthlyZar : plan.pricing.annualZar;
  if (amount == null) {
    return 'Free';
  }

  const formatted = new Intl.NumberFormat('en-ZA', {
    style: 'currency',
    currency: 'ZAR',
    maximumFractionDigits: 0,
  }).format(amount);

  if (cycle === 'annual') {
    return `${formatted}/yr (2 months free)`;
  }

  return `${formatted}/mo`;
}

export function getCheckoutAmountZar(
  planId: keyof typeof PLAN_CHECKOUT_PRICING_ZAR,
  cycle: BillingCycle,
): number {
  return PLAN_CHECKOUT_PRICING_ZAR[planId][cycle === 'monthly' ? 'monthlyZar' : 'annualZar'];
}
