import { err, ok, type CommandContext } from "@tailor-platform/erp-kit/core";
import { Decimal } from "decimal.js";
import { purchaseBillLifecycle } from "../db/purchaseBill.lifecycle.generated";
import type { Transaction } from "../generated/kysely-tailordb";
import { calculateDueSchedule } from "../lib/domain";
import {
  PaymentScheduleCalculationFailedError,
  PurchaseBillNotBlockedError,
  PurchaseBillNotFoundError,
  ReleaseReasonRequiredError,
} from "../lib/errors.generated";
import {
  planThreeWayMatch,
  createThreeWayMatchEvents,
  updatePOBillingStatus,
} from "../lib/matching";

export interface ReleasePurchaseBillInput {
  id: string;
  releaseReason: string;
}

export async function run(db: Transaction, input: ReleasePurchaseBillInput, ctx: CommandContext) {
  void ctx;
  const purchaseBill = await db
    .selectFrom("PurchaseBill")
    .selectAll()
    .where("id", "=", input.id)
    .forUpdate()
    .executeTakeFirst();
  if (!purchaseBill) {
    return err(new PurchaseBillNotFoundError(input.id));
  }
  const nextStatus = purchaseBillLifecycle.tryTransition(purchaseBill.status, "release");
  if (!nextStatus) {
    return err(new PurchaseBillNotBlockedError(input.id));
  }
  if (!input.releaseReason.trim()) {
    return err(new ReleaseReasonRequiredError(input.id));
  }

  const billLines = await db
    .selectFrom("PurchaseBillLine")
    .selectAll()
    .where("purchaseBillId", "=", input.id)
    .execute();

  const billAmount = billLines.reduce(
    (sum, line) => sum.plus(new Decimal(line.quantity).mul(line.billedUnitPrice ?? 0)),
    new Decimal(0),
  );
  const dueSchedule = calculateDueSchedule(
    purchaseBill.paymentTermSnapshotLines ?? [],
    new Date(purchaseBill.invoiceDate),
    billAmount.toString(),
  );
  if ("error" in dueSchedule) {
    return err(new PaymentScheduleCalculationFailedError(input.id));
  }

  // Collect affected PO IDs from bill lines (direct lookup, no lineage resolution)
  const affectedOrderIds = new Set<string>();
  const resolvedOrderLines = new Map<
    string,
    NonNullable<Awaited<ReturnType<typeof getOrderLine>>>
  >();

  async function getOrderLine(lineId: string) {
    return db
      .selectFrom("PurchaseOrderLine")
      .selectAll()
      .where("id", "=", lineId)
      .executeTakeFirst();
  }

  for (const billLine of billLines) {
    if (billLine.purchaseOrderLineId === null) continue;
    const orderLine = await getOrderLine(billLine.purchaseOrderLineId);
    if (!orderLine) continue;
    resolvedOrderLines.set(billLine.id, orderLine);
    affectedOrderIds.add(orderLine.purchaseOrderId);
  }

  // Create ThreeWayMatchEvent records for THREE_WAY bills
  if (purchaseBill.matchingMode === "THREE_WAY") {
    const postedReceipts = await db
      .selectFrom("GoodsReceipt")
      .selectAll()
      .where("status", "=", "POSTED")
      .execute();
    const postedReceiptIdSet = new Set(postedReceipts.map((r) => r.id));

    const plan = await planThreeWayMatch(db, billLines, resolvedOrderLines, postedReceiptIdSet);
    if (plan.allocations.length > 0) {
      await createThreeWayMatchEvents(db, plan.allocations);
    }
  }

  const released = await db
    .updateTable("PurchaseBill")
    .set({
      status: nextStatus,
      releaseReason: input.releaseReason,
      dueSchedule: dueSchedule.schedule,
      updatedAt: new Date(),
    })
    .where("id", "=", input.id)
    .returningAll()
    .executeTakeFirstOrThrow();

  await updatePOBillingStatus(db, affectedOrderIds);

  return ok({ purchaseBill: released });
}
