import { db, type TailorAnyDBField } from "@tailor-platform/sdk";
import { defaultGqlPermission, defaultPermission } from "../../../shared/modelPermissions";
import { item as itemStub } from "../lib/_db_deps";
import { lot } from "./lot";
import { serialNumber } from "./serialNumber";
import { stockAdjustment } from "./stockAdjustment";
import { storageLocation } from "./storageLocation";

export const LINE_ADJUSTMENT_DIRECTIONS = ["INCREASE", "DECREASE"] as const;

export const STOCK_CATEGORIES = ["AVAILABLE", "BLOCKED"] as const;

export interface CreateStockAdjustmentLineTypeParams<F extends Record<string, TailorAnyDBField>> {
  fields?: F;
}

export function createStockAdjustmentLineType<const F extends Record<string, TailorAnyDBField>>(
  params: CreateStockAdjustmentLineTypeParams<F>,
) {
  return db
    .type("StockAdjustmentLine", {
      stockAdjustmentId: db
        .uuid()
        .relation({
          type: "n-1",
          toward: { type: stockAdjustment },
          backward: "stockAdjustmentLines",
        })
        .description("Foreign key to StockAdjustment"),
      itemId: db
        .uuid()
        .relation({
          type: "n-1",
          toward: { type: itemStub },
          backward: "stockAdjustmentLines",
        })
        .description("Foreign key to Item"),
      storageLocationId: db
        .uuid()
        .relation({
          type: "n-1",
          toward: { type: storageLocation },
          backward: "stockAdjustmentLines",
        })
        .description("Storage location where the adjustment occurs"),
      quantity: db.decimal().description("Quantity (always positive)"),
      adjustmentDirection: db
        .enum([...LINE_ADJUSTMENT_DIRECTIONS] as [string, ...string[]], { optional: true })
        .description(
          "Direction of stock adjustment (required for CORRECTION): INCREASE or DECREASE",
        ),
      fromStockCategory: db
        .enum([...STOCK_CATEGORIES] as [string, ...string[]], { optional: true })
        .description(
          "Source stock category (only used for SCRAP): AVAILABLE (default) or BLOCKED for scrapping blocked stock",
        ),
      unitCost: db.decimal({ optional: true }).description("Cost per unit (optional)"),
      lotId: db
        .uuid({ optional: true })
        .relation({
          type: "n-1",
          toward: { type: lot },
          backward: "stockAdjustmentLines",
        })
        .description("Optional foreign key to Lot for lot-tracked items"),
      serialNumberId: db
        .uuid({ optional: true })
        .relation({
          type: "n-1",
          toward: { type: serialNumber },
          backward: "stockAdjustmentLines",
        })
        .description("Optional foreign key to SerialNumber for serial-tracked items"),
      ...((params.fields ?? {}) as F),
      ...db.fields.timestamps(),
    })
    .permission(defaultPermission)
    .gqlPermission(defaultGqlPermission);
}

export const stockAdjustmentLine = createStockAdjustmentLineType({});
