import {
  createGetDB,
  type Generated,
  type Timestamp,
  type NamespaceDB,
  type NamespaceInsertable,
  type NamespaceSelectable,
  type NamespaceTable,
  type NamespaceTableName,
  type NamespaceTransaction,
  type NamespaceUpdateable,
} from "@tailor-platform/sdk/kysely";

export interface Namespace {
  "main-db": {
    BillOfMaterial: {
      id: Generated<string>;
      parentItemId: string;
      companyId: string;
      siteId: string | null;
      bomType: "MANUFACTURE" | "PHANTOM" | "KIT";
      effectivityStartDate: Timestamp | null;
      effectivityEndDate: Timestamp | null;
      defaultSelection: boolean | null;
      revisionNumber: string | null;
      status: "DRAFT" | "ACTIVE" | "INACTIVE";
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    BillOfMaterialLine: {
      id: Generated<string>;
      billOfMaterialId: string;
      itemId: string;
      requiredQuantity: number;
      unitOfMeasure: string | null;
      scrapAssumption: number | null;
      isSubassembly: boolean | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    CostVarianceLine: {
      id: Generated<string>;
      costSummaryId: string;
      varianceType: "MATERIAL_PRICE" | "MATERIAL_USAGE" | "LABOR_RATE" | "LABOR_EFFICIENCY" | "MACHINE_RATE" | "MACHINE_EFFICIENCY" | "SCRAP" | "YIELD";
      amount: number;
      accountReference: string | null;
      variancePercentage: number | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    ManufacturingCostLine: {
      id: Generated<string>;
      costSummaryId: string;
      costType: string;
      plannedAmount: number;
      actualAmount: number;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    ManufacturingCostSettlementRecord: {
      id: Generated<string>;
      costSummaryId: string;
      settlementDate: Timestamp;
      settlementReference: string;
      acknowledgedDate: Timestamp | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    ManufacturingCostSummary: {
      id: Generated<string>;
      productionOrderId: string;
      plannedMaterialCost: number;
      plannedLaborCost: number;
      plannedMachineCost: number;
      plannedOverheadCost: number;
      actualMaterialCost: number;
      actualLaborCost: number;
      actualMachineCost: number;
      actualOverheadCost: number;
      currencyCode: string;
      reviewedDate: Timestamp | null;
      reviewerNotes: string | null;
      status: "COLLECTING" | "PENDING_VARIANCE_REVIEW" | "VARIANCE_REVIEWED" | "SETTLED";
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    ProductionOrder: {
      id: Generated<string>;
      orderedItemId: string;
      companyId: string;
      siteId: string;
      plannedQuantity: number;
      plannedStartDate: Timestamp;
      plannedEndDate: Timestamp;
      selectedBomVersionId: string | null;
      selectedRoutingRevisionId: string | null;
      status: "DRAFT" | "RELEASED" | "CANCELLED" | "IN_PROGRESS" | "COMPLETED" | "TECHNICALLY_COMPLETE" | "CLOSED";
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    ProductionOrderBomSnapshot: {
      id: Generated<string>;
      productionOrderId: string;
      parentItemId: string;
      bomType: string;
      snapshotData: string | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    ProductionOrderCostBaseline: {
      id: Generated<string>;
      productionOrderId: string;
      plannedMaterialCost: number;
      plannedLaborCost: number;
      plannedMachineCost: number;
      plannedOverheadCost: number;
      currencyCode: string;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    ProductionOrderMaterialRequirement: {
      id: Generated<string>;
      productionOrderId: string;
      itemId: string;
      requiredQuantity: number;
      unitOfMeasure: string | null;
      plannedUnitCost: number | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    ProductionOrderRoutingSnapshot: {
      id: Generated<string>;
      productionOrderId: string;
      routingId: string;
      snapshotData: string | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    Routing: {
      id: Generated<string>;
      parentItemId: string;
      companyId: string;
      siteId: string | null;
      revisionNumber: string | null;
      status: "DRAFT" | "ACTIVE" | "INACTIVE";
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    RoutingOperation: {
      id: Generated<string>;
      routingId: string;
      sequenceNumber: number;
      operationDescription: string | null;
      workCenterId: string;
      standardSetupTime: number;
      standardRunTime: number;
      operatorInstructions: string | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    WorkCenter: {
      id: Generated<string>;
      code: string;
      companyId: string;
      siteId: string | null;
      status: "DRAFT" | "INACTIVE" | "ACTIVE";
      capacityAssumptions: number;
      laborRate: number | null;
      machineRate: number | null;
      calendarReference: string | null;
      overheadAbsorptionMethod: "PERCENT_OF_LABOR_COST" | "PERCENT_OF_MACHINE_COST" | "FIXED_AMOUNT_PER_GOOD_UNIT" | null;
      overheadAbsorptionCurrency: string | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    WorkOrder: {
      id: Generated<string>;
      productionOrderId: string;
      routingOperationSequenceNumber: number;
      workCenterId: string;
      plannedQuantity: number;
      completedQuantity: number;
      scrapQuantity: number;
      actualSetupTime: number;
      actualRunTime: number;
      actualStartDate: Timestamp | null;
      pauseReason: string | null;
      executionNotes: string | null;
      status: "PENDING" | "IN_PROGRESS" | "PAUSED" | "COMPLETE" | "CANCELLED";
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    WorkOrderExecutionEvent: {
      id: Generated<string>;
      workOrderId: string;
      eventType: "STARTED" | "PAUSED" | "RESUMED" | "PROGRESS_REPORTED" | "COMPLETED";
      timestamp: Timestamp;
      quantity: number | null;
      timeValue: number | null;
      scrapValue: number | null;
      notes: string | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }
  }
}

export const getDB = createGetDB<Namespace>();

export type DB<N extends keyof Namespace = keyof Namespace> = NamespaceDB<Namespace, N>;

export type Transaction<K extends keyof Namespace | DB = keyof Namespace> =
  NamespaceTransaction<Namespace, K>;

type TableName = NamespaceTableName<Namespace>;
export type Table<T extends TableName> = NamespaceTable<Namespace, T>;

export type Insertable<T extends TableName> = NamespaceInsertable<Namespace, T>;
export type Selectable<T extends TableName> = NamespaceSelectable<Namespace, T>;
export type Updateable<T extends TableName> = NamespaceUpdateable<Namespace, T>;
