export const PLAN_DEFS = {
  premium_30d: {
    code: 'premium_30d',
    label: 'Premium, 30 Days',
    durationDays: 30,
    amountUsd: 20,
    interval: 'month',
    intervalCount: 1,
    tier: 'premium'
  },
  premium_90d: {
    code: 'premium_90d',
    label: 'Premium, 90 Days',
    durationDays: 90,
    amountUsd: 50,
    interval: 'month',
    intervalCount: 3,
    tier: 'premium'
  },
  premium_180d: {
    code: 'premium_180d',
    label: 'Premium, 180 Days',
    durationDays: 180,
    amountUsd: 70,
    interval: 'month',
    intervalCount: 6,
    tier: 'premium'
  },
  premium_365d: {
    code: 'premium_365d',
    label: 'Premium, 365 Days',
    durationDays: 365,
    amountUsd: 99,
    interval: 'year',
    intervalCount: 1,
    tier: 'premium'
  }
};

export const PREMIUM_FEATURES = ['sso', 'recipes', 'swarm', 'fleet'];

export function listPlans() {
  return Object.values(PLAN_DEFS);
}

export function getPlan(planCode) {
  return PLAN_DEFS[planCode] || null;
}

/**
 * Add days to a license's expiry. If expiry is in the past, count from now.
 * Returns the new ISO expiry timestamp.
 */
export function extendExpiry(currentIso, days) {
  const base = currentIso ? new Date(currentIso) : new Date();
  const now = Date.now();
  const start = base.getTime() > now ? base : new Date(now);
  start.setUTCDate(start.getUTCDate() + days);
  return start.toISOString();
}
