import {
  siArtstation,
  siBilibili,
  siBluesky,
  siBuymeacoffee,
  siCarrd,
  siDiscord,
  siDeviantart,
  siFacebook,
  siFandom,
  siFiverr,
  siGithub,
  siGuilded,
  siInstagram,
  siKick,
  siKofi,
  siLinktree,
  siMastodon,
  siMatrix,
  siMedium,
  siNiconico,
  siOnlyfans,
  siPatreon,
  siPicartodottv,
  siPinterest,
  siPixiv,
  siReddit,
  siSnapchat,
  siSoundcloud,
  siSpotify,
  siSteam,
  siSubstack,
  siTelegram,
  siThreads,
  siTiktok,
  siTumblr,
  siTwitch,
  siVimeo,
  siWhatsapp,
  siX,
  siYoutube,
  type SimpleIcon,
} from 'simple-icons'

export type SocialIconOption = {
  key: string
  label: string
}

export type SocialIconOptionGroup = {
  label: string
  options: SocialIconOption[]
}

const SOCIAL_ICONS: Record<string, SimpleIcon> = {
  artstation: siArtstation,
  bilibili: siBilibili,
  bluesky: siBluesky,
  buymeacoffee: siBuymeacoffee,
  carrd: siCarrd,
  discord: siDiscord,
  deviantart: siDeviantart,
  facebook: siFacebook,
  fandom: siFandom,
  fiverr: siFiverr,
  github: siGithub,
  guilded: siGuilded,
  instagram: siInstagram,
  kick: siKick,
  kofi: siKofi,
  linktree: siLinktree,
  mastodon: siMastodon,
  matrix: siMatrix,
  medium: siMedium,
  niconico: siNiconico,
  onlyfans: siOnlyfans,
  patreon: siPatreon,
  picarto: siPicartodottv,
  picartotv: siPicartodottv,
  pinterest: siPinterest,
  pixiv: siPixiv,
  reddit: siReddit,
  snapchat: siSnapchat,
  soundcloud: siSoundcloud,
  spotify: siSpotify,
  steam: siSteam,
  substack: siSubstack,
  telegram: siTelegram,
  threads: siThreads,
  tiktok: siTiktok,
  tumblr: siTumblr,
  twitch: siTwitch,
  twitter: siX,
  vimeo: siVimeo,
  whatsapp: siWhatsapp,
  x: siX,
  youtube: siYoutube,
}

export const SOCIAL_ICON_OPTION_GROUPS: SocialIconOptionGroup[] = [
  {
    label: 'Streaming & Video',
    options: [
      { key: 'twitch', label: 'Twitch' },
      { key: 'youtube', label: 'YouTube' },
      { key: 'kick', label: 'Kick' },
      { key: 'tiktok', label: 'TikTok' },
      { key: 'bilibili', label: 'Bilibili' },
      { key: 'niconico', label: 'Niconico' },
      { key: 'picarto', label: 'Picarto.TV' },
      { key: 'vimeo', label: 'Vimeo' },
    ],
  },
  {
    label: 'Social & Community',
    options: [
      { key: 'x', label: 'X / Twitter' },
      { key: 'instagram', label: 'Instagram' },
      { key: 'bluesky', label: 'Bluesky' },
      { key: 'threads', label: 'Threads' },
      { key: 'mastodon', label: 'Mastodon' },
      { key: 'discord', label: 'Discord' },
      { key: 'guilded', label: 'Guilded' },
      { key: 'matrix', label: 'Matrix' },
      { key: 'telegram', label: 'Telegram' },
      { key: 'whatsapp', label: 'WhatsApp' },
      { key: 'reddit', label: 'Reddit' },
      { key: 'facebook', label: 'Facebook' },
      { key: 'snapchat', label: 'Snapchat' },
    ],
  },
  {
    label: 'Creator & Support',
    options: [
      { key: 'patreon', label: 'Patreon' },
      { key: 'kofi', label: 'Ko-fi' },
      { key: 'buymeacoffee', label: 'Buy Me a Coffee' },
      { key: 'linktree', label: 'Linktree' },
      { key: 'carrd', label: 'Carrd' },
      { key: 'substack', label: 'Substack' },
      { key: 'medium', label: 'Medium' },
      { key: 'github', label: 'GitHub' },
      { key: 'fiverr', label: 'Fiverr' },
      { key: 'onlyfans', label: 'OnlyFans' },
    ],
  },
  {
    label: 'Art, Musik & Portfolio',
    options: [
      { key: 'pixiv', label: 'Pixiv' },
      { key: 'deviantart', label: 'DeviantArt' },
      { key: 'artstation', label: 'ArtStation' },
      { key: 'pinterest', label: 'Pinterest' },
      { key: 'tumblr', label: 'Tumblr' },
      { key: 'fandom', label: 'Fandom' },
      { key: 'spotify', label: 'Spotify' },
      { key: 'soundcloud', label: 'SoundCloud' },
      { key: 'steam', label: 'Steam' },
    ],
  },
  {
    label: 'Fallback',
    options: [
      { key: 'website', label: 'Website / Fallback' },
    ],
  },
]

export const SOCIAL_ICON_OPTIONS: SocialIconOption[] = SOCIAL_ICON_OPTION_GROUPS.flatMap((group) => group.options)

export function normalizeSocialIconKey(value: string | null | undefined) {
  return (value ?? 'website').trim().toLowerCase()
}

export function socialIconOptionForKey(value: string | null | undefined) {
  const normalized = normalizeSocialIconKey(value)
  return SOCIAL_ICON_OPTIONS.find((option) => option.key === normalized) ?? null
}

export function socialIconOptionForValue(value: string | null | undefined) {
  const normalized = normalizeSocialIconKey(value)
  return SOCIAL_ICON_OPTIONS.find((option) =>
    option.key === normalized ||
    option.label.trim().toLowerCase() === normalized,
  ) ?? null
}

export function simpleIconForKey(value: string | null | undefined) {
  return SOCIAL_ICONS[normalizeSocialIconKey(value)] ?? null
}
