import { SigviewRecord as Signature } from "@/lib/pbtypes";

const oneOutOfTen = [
  "Minmatar Contracted Bio-Farm",
  "Old Meanie - Cultivation Center",
  "Pith Robux Asteroid Mining & Co.",
  "Sansha Military Outpost",
  "Serpentis Drug Outlet",
];
const twoOutOfTen = [
  "Angel Creo-Corp Mining",
  "Blood Raider Human Farm",
  "Pith Merchant Depot",
  "Sansha Acclimatization Facility",
  "Serpentis Live Cargo Distribution Facilities",
  "Rogue Drone Infestation Sprout",
];
const threeOutOfTen = [
  "Angel Repurposed Outpost",
  "Blood Raider Intelligence Collection Point",
  "Guristas Guerilla Grounds",
  "Sansha's Command Relay Outpost",
  "Serpentis Narcotic Warehouses",
  "Rogue Drone Asteroid Infestation",
];
const fourOutOfTen = [
  "Angel Cartel Occupied Mining Colony",
  "Mul-Zatah Monastery",
  "Guristas Scout Outpost",
  "Sansha's Nation Occupied Mining Colony",
  "Serpentis Phi-Outpost",
  "Drone Infested Mine",
];
const fiveOutOfTen = [
  "Angel's Red Light District",
  "Blood Raider Psychotropics Depot",
  "Guristas Hallucinogen Supply Waypoint",
  "Sansha's Nation Neural Paralytic Facility",
  "Serpentis Corporation Hydroponics Site",
  "Outgrowth Rogue Drone Hive",
];
function isFourOutOfTen(signature: string): boolean {
  return fourOutOfTen.some((s) => signature.includes(s));
}
function isFiveOutOfTen(signature: string): boolean {
  return fiveOutOfTen.some((s) => signature.includes(s));
}

export const parseSignature = (text: string): Omit<Signature, 'system' | 'id' | 'sysid'> | null => {
  const parts = text.split('\t');
  if (parts.length < 4) return null;

  // Validate signature identifier format (3 letters, dash, 3 numbers)
  const signatureIdentifierRegex = /^\w{3}-\d{3}$/;
  if (!signatureIdentifierRegex.test(parts[0])) {
    return null;
  }

  let note = "";
  const isFour = isFourOutOfTen(parts[3]);
  if (isFour) {
    note = "4/10";
  }
  const isFive = isFiveOutOfTen(parts[3]);
  if (isFive) {
    note = "5/10";
  }

  return {
    identifier: parts[0],
    type: parts[2],
    signame: parts[3],
    scanned: parts.length > 4 ? parts[4] : undefined,
    dangerous: false, // TODO: Implement dangerous signature detection
    note: note,
  };
};

export const parseScannedPercentage = (scannedString?: string): number => {
  if (!scannedString) return 0;
  const match = scannedString.match(/(\d+(?:\.\d+)?)%/);
  return match ? parseFloat(match[1]) : 0;
};
