import {
  type CommonReservedFields,
  type NoReservedFields,
  defaultGqlPermission,
  defaultPermission,
} from "@tailor-platform/erp-kit/core";
import { db, type TailorAnyDBField, type TailorAnyDBType } from "@tailor-platform/sdk";
import { inventoryExecution } from "./inventoryExecution";
import { lot } from "./lot";
import { serialNumber } from "./serialNumber";
import { STOCK_TYPES } from "./stockLevel";
import { storageLocation } from "./storageLocation";

export const LEDGER_DC_INDICATORS = ["DR", "CR"] as const;

const builtins = (params: { itemType?: TailorAnyDBType }) => {
  const itemId = db.uuid().description("Foreign key to Item");
  return {
    inventoryExecutionId: db
      .uuid()
      .relation({
        type: "n-1",
        toward: { type: inventoryExecution },
        backward: "ledgerEntries",
      })
      .description(
        "Foreign key to InventoryExecution. Source documents are reached through the execution.",
      ),
    dcIndicator: db
      .enum([...LEDGER_DC_INDICATORS] as [string, ...string[]])
      .description("Debit/Credit indicator (DR or CR) for accounting integration"),
    itemId: params.itemType
      ? itemId.relation({
          type: "n-1",
          toward: { type: params.itemType },
          backward: "inventoryLedgerEntries",
        })
      : itemId,
    storageLocationId: db
      .uuid()
      .relation({
        type: "n-1",
        toward: { type: storageLocation },
        backward: "inventoryLedgerEntries",
      })
      .description("The storage location where the stock change occurs"),
    stockType: db
      .enum([...STOCK_TYPES] as [string, ...string[]])
      .description("Stock type affected by this ledger row: AVAILABLE, BLOCKED, or IN_TRANSIT"),
    quantity: db
      .decimal()
      .description(
        "Quantity in the item's primary unit (always positive); dcIndicator determines direction",
      ),
    executedAt: db.datetime().description("Timestamp when the stock change was executed"),
    lotId: db
      .uuid({ optional: true })
      .relation({
        type: "n-1",
        toward: { type: lot },
        backward: "inventoryLedgerEntries",
      })
      .description("Optional foreign key to Lot for lot-tracked items"),
    serialNumberId: db
      .uuid({ optional: true })
      .relation({
        type: "n-1",
        toward: { type: serialNumber },
        backward: "inventoryLedgerEntries",
      })
      .description("Optional foreign key to SerialNumber for serial-tracked items"),
  };
};

type ReservedFields = keyof ReturnType<typeof builtins> | CommonReservedFields;

export interface CreateInventoryLedgerTypeParams<F extends Record<string, TailorAnyDBField>> {
  fields?: NoReservedFields<F, ReservedFields>;
  itemType?: TailorAnyDBType;
}

export function createInventoryLedgerType<const F extends Record<string, TailorAnyDBField>>(
  params: CreateInventoryLedgerTypeParams<F>,
) {
  return db
    .type("InventoryLedger", {
      ...builtins(params),
      ...((params.fields ?? {}) as F),
      createdAt: db.fields.timestamps().createdAt,
    })
    .permission(defaultPermission)
    .gqlPermission(defaultGqlPermission);
}

export const inventoryLedger = createInventoryLedgerType({});
