import type { defineModule as defineBusinessPartnerModule } from "../../business-partner";
import type { Transaction } from "../generated/kysely-tailordb";
type BusinessPartnerModule = ReturnType<typeof defineBusinessPartnerModule>;
type BusinessPartnerQueries = BusinessPartnerModule["queries"];
import { err, ok, type CommandContext } from "@tailor-platform/erp-kit/core";
import { purchaseOrderLifecycle } from "../db/purchaseOrder.lifecycle.generated";
import {
  DuplicateExternalSupplierOrderReferenceError,
  EmptyPurchaseOrderLinesError,
  PartnerNotSupplierError,
  PurchaseOrderNotDraftError,
  PurchaseOrderNotFoundError,
} from "../lib/errors.generated";

export interface SubmitPurchaseOrderInput {
  id: string;
}

export async function run(
  db: Transaction,
  input: SubmitPurchaseOrderInput,
  ctx: CommandContext,
  businessPartnerQueries: Pick<BusinessPartnerQueries, "getPartnerRole">,
) {
  const purchaseOrder = await db
    .selectFrom("PurchaseOrder")
    .selectAll()
    .where("id", "=", input.id)
    .forUpdate()
    .executeTakeFirst();
  if (!purchaseOrder) {
    return err(new PurchaseOrderNotFoundError(input.id));
  }
  const nextOrderStatus = purchaseOrderLifecycle.tryTransition(purchaseOrder.orderStatus, "submit");
  if (!nextOrderStatus) {
    return err(new PurchaseOrderNotDraftError(input.id));
  }

  const lines = await db
    .selectFrom("PurchaseOrderLine")
    .selectAll()
    .where("purchaseOrderId", "=", input.id)
    .execute();

  if (lines.length === 0) {
    return err(new EmptyPurchaseOrderLinesError(input.id));
  }
  const { partnerRole } = (
    await businessPartnerQueries.getPartnerRole(
      db,
      { partnerId: purchaseOrder.supplierId, role: "SUPPLIER" },
      ctx,
    )
  ).value;
  if (!partnerRole) {
    return err(new PartnerNotSupplierError(purchaseOrder.supplierId));
  }
  if (purchaseOrder.externalSupplierOrderReference) {
    const duplicate = await db
      .selectFrom("PurchaseOrder")
      .selectAll()
      .where("companyId", "=", purchaseOrder.companyId)
      .where("externalSupplierOrderReference", "=", purchaseOrder.externalSupplierOrderReference)
      .executeTakeFirst();
    if (duplicate && duplicate.id !== input.id) {
      return err(
        new DuplicateExternalSupplierOrderReferenceError(
          purchaseOrder.externalSupplierOrderReference,
        ),
      );
    }
  }

  const submitted = await db
    .updateTable("PurchaseOrder")
    .set({ orderStatus: nextOrderStatus })
    .where("id", "=", input.id)
    .returningAll()
    .executeTakeFirstOrThrow();

  return ok({ purchaseOrder: submitted });
}
