/**
 * Vendor commission — the single source of truth for a vendor's effective
 * commission rate.
 *
 * `resolveVendorCommission` is a PURE function: callers pass already-loaded
 * docs, no DB reads happen here. Its result is written to `Vendor.commission`
 * (the enforcement cache) at write/seed moments only — vendor apply, admin
 * create, plan assignment, and plan-rate re-projection. The order/payout path
 * never calls this; it reads the cached `Vendor.commission` number, so the money
 * path stays inert to plans.
 *
 * Precedence (highest → lowest):
 *   1. the default (admin-owned) store          → 0   (never billed)
 *   2. an applicable subscription plan           → plan.commissionRate
 *   3. settings.orders.commission.vendorRate     → the configured default
 *   4. DEFAULT_VENDOR_COMMISSION_RATE            → the built-in fallback
 *
 * The caller passes `plan` ONLY when it should apply (i.e. the vendor has an
 * active/trialing subscription to it); this keeps subscription-status logic out
 * of the resolver.
 */

import { isDefaultVendorRecord } from "@/lib/multi-vendor";
import { DEFAULT_VENDOR_COMMISSION_RATE } from "@/lib/order-settings";

export interface CommissionVendorLike {
  isDefault?: boolean;
  slug?: string;
}

export interface CommissionPlanLike {
  commissionRate?: number;
}

export interface CommissionSettingsLike {
  orders?: { commission?: { vendorRate?: number } } | null;
}

function clampPercent(value: number): number {
  if (!Number.isFinite(value)) return DEFAULT_VENDOR_COMMISSION_RATE;
  return Math.max(0, Math.min(100, value));
}

export function resolveVendorCommission(
  vendor: CommissionVendorLike | null | undefined,
  plan: CommissionPlanLike | null | undefined,
  settings: CommissionSettingsLike | null | undefined,
): number {
  // The default (admin-owned) store is never billed a commission.
  if (vendor && isDefaultVendorRecord(vendor)) return 0;

  if (plan && typeof plan.commissionRate === "number") {
    return clampPercent(plan.commissionRate);
  }

  const settingsRate = settings?.orders?.commission?.vendorRate;
  if (typeof settingsRate === "number" && Number.isFinite(settingsRate)) {
    return clampPercent(settingsRate);
  }

  return DEFAULT_VENDOR_COMMISSION_RATE;
}
