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

export const BASE_WORK_CENTER_STATUSES = ["DRAFT", "ACTIVE", "INACTIVE"] as const;

export const OVERHEAD_ABSORPTION_METHODS = [
  "PERCENT_OF_LABOR_COST",
  "PERCENT_OF_MACHINE_COST",
  "FIXED_AMOUNT_PER_GOOD_UNIT",
] as const;

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

export function createWorkCenterType<const F extends Record<string, TailorAnyDBField>>(
  params: CreateWorkCenterTypeParams<F>,
) {
  return db
    .type("WorkCenter", {
      code: db.string().description("Work-center code, unique within company and site scope"),
      companyId: db
        .uuid()
        .relation({
          type: "n-1",
          toward: { type: params.companyType },
          backward: "workCenters",
        })
        .description("Foreign key to Company"),
      siteId: db
        .uuid({ optional: true })
        .relation({
          type: "n-1",
          toward: { type: params.siteType },
          backward: "workCenters",
        })
        .description("Foreign key to Site; required when company uses site-level work centers"),
      status: db
        .enum([...BASE_WORK_CENTER_STATUSES] as [string, ...string[]])
        .description("Lifecycle status: DRAFT, ACTIVE, INACTIVE"),
      capacityAssumptions: db
        .float()
        .description(
          "Capacity assumption expressed in a consistent planning unit; must be positive",
        ),
      laborRate: db
        .float({ optional: true })
        .description("Standard labor rate per planning unit; must be zero or greater"),
      machineRate: db
        .float({ optional: true })
        .description("Standard machine rate per planning unit; must be zero or greater"),
      calendarReference: db
        .string({ optional: true })
        .description("Calendar identifier; required when activating the work center"),
      overheadAbsorptionMethod: db
        .enum([...OVERHEAD_ABSORPTION_METHODS] as [string, ...string[]], {
          optional: true,
        })
        .description(
          "Method for absorbing overhead: PERCENT_OF_LABOR_COST, PERCENT_OF_MACHINE_COST, or FIXED_AMOUNT_PER_GOOD_UNIT",
        ),
      overheadAbsorptionCurrency: db
        .string({ optional: true })
        .description(
          "ISO 4217 currency code for overhead absorption; required when method is FIXED_AMOUNT_PER_GOOD_UNIT",
        ),
      ...((params.fields ?? {}) as F),
      ...db.fields.timestamps(),
    })
    .indexes({
      fields: ["code", "companyId", "siteId"],
      unique: true,
      name: "work_center_code_company_site_idx",
    })
    .permission(unsafeAllowAllTypePermission)
    .gqlPermission(unsafeAllowAllGqlPermission);
}

export const workCenter = createWorkCenterType({ companyType: companyStub, siteType: siteStub });
