'use strict';

const BASE_MODULES = [
  'dashboard',
  'reportes',
  'graficas',
  'metricas',
  'inventario',
  'pagos',
  'ia',
  'mesas',
  'meseros',
  'turnos',
  'productos',
  'cortes',
  'compras',
  'insumos',
  'arkhon-card',
  'notificaciones',
];

const MODULES = Object.freeze({
  BASE: BASE_MODULES,
  WHATSAPP: 'whatsapp',
  BILLING: 'facturacion',
  CLOUD: 'cloud',
  RESTCARD: 'restcard',
});

const LICENSE_PLANS = Object.freeze({
  permanente: Object.freeze({
    key: 'permanente',
    name: 'Permanente',
    durationDays: null,
    cloudAccess: false,
    billingAccess: false,
    whatsappAccess: false,
    billingKeyRequired: false,
    listPriceMxn: 3500,
    priceMxn: 3000,
    modules: Object.freeze([...BASE_MODULES]),
  }),
  mensual: Object.freeze({
    key: 'mensual',
    name: 'Mensual',
    durationDays: 30,
    cloudAccess: true,
    billingAccess: false,
    whatsappAccess: true,
    billingKeyRequired: false,
    priceMxn: null,
    modules: Object.freeze([...BASE_MODULES, MODULES.WHATSAPP, MODULES.CLOUD]),
  }),
  anual: Object.freeze({
    key: 'anual',
    name: 'Anual',
    durationDays: 365,
    cloudAccess: true,
    billingAccess: false,
    whatsappAccess: true,
    billingKeyRequired: false,
    priceMxn: null,
    modules: Object.freeze([...BASE_MODULES, MODULES.WHATSAPP, MODULES.CLOUD]),
  }),
  mensual_restcard: Object.freeze({
    key: 'mensual_restcard',
    name: 'Mensual + RestCard',
    durationDays: 30,
    cloudAccess: true,
    billingAccess: false,
    whatsappAccess: true,
    restcardAccess: true,
    billingKeyRequired: false,
    priceMxn: 2500,
    modules: Object.freeze([...BASE_MODULES, MODULES.WHATSAPP, MODULES.CLOUD, MODULES.RESTCARD, 'arkhon-card']),
  }),
  anual_restcard: Object.freeze({
    key: 'anual_restcard',
    name: 'Anual + RestCard',
    durationDays: 365,
    cloudAccess: true,
    billingAccess: false,
    whatsappAccess: true,
    restcardAccess: true,
    billingKeyRequired: false,
    priceMxn: 6000,
    modules: Object.freeze([...BASE_MODULES, MODULES.WHATSAPP, MODULES.CLOUD, MODULES.RESTCARD, 'arkhon-card']),
  }),
  mensual_facturacion: Object.freeze({
    key: 'mensual_facturacion',
    name: 'Mensual + facturacion',
    durationDays: 30,
    cloudAccess: true,
    billingAccess: true,
    whatsappAccess: true,
    billingKeyRequired: true,
    priceMxn: 5000,
    modules: Object.freeze([...BASE_MODULES, MODULES.WHATSAPP, MODULES.CLOUD, MODULES.BILLING]),
  }),
  anual_facturacion: Object.freeze({
    key: 'anual_facturacion',
    name: 'Anual + facturacion',
    durationDays: 365,
    cloudAccess: true,
    billingAccess: true,
    whatsappAccess: true,
    billingKeyRequired: true,
    priceMxn: 8000,
    modules: Object.freeze([...BASE_MODULES, MODULES.WHATSAPP, MODULES.CLOUD, MODULES.BILLING]),
  }),
  mensual_facturacion_restcard: Object.freeze({
    key: 'mensual_facturacion_restcard',
    name: 'Mensual + facturacion + RestCard',
    durationDays: 30,
    cloudAccess: true,
    billingAccess: true,
    whatsappAccess: true,
    restcardAccess: true,
    billingKeyRequired: true,
    priceMxn: 6500,
    modules: Object.freeze([...BASE_MODULES, MODULES.WHATSAPP, MODULES.CLOUD, MODULES.BILLING, MODULES.RESTCARD, 'arkhon-card']),
  }),
  anual_facturacion_restcard: Object.freeze({
    key: 'anual_facturacion_restcard',
    name: 'Anual + facturacion + RestCard',
    durationDays: 365,
    cloudAccess: true,
    billingAccess: true,
    whatsappAccess: true,
    restcardAccess: true,
    billingKeyRequired: true,
    priceMxn: 9500,
    modules: Object.freeze([...BASE_MODULES, MODULES.WHATSAPP, MODULES.CLOUD, MODULES.BILLING, MODULES.RESTCARD, 'arkhon-card']),
  }),
  demo: Object.freeze({
    key: 'demo',
    name: 'Demo',
    durationDays: 5,
    cloudAccess: false,
    billingAccess: false,
    whatsappAccess: false,
    billingKeyRequired: false,
    priceMxn: null,
    modules: Object.freeze([...BASE_MODULES]),
  }),
  trial: Object.freeze({
    key: 'trial',
    name: 'Prueba general',
    durationDays: 5,
    cloudAccess: false,
    billingAccess: false,
    whatsappAccess: false,
    billingKeyRequired: false,
    priceMxn: null,
    modules: Object.freeze([...BASE_MODULES]),
  }),
});

function getPlan(planKey) {
  return LICENSE_PLANS[planKey] || null;
}

function assertPlan(planKey) {
  const plan = getPlan(planKey);
  if (!plan) {
    throw new Error(`Plan invalido: "${planKey}". Validos: ${Object.keys(LICENSE_PLANS).join(', ')}`);
  }
  return plan;
}

function uniqueModules(modules) {
  return Array.from(new Set(modules));
}

function createBillingAccessKey() {
  const crypto = require('crypto');
  return `BILL-${crypto.randomBytes(18).toString('base64url').toUpperCase()}`;
}

function buildLicenseEntitlements(planKey, options = {}) {
  const plan = assertPlan(planKey);
  const billingAccessKey = plan.billingKeyRequired
    ? (options.billingAccessKey || createBillingAccessKey())
    : null;

  return {
    modules: uniqueModules(plan.modules),
    cloud_access: plan.cloudAccess,
    billing_access: plan.billingAccess,
    whatsapp_access: plan.whatsappAccess,
    restcard_access: !!plan.restcardAccess,
    billing_key_required: plan.billingKeyRequired,
    billing_access_key: billingAccessKey,
    list_price_mxn: plan.listPriceMxn || plan.priceMxn,
    price_mxn: plan.priceMxn,
    updates_allowed: true,
  };
}

function planDurationDays(planKey) {
  return assertPlan(planKey).durationDays;
}

module.exports = {
  BASE_MODULES,
  MODULES,
  LICENSE_PLANS,
  getPlan,
  assertPlan,
  buildLicenseEntitlements,
  planDurationDays,
  createBillingAccessKey,
};
