import { ProductWarehouse } from '@/types/api/inventory/product-warehouse';
import { Warehouse } from '@/types/api/master-data/warehouse';

export const getWarehouseScopeLabel = (
  warehouse?: Warehouse | null
): string => {
  if (!warehouse) {
    return 'Gudang';
  }

  if (warehouse.type === 'KANDANG') {
    return warehouse.kandang?.name
      ? `Kandang ${warehouse.kandang.name}`
      : 'Gudang Kandang';
  }

  if (warehouse.type === 'LOKASI') {
    return 'Gudang Farm';
  }

  return 'Gudang Area';
};

export const getProductWarehouseOptionLabel = (
  productWarehouse?: ProductWarehouse | null
): string => {
  if (!productWarehouse) {
    return '';
  }

  const productName = productWarehouse.product?.name || 'Produk';
  const warehouseName = productWarehouse.warehouse?.name || 'Gudang';
  const warehouseScope = getWarehouseScopeLabel(productWarehouse.warehouse);

  return `${productName} • ${warehouseName} (${warehouseScope})`;
};

export const isProductWarehouseSelectableForKandang = (
  productWarehouse: ProductWarehouse,
  kandangId?: number | null
): boolean => {
  const warehouse = productWarehouse.warehouse;

  if (!warehouse) {
    return false;
  }

  if (warehouse.type === 'LOKASI') {
    return true;
  }

  if (warehouse.type === 'KANDANG') {
    return (
      Boolean(kandangId) &&
      (warehouse.kandang?.id === kandangId ||
        productWarehouse.project_flock_kandang?.kandang_id === kandangId)
    );
  }

  return false;
};
