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

export type WarehouseType = "standard" | "production" | "retail" | "vending" | "transit" | "quarantine";

/**
 * 門市資訊
 */
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?: string;
  updated_at?: string;
  total_quantity?: number;
  low_stock_count?: number;
  type?: WarehouseType;
  license_plate?: string; // 車牌號碼 (移動倉)
  driver_name?: string;   // 司機姓名 (移動倉)
  book_stock?: number;
  available_stock?: number;
  book_amount?: number;
  abnormal_amount?: number;
}
// 倉庫中的庫存項目
export interface WarehouseInventory {
  id: string; // 庫存項目 ID (Real ID)
  inventoryId?: string; // 舊有別名，相容性保留
  warehouseId: string;
  productId: string;
  productName: string;
  productCode: string;
  unit: string;
  quantity: number;
  unit_cost?: number; // 單位成本
  total_value?: number; // 總價值
  safetyStock: number | null;
  status?: '正常' | '低於'; // 後端可能回傳的狀態
  batchNumber: string; // 批號 (Mock for now)
  location?: string; // 儲位/貨道
  expiryDate: string;
  lastInboundDate: string | null;
  lastOutboundDate: string | null;
}

// 依照商品分組的庫存項目 (用於庫存列表)
export interface GroupedInventory {
  productId: string;
  productName: string;
  productCode: string;
  baseUnit: string;
  totalQuantity: number;
  totalValue?: number; // 總價值總計
  safetyStock: number | null; // 以商品層級顯示的安全庫存
  status: '正常' | '低於';
  batches: WarehouseInventory[]; // 該商品下的所有批號庫存
}

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;
  totalValue?: number; // 倉庫總值
  abnormalValue?: 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;
  isNew?: boolean;
}

/**
 * 安全庫存狀態
 */
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; // 正數為入庫，負數為出庫
  unit_cost?: 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_cost?: number; // 入庫單價
  unit: string;
  baseUnit?: string;
  largeUnit?: string;
  conversionRate?: number;
  selectedUnit?: 'base' | 'large';
  batchMode?: 'existing' | 'new' | 'none'; // 批號模式
  inventoryId?: string; // 選擇現有批號時的 ID
  batchNumber?: string;
  location?: string; // 儲位/貨道
  originCountry?: 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";