import "server-only";

import { ModulePlanKey } from "@prisma/client";

export type PlanUiKey = "plan-1" | "plan-2" | "plan-3";

type ModulePlanDefinition = {
  key: ModulePlanKey;
  uiKey: PlanUiKey;
  name: string;
  moduleStart: number;
  moduleEnd: number;
  monthlyPrice: number;
  currency: string;
  benefits: string;
};

export const MODULE_PLANS: ModulePlanDefinition[] = [
  {
    key: ModulePlanKey.PLAN_2_4,
    uiKey: "plan-1",
    name: "Plan 1",
    moduleStart: 2,
    moduleEnd: 4,
    monthlyPrice: 499,
    currency: "MXN",
    benefits: "2 usuarios - 50 creditos IA",
  },
  {
    key: ModulePlanKey.PLAN_5_7,
    uiKey: "plan-2",
    name: "Plan 2",
    moduleStart: 5,
    moduleEnd: 7,
    monthlyPrice: 999,
    currency: "MXN",
    benefits: "5 usuarios - 150 creditos IA",
  },
  {
    key: ModulePlanKey.PLAN_8_10,
    uiKey: "plan-3",
    name: "Plan 3",
    moduleStart: 8,
    moduleEnd: 10,
    monthlyPrice: 1999,
    currency: "MXN",
    benefits: "Usuarios ilimitados - 500 creditos IA",
  },
];

export const MODULE_PLAN_BY_KEY = new Map(MODULE_PLANS.map((item) => [item.key, item]));
export const MODULE_PLAN_BY_UI_KEY = new Map(MODULE_PLANS.map((item) => [item.uiKey, item]));

export function getModulePlanByUiKey(planKey: string) {
  return MODULE_PLAN_BY_UI_KEY.get(planKey as PlanUiKey) ?? null;
}

export function getModulePlanByModuleNumber(moduleNumber: number) {
  return MODULE_PLANS.find((plan) => moduleNumber >= plan.moduleStart && moduleNumber <= plan.moduleEnd) ?? null;
}

export function getPlanDescription(planKey: ModulePlanKey) {
  const plan = MODULE_PLAN_BY_KEY.get(planKey);

  if (!plan) {
    return "Plan";
  }

  return `${plan.name} Modulos ${plan.moduleStart}-${plan.moduleEnd}`;
}
