import { db, type TailorAnyDBType } from "@tailor-platform/sdk";
import { defaultGqlPermission, defaultPermission } from "../../../shared/modelPermissions";
import { manufacturingCostSummaryLifecycle } from "./manufacturingCostSummary.lifecycle.generated";
import { productionOrder } from "./productionOrder";

export interface CreateManufacturingCostSummaryTypeParams {
  productionOrderType: TailorAnyDBType;
}

export function createManufacturingCostSummaryType(
  params: CreateManufacturingCostSummaryTypeParams,
) {
  return db
    .type("ManufacturingCostSummary", {
      productionOrderId: db
        .uuid()
        .relation({
          type: "1-1",
          toward: { type: params.productionOrderType },
          backward: "manufacturingCostSummary",
        })
        .description("Foreign key to ProductionOrder"),
      plannedMaterialCost: db.float().description("Planned material cost"),
      plannedLaborCost: db.float().description("Planned labor cost"),
      plannedMachineCost: db.float().description("Planned machine cost"),
      plannedOverheadCost: db.float().description("Planned overhead cost"),
      actualMaterialCost: db.float().description("Actual material cost"),
      actualLaborCost: db.float().description("Actual labor cost"),
      actualMachineCost: db.float().description("Actual machine cost"),
      actualOverheadCost: db.float().description("Actual overhead cost"),
      currencyCode: db.string().description("Currency code (e.g. USD, EUR)"),
      reviewedDate: db
        .datetime({ optional: true })
        .description("Date when cost summary was reviewed"),
      reviewerNotes: db.string({ optional: true }).description("Notes from the reviewer"),
      status: db
        .enum(manufacturingCostSummaryLifecycle.states)
        .description(
          "Lifecycle status: COLLECTING, PENDING_VARIANCE_REVIEW, VARIANCE_REVIEWED, SETTLED",
        ),
      ...db.fields.timestamps(),
    })
    .permission(defaultPermission)
    .gqlPermission(defaultGqlPermission);
}

export const manufacturingCostSummary = createManufacturingCostSummaryType({
  productionOrderType: productionOrder,
});
