import type {
  EstimateDemandLineCalculationMetadata,
  EstimateDemandLineMetadata,
  EstimateDemandLineRateMode,
} from "@capakraken/shared";

interface ResourceRateSnapshotLike {
  lcrCents: number;
  ucrCents: number;
  currency: string;
}

function parseRateMode(value: unknown): EstimateDemandLineRateMode | undefined {
  return value === "resource" || value === "manual" ? value : undefined;
}

export function parseDemandLineMetadata(
  metadata: Record<string, unknown> | null | undefined,
): EstimateDemandLineMetadata {
  if (typeof metadata !== "object" || metadata === null || Array.isArray(metadata)) {
    return {};
  }

  return metadata as EstimateDemandLineMetadata;
}

export function resolveDemandLineCalculationMetadata(options: {
  resourceSnapshot?: ResourceRateSnapshotLike | null | undefined;
  metadata?: Record<string, unknown> | null | undefined;
  costRateCents: number;
  billRateCents: number;
}): EstimateDemandLineCalculationMetadata {
  const resourceSnapshot = options.resourceSnapshot;
  const parsedMetadata = parseDemandLineMetadata(options.metadata);
  const calculation =
    typeof parsedMetadata.calculation === "object" &&
    parsedMetadata.calculation !== null
      ? parsedMetadata.calculation
      : undefined;
  const costRateMode =
    parseRateMode(calculation?.costRateMode) ??
    (resourceSnapshot && options.costRateCents === resourceSnapshot.lcrCents
      ? "resource"
      : "manual");
  const billRateMode =
    parseRateMode(calculation?.billRateMode) ??
    (resourceSnapshot && options.billRateCents === resourceSnapshot.ucrCents
      ? "resource"
      : "manual");

  return {
    costRateMode,
    billRateMode,
    totalMode: "computed",
    liveCostRateCents: resourceSnapshot?.lcrCents ?? null,
    liveBillRateCents: resourceSnapshot?.ucrCents ?? null,
    liveCurrency: resourceSnapshot?.currency ?? null,
  };
}

export function buildDemandLineMetadata(
  metadata: Record<string, unknown> | null | undefined,
  calculation: EstimateDemandLineCalculationMetadata,
): EstimateDemandLineMetadata {
  return {
    ...parseDemandLineMetadata(metadata),
    calculation,
  };
}

export function getEffectiveDemandLineValues(options: {
  resourceSnapshot?: ResourceRateSnapshotLike | null | undefined;
  hours: number;
  currency?: string | null;
  defaultCurrency: string;
  costRateCents: number;
  billRateCents: number;
  costRateMode: EstimateDemandLineRateMode;
  billRateMode: EstimateDemandLineRateMode;
}) {
  const effectiveCostRateCents =
    options.costRateMode === "resource" && options.resourceSnapshot
      ? options.resourceSnapshot.lcrCents
      : options.costRateCents;
  const effectiveBillRateCents =
    options.billRateMode === "resource" && options.resourceSnapshot
      ? options.resourceSnapshot.ucrCents
      : options.billRateCents;
  const currency =
    ((options.costRateMode === "resource" || options.billRateMode === "resource") &&
    options.resourceSnapshot?.currency
      ? options.resourceSnapshot.currency
      : options.currency) ||
    options.resourceSnapshot?.currency ||
    options.defaultCurrency;

  return {
    effectiveCostRateCents,
    effectiveBillRateCents,
    currency,
    costTotalCents: Math.round(options.hours * effectiveCostRateCents),
    priceTotalCents: Math.round(options.hours * effectiveBillRateCents),
  };
}
