import {
  db,
  type TailorAnyDBField,
  type TailorAnyDBType,
  unsafeAllowAllGqlPermission,
  unsafeAllowAllTypePermission,
} from "@tailor-platform/sdk";
import { site as siteStub } from "../lib/_db_deps";

export const BASE_WAREHOUSE_STATUSES = ["ACTIVE", "INACTIVE"] as const;

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

export function createWarehouseType<const F extends Record<string, TailorAnyDBField>>(
  params: CreateWarehouseTypeParams<F>,
) {
  return db
    .type("Warehouse", {
      name: db.string().description("Warehouse display name, unique within site"),
      siteId: db
        .uuid()
        .relation({
          type: "n-1",
          toward: { type: params.siteType },
          backward: "warehouses",
        })
        .description("Foreign key to Site"),
      storageCondition: db
        .string({ optional: true })
        .description("Storage condition: ambient, cold, hazardous"),
      capacity: db.float({ optional: true }).description("Optional storage capacity"),
      status: db
        .enum([...BASE_WAREHOUSE_STATUSES] as [string, ...string[]])
        .description("Lifecycle status: ACTIVE, INACTIVE"),
      ...((params.fields ?? {}) as F),
      ...db.fields.timestamps(),
    })
    .permission(unsafeAllowAllTypePermission)
    .gqlPermission(unsafeAllowAllGqlPermission);
}

export const warehouse = createWarehouseType({ siteType: siteStub });
