import { defaultGqlPermission, defaultPermission } from "@tailor-platform/erp-kit/core";
import { db } from "@tailor-platform/sdk";
import { manufacturingCostSummaryLifecycle } from "./manufacturingCostSummary.lifecycle.generated";
import { productionOrder } from "./productionOrder";

export function createManufacturingCostSummaryType() {
  return db
    .type("ManufacturingCostSummary", {
      productionOrderId: db
        .uuid()
        .relation({
          type: "1-1",
          toward: { type: productionOrder },
          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();
