"use server";

import { lookupByMd5, type TapeMatch } from "@/server/repo";

export async function identifyTape(
  md5: string,
  sizeBytes: number
): Promise<TapeMatch[]> {
  // Validate input shape
  if (!/^[0-9a-f]{32}$/i.test(md5)) return [];
  if (!Number.isFinite(sizeBytes) || sizeBytes < 0) return [];

  const matches = await lookupByMd5(md5);

  // If multiple matches and size can disambiguate, filter by size
  if (matches.length > 1) {
    const bySz = matches.filter((m) => m.sizeBytes === sizeBytes);
    if (bySz.length > 0) return bySz;
  }

  return matches;
}
