export const NONE_SELECT_VALUE = "__none__";

/** 低库存阈值，amount 低于此值且大于 0 时显示「低库存」 */
export const LOW_STOCK_THRESHOLD = 100;

export interface SelectOption {
  value: string;
  label: string;
}

export interface ContentMixtureRow {
  germplasm_id: string;
  cross_id: string;
  mixture_percentage: string;
}

export type TransactionActionType = "in" | "out" | "transfer" | "split" | "consume";

export interface SeedLotContentMixtureRecord {
  crossDbId?: string | null;
  crossName?: string | null;
  cross_id?: string | null;
  cross_name?: string | null;
  germplasmDbId?: string | null;
  germplasmName?: string | null;
  germplasm_id?: string | null;
  germplasm_name?: string | null;
  mixturePercentage?: number | null;
  mixture_percentage?: number | null;
}

export interface SeedLotRecord {
  id: string;
  seedLotDbId: string;
  seedLotName: string | null;
  seed_lot_name: string | null;
  name: string | null;
  seedLotDescription: string | null;
  seed_lot_description: string | null;
  description: string | null;
  amount: number | null;
  units: string | null;
  createdDate: string | null;
  created_date: string | null;
  lastUpdated: string | null;
  last_updated: string | null;
  sourceCollection: string | null;
  source_collection: string | null;
  storageLocation: string | null;
  storage_location: string | null;
  locationDbId: string | null;
  location_id: string | null;
  locationName: string | null;
  location_name: string | null;
  programDbId: string | null;
  program_id: string | null;
  programName: string | null;
  program_name: string | null;
  germplasmDbId?: string | null;
  contentMixture?: SeedLotContentMixtureRecord[] | null;
  content_mixture?: SeedLotContentMixtureRecord[] | null;
}

export interface SeedLotTransactionRecord {
  id: string;
  transactionDbId?: string | null;
  amount: number | null;
  units: string | null;
  fromSeedLotDbId?: string | null;
  from_seed_lot_id?: string | null;
  toSeedLotDbId?: string | null;
  to_seed_lot_id?: string | null;
  transactionDescription?: string | null;
  description?: string | null;
  transactionTimestamp?: string | null;
  timestamp?: string | null;
  from_seed_lot_name?: string | null;
  to_seed_lot_name?: string | null;
}

export type StockStatus = "depleted" | "low" | "sufficient";

export interface SeedLotQuery {
  program_id?: string;
  location_id?: string;
  name?: string;
  stock_status?: StockStatus | typeof NONE_SELECT_VALUE;
}

export interface SeedLotTransactionQuery {
  seed_lot_id?: string;
  program_id?: string;
  location_id?: string;
  date_from?: string;
  date_to?: string;
  keyword?: string;
}

export type SeedLotDetailTab = "mixture" | "transactions";

export function resolveStockStatus(amount: number | null | undefined): StockStatus {
  const value = Number(amount ?? 0);
  if (value <= 0) return "depleted";
  if (value < LOW_STOCK_THRESHOLD) return "low";
  return "sufficient";
}

export const STOCK_STATUS_LABEL: Record<StockStatus, string> = {
  depleted: "耗尽",
  low: "低库存",
  sufficient: "充足",
};

export const TRANSACTION_ACTION_LABEL: Record<TransactionActionType, string> = {
  in: "入库",
  out: "出库",
  transfer: "转移",
  split: "分装",
  consume: "消耗/报废",
};
