// Master Data DTOs (docs/11-BACKEND-PHASE1.md §2; FR-MD-01..08).
import { EntityStatus } from "@/types/common"

export type ItemType = "Stocked" | "NonStocked" | "Service"
export type TrackingMode = "None" | "Batch" | "Serial"

export interface ItemListItem {
  itemId: number
  sku: string
  name: string
  categoryId: number
  brandId?: number | null
  baseUomId: number
  defaultVendorId: number | null
  itemType: ItemType
  trackingMode: TrackingMode
  taxClass: string | null
  status: EntityStatus
}

export interface ItemReorderSetting {
  warehouseId: number
  reorderPoint: number
  reorderQty: number
}

export interface UomConversion {
  conversionId: number
  fromUom: number
  toUom: number
  factor: number
}

/**
 * Full Item resource (docs/11 §2.1 `GET /items/{itemId}`). `conversions` is a
 * deviation: the documented example response only shows `reorder` inline, and
 * conversions are otherwise reachable only via `PUT /items/{itemId}/uom-conversions`
 * (which returns them but has no matching GET). Embedding them here — like the
 * assumed `GET /grns`/`GET /grns/{id}` reads elsewhere in this app — lets the Item
 * detail screen show current conversions before editing; flag this to whoever
 * implements the real endpoint (docs/10 §4 already lists UOMConversion as its own
 * entity, so the full resource plausibly carries it).
 */
export interface Item {
  itemId: number
  sku: string
  name: string
  description: string | null
  categoryId: number
  brandId?: number | null
  baseUomId: number
  defaultVendorId: number | null
  itemType: ItemType
  trackingMode: TrackingMode
  taxClass: string | null
  status: EntityStatus
  reorder: ItemReorderSetting[]
  conversions: UomConversion[]
  /** Quantity captured at creation time (e.g. from the variant builder). Not wired into the Stock Core ledger — informational only. */
  initialQty?: number | null
  createdAt: string
  updatedAt: string | null
}

export interface CreateItemRequest {
  sku: string
  name: string
  description?: string | null
  categoryId: number
  brandId?: number | null
  baseUomId: number
  defaultVendorId?: number | null
  itemType: ItemType
  trackingMode: TrackingMode
  taxClass?: string | null
  initialQty?: number | null
}

export type UpdateItemRequest = CreateItemRequest

export interface UpdateItemReorderRequest {
  settings: ItemReorderSetting[]
}

export interface UpdateUomConversionsRequest {
  conversions: { fromUom: number; toUom: number; factor: number }[]
}

export interface UpdateUomConversionsResponse {
  itemId: number
  baseUomId: number
  conversions: UomConversion[]
}

export interface Warehouse {
  warehouseId: number
  code: string
  name: string
}

export interface Bin {
  binId: number
  warehouseId: number
  code: string
  binType: string | null
}

export interface Vendor {
  vendorId: number
  code: string
  name: string
  terms: string | null
  taxReg: string | null
  currency: string
  status: EntityStatus
  createdAt: string
  updatedAt: string | null
}

export interface Uom {
  uomId: number
  name: string
}

export interface CreateUomRequest {
  name: string
}

export interface Category {
  categoryId: number
  name: string
  parentId: number | null
  createdAt: string
}

export interface CategoryTreeNode extends Category {
  children: CategoryTreeNode[]
}

export interface CreateCategoryRequest {
  name: string
  parentId?: number | null
}

export interface UpdateCategoryRequest {
  name: string
}

export interface Brand {
  brandId: number
  name: string
  createdAt: string
}

export interface CreateBrandRequest {
  name: string
}

export interface UpdateBrandRequest {
  name: string
}

export interface VariantCategory {
  variantCategoryId: number
  name: string
  createdAt: string
}

export interface CreateVariantCategoryRequest {
  name: string
}

export interface UpdateVariantCategoryRequest {
  name: string
}
