export type Plan = "free" | "pro" | "legend";

export interface PlanLimits {
  /** Max. eigene Domains (Infinity = unbegrenzt) */
  customDomains: number;
  /** Freigeschaltete Domain-Slots füllen sich wieder auf (Community-Promotion) */
  domainRefill: boolean;
  /** Max. aktive Mail-Agenten (Infinity = unbegrenzt) */
  mailAgents: number;
  /** Erlaubte Scan-Intervalle in Stunden */
  mailIntervalOptions: number[];
  /** Zugang zur globalen HaGeZi-Blocklist (200k+) */
  globalBlocklist: boolean;
  /** Darf in der Community posten */
  canPost: boolean;
  /** Darf Gruppen gründen */
  canCreateGroup: boolean;
  /** Darf Domains direkt zur ReBreak Blocklist hinzufügen */
  canAddToBlocklist: boolean;
  /** Max. parallel registrierte Devices pro Account (Anti-Account-Sharing) */
  maxDevices: number;
  /** Primäres OpenRouter/Groq-Modell für KI-Coach */
  aiModel: string;
  /** Fallback-Modelle (werden der Reihe nach versucht wenn primary fehlschlägt) */
  aiModelFallbacks: Array<{ provider: "groq" | "openrouter"; model: string }>;
  /** AI-Provider: groq (Free/Pro) oder openrouter (Legend/Claude) */
  aiProvider: "groq" | "openrouter";
}

export const PLAN_LIMITS: Record<Plan, PlanLimits> = {
  free: {
    customDomains: 5,
    domainRefill: false,
    mailAgents: 1,
    mailIntervalOptions: [4],
    globalBlocklist: false,
    canPost: true,
    canCreateGroup: false,
    canAddToBlocklist: false,
    maxDevices: 1,
    aiModel: "llama-3.1-8b-instant",
    aiModelFallbacks: [
      { provider: "groq", model: "llama-3.3-70b-versatile" },
      { provider: "groq", model: "gemma2-9b-it" },
      { provider: "openrouter", model: "meta-llama/llama-3.1-8b-instruct" },
    ],
    aiProvider: "groq",
  },
  pro: {
    customDomains: 5,
    domainRefill: true,
    mailAgents: 3,
    mailIntervalOptions: [1, 4, 8],
    globalBlocklist: true,
    canPost: true,
    canCreateGroup: false,
    canAddToBlocklist: false,
    maxDevices: 1,
    aiModel: "llama-3.3-70b-versatile",
    aiModelFallbacks: [
      { provider: "groq", model: "llama-3.1-8b-instant" },
      { provider: "openrouter", model: "meta-llama/llama-3.3-70b-instruct" },
    ],
    aiProvider: "groq",
  },
  legend: {
    customDomains: 10,
    domainRefill: true,
    mailAgents: Infinity,
    mailIntervalOptions: [1, 4, 8],
    globalBlocklist: true,
    canPost: true,
    canCreateGroup: true,
    canAddToBlocklist: true,
    maxDevices: 3,
    aiModel: "anthropic/claude-3.5-haiku",
    aiModelFallbacks: [
      { provider: "openrouter", model: "anthropic/claude-3-haiku" },
      { provider: "groq", model: "llama-3.3-70b-versatile" },
    ],
    aiProvider: "openrouter",
  },
};

export function getPlanLimits(plan: string): PlanLimits {
  // Legacy-Pläne auf neue Namen mappen
  if (plan === "premium") return PLAN_LIMITS.legend;
  if (plan === "standard") return PLAN_LIMITS.pro;
  return PLAN_LIMITS[(plan as Plan) ?? "free"] ?? PLAN_LIMITS.free;
}
