import {
  Archive,
  Binary,
  Blocks,
  BookOpen,
  Box,
  Braces,
  Code2,
  Cog,
  Coffee,
  Cpu,
  Database,
  File,
  FileCheck,
  FileCode,
  FileCode2,
  FileSpreadsheet,
  FileText,
  FileType,
  Flame,
  FlaskConical,
  Gem,
  Globe,
  Hash,
  Hexagon,
  Image,
  Lock,
  Music2,
  NotebookPen,
  Palette,
  Scroll,
  Settings,
  Shield,
  SquareFunction,
  Terminal,
  Video,
  Workflow,
} from 'lucide-react';
import type { FileIconData, FileIconMap } from '../types/types';

export const ICON_SIZE_CLASS = 'w-4 h-4 flex-shrink-0';

const FILE_ICON_MAP: FileIconMap = {
  js: { icon: FileCode, color: 'text-yellow-500' },
  jsx: { icon: FileCode, color: 'text-yellow-500' },
  mjs: { icon: FileCode, color: 'text-yellow-500' },
  cjs: { icon: FileCode, color: 'text-yellow-500' },
  ts: { icon: FileCode2, color: 'text-blue-500' },
  tsx: { icon: FileCode2, color: 'text-blue-500' },
  mts: { icon: FileCode2, color: 'text-blue-500' },
  py: { icon: Code2, color: 'text-emerald-500' },
  pyw: { icon: Code2, color: 'text-emerald-500' },
  pyi: { icon: Code2, color: 'text-emerald-400' },
  ipynb: { icon: NotebookPen, color: 'text-orange-500' },
  rs: { icon: Cog, color: 'text-orange-600' },
  toml: { icon: Settings, color: 'text-gray-500' },
  go: { icon: Hexagon, color: 'text-cyan-500' },
  rb: { icon: Gem, color: 'text-red-500' },
  erb: { icon: Gem, color: 'text-red-400' },
  php: { icon: Blocks, color: 'text-violet-500' },
  java: { icon: Coffee, color: 'text-red-600' },
  jar: { icon: Coffee, color: 'text-red-500' },
  kt: { icon: Hexagon, color: 'text-violet-500' },
  kts: { icon: Hexagon, color: 'text-violet-400' },
  c: { icon: Cpu, color: 'text-blue-600' },
  h: { icon: Cpu, color: 'text-blue-400' },
  cpp: { icon: Cpu, color: 'text-blue-700' },
  hpp: { icon: Cpu, color: 'text-blue-500' },
  cc: { icon: Cpu, color: 'text-blue-700' },
  cs: { icon: Hexagon, color: 'text-purple-600' },
  swift: { icon: Flame, color: 'text-orange-500' },
  lua: { icon: SquareFunction, color: 'text-blue-500' },
  r: { icon: FlaskConical, color: 'text-blue-600' },
  html: { icon: Globe, color: 'text-orange-600' },
  htm: { icon: Globe, color: 'text-orange-600' },
  css: { icon: Hash, color: 'text-blue-500' },
  scss: { icon: Hash, color: 'text-pink-500' },
  sass: { icon: Hash, color: 'text-pink-400' },
  less: { icon: Hash, color: 'text-indigo-500' },
  vue: { icon: FileCode2, color: 'text-emerald-500' },
  svelte: { icon: FileCode2, color: 'text-orange-500' },
  json: { icon: Braces, color: 'text-yellow-600' },
  jsonc: { icon: Braces, color: 'text-yellow-500' },
  json5: { icon: Braces, color: 'text-yellow-500' },
  yaml: { icon: Settings, color: 'text-purple-400' },
  yml: { icon: Settings, color: 'text-purple-400' },
  xml: { icon: FileCode, color: 'text-orange-500' },
  csv: { icon: FileSpreadsheet, color: 'text-green-600' },
  tsv: { icon: FileSpreadsheet, color: 'text-green-500' },
  sql: { icon: Database, color: 'text-blue-500' },
  graphql: { icon: Workflow, color: 'text-pink-500' },
  gql: { icon: Workflow, color: 'text-pink-500' },
  proto: { icon: Box, color: 'text-green-500' },
  env: { icon: Shield, color: 'text-yellow-600' },
  md: { icon: BookOpen, color: 'text-blue-500' },
  mdx: { icon: BookOpen, color: 'text-blue-400' },
  txt: { icon: FileText, color: 'text-gray-500' },
  doc: { icon: FileText, color: 'text-blue-600' },
  docx: { icon: FileText, color: 'text-blue-600' },
  pdf: { icon: FileCheck, color: 'text-red-600' },
  rtf: { icon: FileText, color: 'text-gray-500' },
  tex: { icon: Scroll, color: 'text-teal-600' },
  rst: { icon: FileText, color: 'text-gray-400' },
  sh: { icon: Terminal, color: 'text-green-500' },
  bash: { icon: Terminal, color: 'text-green-500' },
  zsh: { icon: Terminal, color: 'text-green-400' },
  fish: { icon: Terminal, color: 'text-green-400' },
  ps1: { icon: Terminal, color: 'text-blue-400' },
  bat: { icon: Terminal, color: 'text-gray-500' },
  cmd: { icon: Terminal, color: 'text-gray-500' },
  png: { icon: Image, color: 'text-purple-500' },
  jpg: { icon: Image, color: 'text-purple-500' },
  jpeg: { icon: Image, color: 'text-purple-500' },
  gif: { icon: Image, color: 'text-purple-400' },
  webp: { icon: Image, color: 'text-purple-400' },
  ico: { icon: Image, color: 'text-purple-400' },
  bmp: { icon: Image, color: 'text-purple-400' },
  tiff: { icon: Image, color: 'text-purple-400' },
  svg: { icon: Palette, color: 'text-amber-500' },
  mp3: { icon: Music2, color: 'text-pink-500' },
  wav: { icon: Music2, color: 'text-pink-500' },
  ogg: { icon: Music2, color: 'text-pink-400' },
  flac: { icon: Music2, color: 'text-pink-400' },
  aac: { icon: Music2, color: 'text-pink-400' },
  m4a: { icon: Music2, color: 'text-pink-400' },
  mp4: { icon: Video, color: 'text-rose-500' },
  mov: { icon: Video, color: 'text-rose-500' },
  avi: { icon: Video, color: 'text-rose-500' },
  webm: { icon: Video, color: 'text-rose-400' },
  mkv: { icon: Video, color: 'text-rose-400' },
  ttf: { icon: FileType, color: 'text-red-500' },
  otf: { icon: FileType, color: 'text-red-500' },
  woff: { icon: FileType, color: 'text-red-400' },
  woff2: { icon: FileType, color: 'text-red-400' },
  eot: { icon: FileType, color: 'text-red-400' },
  zip: { icon: Archive, color: 'text-amber-600' },
  tar: { icon: Archive, color: 'text-amber-600' },
  gz: { icon: Archive, color: 'text-amber-600' },
  bz2: { icon: Archive, color: 'text-amber-600' },
  rar: { icon: Archive, color: 'text-amber-500' },
  '7z': { icon: Archive, color: 'text-amber-500' },
  lock: { icon: Lock, color: 'text-gray-500' },
  exe: { icon: Binary, color: 'text-gray-500' },
  bin: { icon: Binary, color: 'text-gray-500' },
  dll: { icon: Binary, color: 'text-gray-400' },
  so: { icon: Binary, color: 'text-gray-400' },
  dylib: { icon: Binary, color: 'text-gray-400' },
  wasm: { icon: Binary, color: 'text-purple-500' },
  ini: { icon: Settings, color: 'text-gray-500' },
  cfg: { icon: Settings, color: 'text-gray-500' },
  conf: { icon: Settings, color: 'text-gray-500' },
  log: { icon: Scroll, color: 'text-gray-400' },
  map: { icon: File, color: 'text-gray-400' },
};

const FILENAME_ICON_MAP: FileIconMap = {
  Dockerfile: { icon: Box, color: 'text-blue-500' },
  'docker-compose.yml': { icon: Box, color: 'text-blue-500' },
  'docker-compose.yaml': { icon: Box, color: 'text-blue-500' },
  '.dockerignore': { icon: Box, color: 'text-gray-500' },
  '.gitignore': { icon: Settings, color: 'text-gray-500' },
  '.gitmodules': { icon: Settings, color: 'text-gray-500' },
  '.gitattributes': { icon: Settings, color: 'text-gray-500' },
  '.editorconfig': { icon: Settings, color: 'text-gray-500' },
  '.prettierrc': { icon: Settings, color: 'text-pink-400' },
  '.prettierignore': { icon: Settings, color: 'text-gray-500' },
  '.eslintrc': { icon: Settings, color: 'text-violet-500' },
  '.eslintrc.js': { icon: Settings, color: 'text-violet-500' },
  '.eslintrc.json': { icon: Settings, color: 'text-violet-500' },
  '.eslintrc.cjs': { icon: Settings, color: 'text-violet-500' },
  'eslint.config.js': { icon: Settings, color: 'text-violet-500' },
  'eslint.config.mjs': { icon: Settings, color: 'text-violet-500' },
  '.env': { icon: Shield, color: 'text-yellow-600' },
  '.env.local': { icon: Shield, color: 'text-yellow-600' },
  '.env.development': { icon: Shield, color: 'text-yellow-500' },
  '.env.production': { icon: Shield, color: 'text-yellow-600' },
  '.env.example': { icon: Shield, color: 'text-yellow-400' },
  'package.json': { icon: Braces, color: 'text-green-500' },
  'package-lock.json': { icon: Lock, color: 'text-gray-500' },
  'yarn.lock': { icon: Lock, color: 'text-blue-400' },
  'pnpm-lock.yaml': { icon: Lock, color: 'text-orange-400' },
  'bun.lockb': { icon: Lock, color: 'text-gray-400' },
  'Cargo.toml': { icon: Cog, color: 'text-orange-600' },
  'Cargo.lock': { icon: Lock, color: 'text-orange-400' },
  Gemfile: { icon: Gem, color: 'text-red-500' },
  'Gemfile.lock': { icon: Lock, color: 'text-red-400' },
  Makefile: { icon: Terminal, color: 'text-gray-500' },
  'CMakeLists.txt': { icon: Cog, color: 'text-blue-500' },
  'tsconfig.json': { icon: Braces, color: 'text-blue-500' },
  'jsconfig.json': { icon: Braces, color: 'text-yellow-500' },
  'vite.config.ts': { icon: Flame, color: 'text-purple-500' },
  'vite.config.js': { icon: Flame, color: 'text-purple-500' },
  'webpack.config.js': { icon: Cog, color: 'text-blue-500' },
  'tailwind.config.js': { icon: Hash, color: 'text-cyan-500' },
  'tailwind.config.ts': { icon: Hash, color: 'text-cyan-500' },
  'postcss.config.js': { icon: Cog, color: 'text-red-400' },
  'babel.config.js': { icon: Settings, color: 'text-yellow-500' },
  '.babelrc': { icon: Settings, color: 'text-yellow-500' },
  'README.md': { icon: BookOpen, color: 'text-blue-500' },
  LICENSE: { icon: FileCheck, color: 'text-gray-500' },
  'LICENSE.md': { icon: FileCheck, color: 'text-gray-500' },
  'CHANGELOG.md': { icon: Scroll, color: 'text-blue-400' },
  'requirements.txt': { icon: FileText, color: 'text-emerald-400' },
  'go.mod': { icon: Hexagon, color: 'text-cyan-500' },
  'go.sum': { icon: Lock, color: 'text-cyan-400' },
};

// Icon resolution is deterministic: exact filename, then .env prefixes, then extension, then fallback.
export function getFileIconData(filename: string): FileIconData {
  if (FILENAME_ICON_MAP[filename]) {
    return FILENAME_ICON_MAP[filename];
  }

  if (filename.startsWith('.env')) {
    return { icon: Shield, color: 'text-yellow-600' };
  }

  const extension = filename.split('.').pop()?.toLowerCase();
  if (extension && FILE_ICON_MAP[extension]) {
    return FILE_ICON_MAP[extension];
  }

  return { icon: File, color: 'text-muted-foreground' };
}
