import { defaultGqlPermission, defaultPermission } from "@tailor-platform/erp-kit/core";
import { db } from "@tailor-platform/sdk";
import { workOrder } from "./workOrder";

export const EXECUTION_EVENT_TYPES = [
  "STARTED",
  "PAUSED",
  "RESUMED",
  "PROGRESS_REPORTED",
  "COMPLETED",
] as const;

export function createWorkOrderExecutionEventType() {
  return db
    .type("WorkOrderExecutionEvent", {
      workOrderId: db
        .uuid()
        .relation({
          type: "n-1",
          toward: { type: workOrder },
          backward: "executionEvents",
        })
        .description("Foreign key to WorkOrder"),
      eventType: db
        .enum(EXECUTION_EVENT_TYPES)
        .description("Event type: STARTED, PAUSED, RESUMED, PROGRESS_REPORTED, COMPLETED"),
      timestamp: db.datetime().description("When the event occurred"),
      quantity: db
        .float({ optional: true })
        .description("Quantity associated with the event, must be >= 0"),
      timeValue: db
        .float({ optional: true })
        .description("Time value associated with the event, must be >= 0"),
      scrapValue: db
        .float({ optional: true })
        .description("Scrap value associated with the event, must be >= 0"),
      notes: db.string({ optional: true }).description("Optional notes about the event"),
      ...db.fields.timestamps(),
    })
    .permission(defaultPermission)
    .gqlPermission(defaultGqlPermission);
}

export const workOrderExecutionEvent = createWorkOrderExecutionEventType();
