import {
  db,
  type TailorAnyDBField,
  type TailorAnyDBType,
  unsafeAllowAllGqlPermission,
  unsafeAllowAllTypePermission,
} from "@tailor-platform/sdk";
import {
  businessPartner as businessPartnerStub,
  currency as currencyStub,
  item as itemStub,
  unit as unitStub,
} from "../lib/_db_deps";
import { channel } from "./channel";
import { salesPriceList } from "./salesPriceList";

export const SALES_PRICE_RULE_STATUSES: [string, string, string] = ["DRAFT", "ACTIVE", "INACTIVE"];

export interface CreateSalesPriceRuleTypeParams<F extends Record<string, TailorAnyDBField>> {
  fields?: F;
  salesPriceListType?: TailorAnyDBType;
  itemType?: TailorAnyDBType;
  customerType?: TailorAnyDBType;
  channelType?: TailorAnyDBType;
  currencyType?: TailorAnyDBType;
  unitType?: TailorAnyDBType;
}

export function createSalesPriceRuleType<const F extends Record<string, TailorAnyDBField>>(
  params: CreateSalesPriceRuleTypeParams<F>,
) {
  return db
    .type("SalesPriceRule", {
      salesPriceListId: db
        .uuid()
        .relation({
          type: "n-1",
          toward: { type: params.salesPriceListType ?? salesPriceList },
          backward: "salesPriceRules",
        })
        .description("Foreign key to SalesPriceList"),
      itemId: db
        .uuid()
        .relation({
          type: "n-1",
          toward: { type: params.itemType ?? itemStub },
          backward: "salesPriceRules",
        })
        .description("Foreign key to Item"),
      customerId: db
        .uuid({ optional: true })
        .relation({
          type: "n-1",
          toward: { type: params.customerType ?? businessPartnerStub },
          backward: "salesPriceRules",
        })
        .description("Optional scoped customer"),
      channelId: db
        .uuid({ optional: true })
        .relation({
          type: "n-1",
          toward: { type: params.channelType ?? channel },
          backward: "salesPriceRules",
        })
        .description("Optional scoped channel"),
      currencyId: db
        .uuid()
        .relation({
          type: "n-1",
          toward: { type: params.currencyType ?? currencyStub },
          backward: "salesPriceRules",
        })
        .description("Foreign key to Currency"),
      unitId: db
        .uuid()
        .relation({
          type: "n-1",
          toward: { type: params.unitType ?? unitStub },
          backward: "salesPriceRules",
        })
        .description("Foreign key to Unit"),
      minimumQuantity: db.float().description("Minimum quantity break"),
      unitPrice: db.float().description("Rule unit price"),
      effectiveFrom: db.date({ optional: true }).description("Optional effective start date"),
      effectiveTo: db.date({ optional: true }).description("Optional effective end date"),
      priority: db.int().description("Rule tie-break priority"),
      status: db.enum(SALES_PRICE_RULE_STATUSES).description("Rule lifecycle status"),
      notes: db.string({ optional: true }).description("Optional notes"),
      ...((params.fields ?? {}) as F),
      ...db.fields.timestamps(),
    })
    .permission(unsafeAllowAllTypePermission)
    .gqlPermission(unsafeAllowAllGqlPermission);
}

export const salesPriceRule = createSalesPriceRuleType({});
