/**
 * Admin Commercial bindings contain transport coordinates only.
 * All user-facing copy comes from the universal Presentation Dictionary.
 */
import {
  getRegistryPresentation,
  normalizeRegistryKind,
  type PresentationRegistryKind,
  type RegistryPresentation,
} from "../presentation";

export type CommercialAdminResourceKey = PresentationRegistryKind;

export interface CommercialAdminResourceDef {
  key: CommercialAdminResourceKey;
  presentation: RegistryPresentation;
  listPath: string;
  createPath: string;
  codeField: string;
  nameField: string;
  envelopeKeys: string[];
  requiresTenant?: boolean;
  readOnly?: boolean;
  group:
    | "catalog"
    | "commerce"
    | "governance"
    | "ops"
    | "localization"
    | "tenant";
}

type ResourceBinding = Omit<
  CommercialAdminResourceDef,
  "presentation" | "createPath"
> & {
  createPath?: string;
};

function defineResource(binding: ResourceBinding): CommercialAdminResourceDef {
  return {
    ...binding,
    presentation: getRegistryPresentation(binding.key),
    createPath: binding.createPath || binding.listPath,
  };
}

const RESOURCE_BINDINGS: ResourceBinding[] = [
  {
    key: "products",
    listPath: "/api/v1/commercial/products",
    codeField: "product_code",
    nameField: "display_name",
    envelopeKeys: ["items", "products"],
    group: "catalog",
  },
  {
    key: "bundles",
    listPath: "/api/v1/commercial/bundles",
    codeField: "bundle_code",
    nameField: "display_name",
    envelopeKeys: ["items", "bundles"],
    group: "catalog",
  },
  {
    key: "pricing",
    listPath: "/api/v1/commercial/pricing",
    codeField: "pricing_item_code",
    nameField: "display_name",
    envelopeKeys: ["items", "pricing"],
    group: "commerce",
  },
  {
    key: "plans",
    listPath: "/api/v1/commercial/plans",
    codeField: "plan_code",
    nameField: "display_name",
    envelopeKeys: ["items", "plans"],
    group: "commerce",
  },
  {
    key: "trial-rules",
    listPath: "/api/v1/commercial/trial-rules",
    codeField: "trial_rule_code",
    nameField: "display_name",
    envelopeKeys: ["items", "trial_rules"],
    group: "commerce",
  },
  {
    key: "subscription-templates",
    listPath: "/api/v1/commercial/subscription-templates",
    codeField: "template_code",
    nameField: "display_name",
    envelopeKeys: ["items", "templates", "subscription_templates"],
    group: "commerce",
  },
  {
    key: "usage-plans",
    listPath: "/api/v1/commercial/usage-plans",
    codeField: "usage_plan_code",
    nameField: "display_name",
    envelopeKeys: ["items", "usage_plans"],
    group: "commerce",
  },
  {
    key: "capabilities",
    listPath: "/api/v1/commercial/capabilities",
    codeField: "capability_code",
    nameField: "display_name",
    envelopeKeys: ["items", "capabilities"],
    group: "catalog",
  },
  {
    key: "business-types",
    listPath: "/api/v1/commercial/business-types",
    codeField: "business_type_code",
    nameField: "display_name",
    envelopeKeys: ["items", "business_types"],
    group: "catalog",
  },
  {
    key: "assessment-schemas",
    listPath: "/api/v1/commercial/assessment-schemas",
    codeField: "assessment_schema_code",
    nameField: "display_name",
    envelopeKeys: ["items", "schemas", "assessment_schemas"],
    group: "governance",
  },
  {
    key: "recommendation-rules",
    listPath: "/api/v1/commercial/recommendation-rules",
    codeField: "rule_code",
    nameField: "display_name",
    envelopeKeys: ["items", "rules"],
    group: "governance",
  },
  {
    key: "policies",
    listPath: "/api/v1/commercial/policies",
    codeField: "policy_code",
    nameField: "display_name",
    envelopeKeys: ["items", "policies"],
    group: "governance",
  },
  {
    key: "extensions",
    listPath: "/api/v1/commercial/extensions",
    codeField: "extension_code",
    nameField: "display_name",
    envelopeKeys: ["items", "extensions"],
    group: "catalog",
  },
  {
    key: "assets",
    listPath: "/api/v1/commercial/assets",
    codeField: "asset_code",
    nameField: "display_name",
    envelopeKeys: ["items", "assets"],
    group: "catalog",
  },
  {
    key: "automation-packs",
    listPath: "/api/v1/commercial/automation-packs",
    codeField: "automation_pack_code",
    nameField: "display_name",
    envelopeKeys: ["items", "automation_packs", "packs"],
    group: "catalog",
  },
  {
    key: "metadata",
    listPath: "/api/v1/commercial/metadata",
    codeField: "registry_object_id",
    nameField: "target_kind",
    envelopeKeys: ["items", "bags", "metadata"],
    group: "governance",
  },
  {
    key: "announcements",
    listPath: "/api/v1/commercial/announcements",
    codeField: "announcement_code",
    nameField: "title",
    envelopeKeys: ["items", "announcements"],
    group: "ops",
  },
  {
    key: "notifications",
    listPath: "/api/v1/commercial/notifications",
    codeField: "notification_code",
    nameField: "title",
    envelopeKeys: ["items", "notifications"],
    group: "ops",
  },
  {
    key: "promotions",
    listPath: "/api/v1/commercial/promotions",
    codeField: "promotion_code",
    nameField: "display_name",
    envelopeKeys: ["items", "promotions"],
    group: "commerce",
  },
  {
    key: "coupons",
    listPath: "/api/v1/commercial/coupons",
    codeField: "coupon_code",
    nameField: "display_name",
    envelopeKeys: ["items", "coupons"],
    group: "commerce",
  },
  {
    key: "discount-campaigns",
    listPath: "/api/v1/commercial/discount-campaigns",
    codeField: "campaign_code",
    nameField: "display_name",
    envelopeKeys: ["items", "campaigns", "discount_campaigns"],
    group: "commerce",
  },
  {
    key: "regional-pricing",
    listPath: "/api/v1/commercial/regional-pricing",
    codeField: "regional_pricing_code",
    nameField: "display_name",
    envelopeKeys: ["items", "regional_pricing"],
    group: "commerce",
  },
  {
    key: "taxes",
    listPath: "/api/v1/commercial/taxes",
    codeField: "tax_class_code",
    nameField: "display_name",
    envelopeKeys: ["items", "taxes", "tax_classes"],
    group: "commerce",
  },
  {
    key: "currencies",
    listPath: "/api/v1/commercial/currencies",
    codeField: "currency_code",
    nameField: "display_name",
    envelopeKeys: ["items", "currencies"],
    group: "localization",
  },
  {
    key: "languages",
    listPath: "/api/v1/commercial/languages",
    codeField: "language_code",
    nameField: "display_name",
    envelopeKeys: ["items", "languages"],
    group: "localization",
  },
  {
    key: "marketplace-visibility",
    listPath: "/api/v1/commercial/marketplace-visibility",
    codeField: "visibility_code",
    nameField: "display_name",
    envelopeKeys: ["items", "visibility"],
    group: "ops",
  },
  {
    key: "release-channels",
    listPath: "/api/v1/commercial/release-channels",
    codeField: "channel_code",
    nameField: "display_name",
    envelopeKeys: ["items", "channels"],
    group: "ops",
  },
  {
    key: "support-levels",
    listPath: "/api/v1/commercial/support-levels",
    codeField: "support_level_code",
    nameField: "display_name",
    envelopeKeys: ["items", "support_levels"],
    group: "ops",
  },
  {
    key: "subscriptions",
    listPath: "/api/v1/commercial/subscriptions",
    codeField: "id",
    nameField: "tenant_id",
    envelopeKeys: ["items", "subscriptions"],
    group: "tenant",
    requiresTenant: true,
    readOnly: true,
  },
  {
    key: "licenses",
    listPath: "/api/v1/commercial/licenses",
    codeField: "license_code",
    nameField: "display_name",
    envelopeKeys: ["items", "licenses"],
    group: "tenant",
    requiresTenant: true,
    readOnly: true,
  },
  {
    key: "entitlements",
    listPath: "/api/v1/commercial/entitlements",
    codeField: "entitlement_code",
    nameField: "display_name",
    envelopeKeys: ["items", "entitlements"],
    group: "tenant",
    requiresTenant: true,
    readOnly: true,
  },
  {
    key: "activation-logs",
    listPath: "/api/v1/commercial/activation-logs",
    codeField: "correlation_id",
    nameField: "tenant_id",
    envelopeKeys: ["items", "pipelines", "activation_logs"],
    group: "tenant",
    requiresTenant: true,
    readOnly: true,
  },
  {
    key: "invoices",
    listPath: "/api/v1/commercial/invoices",
    codeField: "invoice_code",
    nameField: "display_name",
    envelopeKeys: ["items", "invoices"],
    group: "tenant",
    requiresTenant: true,
    readOnly: true,
  },
  {
    key: "transactions",
    listPath: "/api/v1/commercial/transactions",
    codeField: "transaction_code",
    nameField: "display_name",
    envelopeKeys: ["items", "transactions"],
    group: "tenant",
    requiresTenant: true,
    readOnly: true,
  },
];

export const COMMERCIAL_ADMIN_RESOURCES: CommercialAdminResourceDef[] =
  RESOURCE_BINDINGS.map(defineResource);

export function getCommercialAdminResource(
  key: string
): CommercialAdminResourceDef | undefined {
  const normalized = normalizeRegistryKind(key);
  return COMMERCIAL_ADMIN_RESOURCES.find((resource) => resource.key === normalized);
}
