/**
 * 倉庫相關型別定義
 */

export type WarehouseType = "中央倉庫" | "門市";

/**
 * 門市資訊
 */
export interface Store {
  id: string;
  name: string;
  address: string;
}

export interface Warehouse {
  id: string;
  code: string;
  name: string;
  address?: string;
  description?: string;
  createdAt?: string; // 對應 created_at 但前端可能習慣 camelCase，後端傳回 snake_case，Inertia 會保持原樣。
  // 若後端 Resource 沒轉 camelCase，這裡應該用 snake_case 或在前端轉
  // 為求簡單，我修改 interface 為 snake_case 以匹配 Laravel 預設 Response
  created_at?: string;
  updated_at?: string;
  total_quantity?: number;
  low_stock_count?: number;
  type?: WarehouseType;
}
// 倉庫中的庫存項目
export interface WarehouseInventory {
  id: string; // 庫存項目 ID (Real ID)
  inventoryId?: string; // 舊有別名，相容性保留
  warehouseId: string;
  productId: string;
  productName: string;
  productCode: string;
  unit: string;
  quantity: number;
  safetyStock: number | null;
  status?: '正常' | '接近' | '低於'; // 後端可能回傳的狀態
  batchNumber: string; // 批號 (Mock for now)
  expiryDate: string;
  lastInboundDate: string | null;
  lastOutboundDate: string | null;
}

export type TransferOrderStatus = "待處理" | "處理中" | "已完成" | "已取消";

export interface TransferOrder {
  id: string;
  orderNumber: string;
  sourceWarehouseId: string;
  sourceWarehouseName: string;
  targetWarehouseId: string;
  targetWarehouseName: string;
  productId: string;
  productName: string;
  batchNumber: string;
  quantity: number;
  transferDate: string;
  status: TransferOrderStatus;
  notes?: string;
  createdAt: string;
}

export interface Product {
  id: string;
  name: string;
  type: string;
  unit?: string;
}

export interface WarehouseStats {
  totalQuantity: number;
  lowStockCount: number;
  replenishmentNeeded: number;
}

/**
 * 安全庫存設定
 */
export interface SafetyStockSetting {
  id: string;
  warehouseId: string;
  productId: string;
  productName: string;
  productType: string;
  safetyStock: number;
  unit?: string;
  createdAt: string;
  updatedAt: string;
}

/**
 * 安全庫存狀態
 */
export type SafetyStockStatus = "正常" | "接近" | "低於";

/**
 * 入庫原因類型
 */
export type InboundReason =
  | "期初建檔"
  | "盤點調整"
  | "實際入庫未走採購流程"
  | "生產加工成品入庫"
  | "其他";

/**
 * 庫存異動類型
 */
export type TransactionType =
  | "手動入庫"
  | "手動出庫"
  | "撥補入庫"
  | "撥補出庫"
  | "盤點調整"
  | "其他";

/**
 * 庫存異動記錄
 */
export interface InventoryTransaction {
  id: string;
  warehouseId: string;
  warehouseName: string;
  productId: string;
  productName: string;
  batchNumber: string;
  quantity: number; // 正數為入庫，負數為出庫
  transactionType: TransactionType;
  reason?: string;
  notes?: string;
  expiryDate?: string;
  operatorName?: string;
  createdAt: string;
}

/**
 * 入庫明細項目
 */
export interface InboundItem {
  tempId: string; // 臨時ID，用於表單編輯
  productId: string;
  productName: string;
  quantity: number;
  unit: string;
  baseUnit?: string;
  largeUnit?: string;
  conversionRate?: number;
  selectedUnit?: 'base' | 'large';
  batchNumber?: string;
  expiryDate?: string;
}

/**
 * 入庫記錄
 */
export interface InboundRecord {
  id: string;
  warehouseId: string;
  warehouseName: string;
  inboundDate: string;
  reason: InboundReason;
  notes?: string;
  items: InboundItem[];
  operatorName?: string;
  createdAt: string;
}

// 前端庫存列表用的商品型別
export interface InventoryProduct {
  id: string;
  code: string;
  name: string;
  specification: string;
  base_unit: string;
  current_stock: number;
  safety_stock: number;
  location: string;
  last_updated: string | null;
}

export type InventoryTransactionType =
  | "adjustment"
  | "purchase_in"
  | "sales_out"
  | "return_in"
  | "return_out"
  | "transfer_in"
  | "transfer_out";

export type InventoryOperation = "add" | "subtract" | "set";