import {
  AudioLines,
  Clapperboard,
  Film,
  ImageIcon,
  Ratio,
  Scissors,
  Sparkles,
  Split,
  Subtitles,
  Waves,
  WandSparkles
} from "lucide-react";

import type { FutureToolDefinition, ToolDefinition, ToolDefinitionInput } from "@/types/tools";

function normalizeToolDefinition(tool: ToolDefinitionInput): ToolDefinition {
  return {
    ...tool,
    route: tool.route || `/tools/${tool.slug}`
  };
}

function createToolRegistry(toolInputs: ToolDefinitionInput[]) {
  const tools = toolInputs.map(normalizeToolDefinition);
  const toolBySlug = new Map<string, ToolDefinition>();
  const duplicateSlugs = new Set<string>();

  for (const tool of tools) {
    if (toolBySlug.has(tool.slug)) {
      duplicateSlugs.add(tool.slug);
      continue;
    }

    toolBySlug.set(tool.slug, tool);
  }

  if (duplicateSlugs.size > 0) {
    throw new Error(`Duplicate tool slugs detected: ${[...duplicateSlugs].join(", ")}`);
  }

  return {
    tools,
    toolBySlug
  };
}

const registry = createToolRegistry([
  {
    slug: "clip-frame-extractor",
    title: "Clip Frame Extractor",
    description: "Capture exact frames at source resolution for stills, decks, and thumbnails.",
    icon: Clapperboard,
    status: "available",
    accent: {
      borderClass: "border-sky-400/45",
      glowClass: "shadow-[0_20px_45px_rgba(38,145,224,0.22)]",
      badgeClass: "bg-sky-500/18 text-sky-200 border-sky-400/40"
    }
  },
  {
    slug: "extract-audio",
    title: "Extract Audio",
    description: "Pull full tracks or selected ranges with precise timecode control.",
    icon: AudioLines,
    status: "available",
    accent: {
      borderClass: "border-emerald-400/45",
      glowClass: "shadow-[0_20px_45px_rgba(31,167,132,0.22)]",
      badgeClass: "bg-emerald-500/18 text-emerald-200 border-emerald-400/40"
    }
  },
  {
    slug: "video-thumbnail-creator",
    title: "Video Thumbnail Creator",
    description: "Analyze footage, rank candidates, and export your strongest hook frame.",
    icon: ImageIcon,
    status: "available",
    accent: {
      borderClass: "border-amber-300/45",
      glowClass: "shadow-[0_20px_45px_rgba(211,141,43,0.2)]",
      badgeClass: "bg-amber-400/20 text-amber-100 border-amber-300/40"
    }
  },
  {
    slug: "video-format-converter",
    title: "Video Format Converter",
    description: "Convert to delivery-ready formats with controlled quality presets.",
    icon: Film,
    status: "available",
    accent: {
      borderClass: "border-violet-300/45",
      glowClass: "shadow-[0_20px_45px_rgba(123,107,255,0.22)]",
      badgeClass: "bg-violet-400/18 text-violet-100 border-violet-300/40"
    }
  }
]);

export const initialTools = registry.tools;
export const registeredToolSlugs = initialTools.map((tool) => tool.slug);
export type RegisteredToolSlug = (typeof initialTools)[number]["slug"];

export const toolRouteMetadata = Object.fromEntries(
  initialTools.map((tool) => [
    tool.slug,
    {
      title: tool.title,
      description: tool.description,
      route: tool.route
    }
  ])
) as Record<
  RegisteredToolSlug,
  {
    title: string;
    description: string;
    route: string;
  }
>;

export const futureTools: FutureToolDefinition[] = [
  {
    slug: "clip-trimmer",
    title: "Clip Trimmer",
    description: "Trim segments with frame-level precision and fast export presets."
  },
  {
    slug: "gif-maker",
    title: "GIF Maker",
    description: "Turn moments into loopable GIFs with quality and framerate controls."
  },
  {
    slug: "subtitle-extractor",
    title: "Subtitle Extractor",
    description: "Extract and convert subtitle tracks to common formats."
  },
  {
    slug: "speech-to-text",
    title: "Speech to Text",
    description: "Generate transcript drafts with speaker-aware timestamps."
  },
  {
    slug: "silence-remover",
    title: "Silence Remover",
    description: "Find dead air and auto-compact timelines for tighter edits."
  },
  {
    slug: "scene-splitter",
    title: "Scene Splitter",
    description: "Detect scene cuts and split media into reusable segments."
  },
  {
    slug: "caption-burn-in",
    title: "Caption Burn-In",
    description: "Stylize and burn captions directly into video exports."
  },
  {
    slug: "social-resize-presets",
    title: "Social Resize Presets",
    description: "Generate platform-native aspect ratio versions in one pass."
  },
  {
    slug: "transcript-clip-finder",
    title: "Transcript-Based Clip Finder",
    description: "Search transcripts and jump to exact spoken moments."
  }
];

export const recentToolsMock = [
  {
    slug: "video-thumbnail-creator",
    title: "Video Thumbnail Creator",
    usedAtLabel: "8 minutes ago"
  },
  {
    slug: "clip-frame-extractor",
    title: "Clip Frame Extractor",
    usedAtLabel: "Yesterday"
  },
  {
    slug: "extract-audio",
    title: "Extract Audio",
    usedAtLabel: "2 days ago"
  }
];

export function isRegisteredToolSlug(value: string): value is RegisteredToolSlug {
  return registry.toolBySlug.has(value);
}

export function getToolBySlug(slug: string): ToolDefinition | undefined {
  return registry.toolBySlug.get(slug);
}

export function getToolIconForFuture(slug: string) {
  const iconBySlug: Record<string, typeof Sparkles> = {
    "clip-trimmer": Scissors,
    "gif-maker": WandSparkles,
    "subtitle-extractor": Subtitles,
    "speech-to-text": Waves,
    "silence-remover": AudioLines,
    "scene-splitter": Split,
    "caption-burn-in": Ratio,
    "social-resize-presets": Sparkles,
    "transcript-clip-finder": Film
  };

  return iconBySlug[slug] ?? Sparkles;
}
