defmodule BerrypodWeb.ShopComponents.SiteEditor do
  @moduledoc """
  Site editor component for the on-site editor panel.

  Manages site-wide content that appears across all pages:
  - Branding (shop name, logo, favicon)
  - Announcement bar
  - Header navigation
  - Footer content & navigation
  - Social links
  """

  use Phoenix.Component

  import BerrypodWeb.CoreComponents, only: [icon: 1]

  # ── Main Editor Component ────────────────────────────────────────────

  @doc """
  Renders the site editor panel.

  Shows collapsible sections for each category of site-wide content.

  Expects:
  - site_state: SiteEditorState struct with all site tab state
  - site_nav_pages: list of pages for the nav picker
  - Branding-related props passed from theme state
  """
  attr :site_state, :any, required: true
  attr :site_nav_pages, :list, default: []
  attr :event_prefix, :string, default: "site_"
  attr :site_name, :string, default: ""
  attr :theme_settings, :map, default: nil
  attr :logo_image, :map, default: nil
  attr :header_image, :map, default: nil
  attr :icon_image, :map, default: nil
  attr :contrast_warning, :atom, default: :ok
  attr :uploads, :map, default: nil

  def site_editor(%{site_state: nil} = assigns) do
    ~H"""
    <div class="editor-site-content">
      <p class="admin-text-secondary">Loading site settings...</p>
    </div>
    """
  end

  def site_editor(assigns) do
    state = assigns.site_state

    # Extract fields from state for child components
    settings = %{
      announcement_text: state.announcement_text,
      announcement_link: state.announcement_link,
      announcement_style: state.announcement_style,
      footer_about: state.footer_about,
      footer_copyright: state.footer_copyright,
      show_newsletter: state.show_newsletter
    }

    assigns =
      assigns
      |> assign(:settings, settings)
      |> assign(:header_nav, state.header_nav)
      |> assign(:footer_nav, state.footer_nav)
      |> assign(:social_links, state.social_links)

    ~H"""
    <div class="editor-site-content">
      <.site_section title="Branding" icon="hero-sparkles" open={true}>
        <.branding_editor
          site_name={@site_name}
          theme_settings={@theme_settings}
          logo_image={@logo_image}
          header_image={@header_image}
          icon_image={@icon_image}
          contrast_warning={@contrast_warning}
          uploads={@uploads}
          event_prefix={@event_prefix}
        />
      </.site_section>

      <.site_section title="Announcement bar" icon="hero-megaphone">
        <.announcement_editor settings={@settings} event_prefix={@event_prefix} />
      </.site_section>

      <.site_section title="Header navigation" icon="hero-bars-3">
        <.nav_editor
          items={@header_nav}
          location="header"
          pages={@site_nav_pages}
          event_prefix={@event_prefix}
        />
      </.site_section>

      <.site_section title="Footer" icon="hero-document-text">
        <.footer_editor settings={@settings} event_prefix={@event_prefix} />
      </.site_section>

      <.site_section title="Footer navigation" icon="hero-queue-list">
        <.nav_editor
          items={@footer_nav}
          location="footer"
          pages={@site_nav_pages}
          event_prefix={@event_prefix}
        />
      </.site_section>

      <.site_section title="Social links" icon="hero-link">
        <.social_links_editor links={@social_links} event_prefix={@event_prefix} />
      </.site_section>
    </div>
    """
  end

  # ── Collapsible Section ────────────────────────────────────────────

  attr :title, :string, required: true
  attr :icon, :string, default: nil
  attr :open, :boolean, default: false
  slot :inner_block, required: true

  defp site_section(assigns) do
    # Generate a stable ID from the title for the details element
    id = "site-section-" <> (assigns.title |> String.downcase() |> String.replace(~r/\s+/, "-"))
    assigns = assign(assigns, :id, id)

    # Use phx-hook to preserve open state across re-renders
    ~H"""
    <details
      id={@id}
      class="site-editor-section"
      phx-hook="DetailsPreserver"
      {if @open, do: [open: true], else: []}
    >
      <summary class="site-editor-section-header">
        <.icon :if={@icon} name={@icon} class="size-4" />
        <span>{@title}</span>
        <.icon name="hero-chevron-down-mini" class="size-4 site-editor-chevron" />
      </summary>
      <div class="site-editor-section-content">
        {render_slot(@inner_block)}
      </div>
    </details>
    """
  end

  # ── Branding Editor ─────────────────────────────────────────────────

  attr :site_name, :string, default: ""
  attr :theme_settings, :map, default: nil
  attr :logo_image, :map, default: nil
  attr :header_image, :map, default: nil
  attr :icon_image, :map, default: nil
  attr :contrast_warning, :atom, default: :ok
  attr :uploads, :map, default: nil
  attr :event_prefix, :string, default: "site_"

  defp branding_editor(%{theme_settings: nil} = assigns) do
    ~H"""
    <p class="admin-text-secondary">Loading branding settings...</p>
    """
  end

  defp branding_editor(assigns) do
    ~H"""
    <div class="site-editor-form">
      <%!-- Shop name --%>
      <div class="theme-section">
        <label class="theme-section-label">Shop name</label>
        <form phx-change={@event_prefix <> "update_branding"} phx-value-field="site_name">
          <input
            type="text"
            name="site_name"
            value={@site_name}
            placeholder="Your shop name"
            class="admin-input"
          />
        </form>
      </div>

      <%!-- Display toggles --%>
      <div class="admin-stack admin-stack-sm theme-field">
        <label class="admin-check-label">
          <input
            type="checkbox"
            checked={@theme_settings.show_site_name}
            phx-click={@event_prefix <> "toggle_branding"}
            phx-value-field="show_site_name"
            class="admin-checkbox admin-checkbox-sm"
          />
          <span class="theme-check-text">Show shop name</span>
        </label>

        <label class="admin-check-label">
          <input
            type="checkbox"
            checked={@theme_settings.show_logo}
            phx-click={@event_prefix <> "toggle_branding"}
            phx-value-field="show_logo"
            class="admin-checkbox admin-checkbox-sm"
          />
          <span class="theme-check-text">Show logo</span>
        </label>
      </div>

      <%!-- Logo upload section --%>
      <%= if @theme_settings.show_logo && @uploads do %>
        <.logo_upload_section
          uploads={@uploads}
          logo_image={@logo_image}
          theme_settings={@theme_settings}
          site_name={@site_name}
          event_prefix={@event_prefix}
        />
      <% end %>

      <%!-- Header background toggle --%>
      <label class="admin-check-label theme-field">
        <input
          type="checkbox"
          checked={@theme_settings.header_background_enabled}
          phx-click={@event_prefix <> "update_branding"}
          phx-value-field="header_background_enabled"
          phx-value-setting_value={
            if @theme_settings.header_background_enabled, do: "false", else: "true"
          }
          class="admin-checkbox admin-checkbox-sm"
        />
        <span class="theme-check-text">Header background image</span>
      </label>

      <%!-- Header upload section --%>
      <%= if @theme_settings.header_background_enabled && @uploads do %>
        <.header_upload_section
          uploads={@uploads}
          header_image={@header_image}
          theme_settings={@theme_settings}
          contrast_warning={@contrast_warning}
          event_prefix={@event_prefix}
        />
      <% end %>
    </div>
    """
  end

  # Logo upload sub-section
  attr :uploads, :map, required: true
  attr :logo_image, :map, default: nil
  attr :theme_settings, :map, required: true
  attr :site_name, :string, default: ""
  attr :event_prefix, :string, default: "site_"

  defp logo_upload_section(assigns) do
    ~H"""
    <div class="theme-subsection">
      <span class="theme-slider-label theme-block-label">Upload logo (SVG or PNG)</span>
      <div class="admin-row admin-row-lg">
        <form phx-change="noop" phx-submit="noop" class="admin-fill">
          <label class="theme-upload-label theme-upload-label-compact">
            <span>Choose file...</span>
            <.live_file_input upload={@uploads.theme_logo_upload} class="hidden" />
          </label>
        </form>
        <%= if @logo_image do %>
          <div class="theme-thumb theme-thumb-logo theme-thumb-compact">
            <img src={"/image_cache/#{@logo_image.id}.webp"} alt={@site_name} />
            <button
              type="button"
              phx-click={@event_prefix <> "remove_logo"}
              class="theme-remove-btn"
              title="Remove logo"
            >
              ×
            </button>
          </div>
        <% end %>
      </div>

      <%= for entry <- @uploads.theme_logo_upload.entries do %>
        <.upload_progress entry={entry} upload_name="theme_logo_upload" />
        <%= for err <- upload_errors(@uploads.theme_logo_upload, entry) do %>
          <p class="theme-error-text">{error_to_string(err)}</p>
        <% end %>
      <% end %>

      <%= for err <- upload_errors(@uploads.theme_logo_upload) do %>
        <p class="theme-error-text">{error_to_string(err)}</p>
      <% end %>

      <%= if @logo_image do %>
        <form
          phx-change={@event_prefix <> "update_branding"}
          phx-value-field="logo_size"
          class="theme-subfield"
        >
          <div class="theme-slider-header">
            <span class="theme-slider-label">Logo size</span>
            <span class="theme-slider-value">{@theme_settings.logo_size}px</span>
          </div>
          <input
            type="range"
            min="24"
            max="120"
            value={@theme_settings.logo_size}
            name="logo_size"
            class="admin-range"
          />
        </form>

        <%= if @logo_image.is_svg do %>
          <div class="theme-subfield">
            <label class="admin-check-label">
              <input
                type="checkbox"
                checked={@theme_settings.logo_recolor}
                phx-click={@event_prefix <> "update_branding"}
                phx-value-field="logo_recolor"
                phx-value-setting_value={if @theme_settings.logo_recolor, do: "false", else: "true"}
                class="admin-checkbox admin-checkbox-sm"
              />
              <span class="theme-check-text">Recolour logo</span>
            </label>

            <%= if @theme_settings.logo_recolor do %>
              <form
                id="logo-color-form-site"
                phx-change={@event_prefix <> "update_color"}
                phx-value-field="logo_color"
                phx-hook="ColorSync"
                class="theme-color-row theme-subfield-sm"
              >
                <input
                  type="color"
                  name="value"
                  value={@theme_settings.logo_color}
                  class="theme-color-swatch theme-color-swatch-sm"
                />
                <span class="theme-color-value">{@theme_settings.logo_color}</span>
              </form>
            <% end %>
          </div>
        <% end %>
      <% end %>
    </div>
    """
  end

  # Header upload sub-section
  attr :uploads, :map, required: true
  attr :header_image, :map, default: nil
  attr :theme_settings, :map, required: true
  attr :contrast_warning, :atom, default: :ok
  attr :event_prefix, :string, default: "site_"

  defp header_upload_section(assigns) do
    ~H"""
    <div class="theme-subsection">
      <span class="theme-slider-label theme-block-label">Upload header image</span>
      <form phx-change="noop" phx-submit="noop">
        <label class="theme-upload-label theme-upload-label-compact">
          <span>Choose file...</span>
          <.live_file_input upload={@uploads.theme_header_upload} class="hidden" />
        </label>
      </form>

      <%= if @header_image do %>
        <div class="theme-thumb theme-thumb-cover theme-thumb-header theme-thumb-compact">
          <img src={"/image_cache/#{@header_image.id}.webp"} alt="" />
          <button
            type="button"
            phx-click={@event_prefix <> "remove_header"}
            class="theme-remove-btn"
            title="Remove header background"
          >
            ×
          </button>
        </div>

        <%= if @contrast_warning != :ok do %>
          <div class="theme-contrast-warning theme-contrast-warning-compact">
            <strong>
              <%= if @contrast_warning == :poor do %>
                Text may be hard to read
              <% else %>
                Text contrast could be better
              <% end %>
            </strong>
            <p>
              Try switching to a
              <%= if @theme_settings.mood == "dark" do %>
                lighter mood
              <% else %>
                dark mood
              <% end %>
              or choosing a different image.
            </p>
          </div>
        <% end %>

        <div class="admin-stack admin-stack-md theme-subfield">
          <form phx-change={@event_prefix <> "update_branding"} phx-value-field="header_zoom">
            <div class="theme-slider-header">
              <span class="theme-slider-label">Zoom</span>
              <span class="theme-slider-value">{@theme_settings.header_zoom}%</span>
            </div>
            <input
              type="range"
              min="100"
              max="200"
              value={@theme_settings.header_zoom}
              name="header_zoom"
              class="admin-range"
            />
          </form>
          <%= if @theme_settings.header_zoom > 100 do %>
            <form
              phx-change={@event_prefix <> "update_branding"}
              phx-value-field="header_position_x"
            >
              <div class="theme-slider-header">
                <span class="theme-slider-label">Horizontal position</span>
                <span class="theme-slider-value">{@theme_settings.header_position_x}%</span>
              </div>
              <input
                type="range"
                min="0"
                max="100"
                value={@theme_settings.header_position_x}
                name="header_position_x"
                class="admin-range"
              />
            </form>
            <form
              phx-change={@event_prefix <> "update_branding"}
              phx-value-field="header_position_y"
            >
              <div class="theme-slider-header">
                <span class="theme-slider-label">Vertical position</span>
                <span class="theme-slider-value">{@theme_settings.header_position_y}%</span>
              </div>
              <input
                type="range"
                min="0"
                max="100"
                value={@theme_settings.header_position_y}
                name="header_position_y"
                class="admin-range"
              />
            </form>
          <% end %>
        </div>
      <% end %>

      <%= for entry <- @uploads.theme_header_upload.entries do %>
        <.upload_progress entry={entry} upload_name="theme_header_upload" />
        <%= for err <- upload_errors(@uploads.theme_header_upload, entry) do %>
          <p class="theme-error-text">{error_to_string(err)}</p>
        <% end %>
      <% end %>

      <%= for err <- upload_errors(@uploads.theme_header_upload) do %>
        <p class="theme-error-text">{error_to_string(err)}</p>
      <% end %>
    </div>
    """
  end

  # Shared upload progress component
  attr :entry, :map, required: true
  attr :upload_name, :string, required: true

  defp upload_progress(assigns) do
    ~H"""
    <div class="theme-progress">
      <div class="theme-progress-bar">
        <div class="theme-progress-fill" style={"width: #{@entry.progress}%"}></div>
      </div>
      <span class="admin-text-secondary">{@entry.progress}%</span>
      <button
        type="button"
        phx-click="theme_cancel_upload"
        phx-value-ref={@entry.ref}
        phx-value-upload={@upload_name}
        class="theme-upload-cancel"
      >
        ×
      </button>
    </div>
    """
  end

  defp error_to_string(:too_large), do: "File is too large"
  defp error_to_string(:too_many_files), do: "Too many files"
  defp error_to_string(:not_accepted), do: "File type not accepted"
  defp error_to_string(err), do: inspect(err)

  # ── Announcement Bar Editor ─────────────────────────────────────────

  attr :settings, :map, required: true
  attr :event_prefix, :string, default: "site_"

  defp announcement_editor(assigns) do
    ~H"""
    <form class="site-editor-form" phx-change={@event_prefix <> "update"}>
      <div class="theme-section">
        <label class="theme-section-label" for="announcement-text">Announcement text</label>
        <input
          type="text"
          id="announcement-text"
          name="site[announcement_text]"
          value={@settings.announcement_text}
          class="admin-input"
          placeholder="Free shipping on orders over £40"
          phx-debounce="500"
        />
      </div>

      <div class="theme-section">
        <label class="theme-section-label" for="announcement-link">Link URL (optional)</label>
        <input
          type="text"
          id="announcement-link"
          name="site[announcement_link]"
          value={@settings.announcement_link}
          class="admin-input"
          placeholder="/delivery"
          phx-debounce="500"
        />
      </div>

      <div class="theme-section">
        <label class="theme-section-label">Style</label>
        <div class="site-editor-radio-group">
          <label class="admin-radio-label">
            <input
              type="radio"
              name="site[announcement_style]"
              value="info"
              checked={@settings.announcement_style == "info"}
            />
            <span>Info</span>
          </label>
          <label class="admin-radio-label">
            <input
              type="radio"
              name="site[announcement_style]"
              value="sale"
              checked={@settings.announcement_style == "sale"}
            />
            <span>Sale</span>
          </label>
          <label class="admin-radio-label">
            <input
              type="radio"
              name="site[announcement_style]"
              value="warning"
              checked={@settings.announcement_style == "warning"}
            />
            <span>Warning</span>
          </label>
        </div>
      </div>
    </form>
    """
  end

  # ── Footer Content Editor ───────────────────────────────────────────

  attr :settings, :map, required: true
  attr :event_prefix, :string, default: "site_"

  defp footer_editor(assigns) do
    ~H"""
    <form class="site-editor-form" phx-change={@event_prefix <> "update"}>
      <div class="theme-section">
        <label class="theme-section-label" for="footer-about">About text</label>
        <textarea
          id="footer-about"
          name="site[footer_about]"
          rows="3"
          class="admin-input admin-textarea"
          placeholder="A short blurb about your shop..."
          phx-debounce="500"
        >{@settings.footer_about}</textarea>
      </div>

      <div class="theme-section">
        <label class="theme-section-label" for="footer-copyright">Copyright text</label>
        <input
          type="text"
          id="footer-copyright"
          name="site[footer_copyright]"
          value={@settings.footer_copyright}
          class="admin-input"
          placeholder={"Leave blank for \"© #{Date.utc_today().year} Shop Name\""}
          phx-debounce="500"
        />
      </div>

      <div class="theme-section">
        <label class="admin-check-label">
          <input
            type="checkbox"
            name="site[show_newsletter]"
            value="true"
            checked={@settings.show_newsletter}
            class="admin-checkbox admin-checkbox-sm"
          />
          <span class="theme-check-text">Show newsletter signup</span>
        </label>
      </div>
    </form>
    """
  end

  # ── Navigation Editor ────────────────────────────────────────────────

  attr :items, :list, required: true
  attr :location, :string, required: true
  attr :pages, :list, default: []
  attr :event_prefix, :string, default: "site_"

  defp nav_editor(assigns) do
    # Build a set of known page URLs for quick lookup
    page_urls =
      Enum.flat_map(assigns.pages, fn page ->
        [page.slug, "/#{page.slug}"]
      end)
      |> MapSet.new()
      |> MapSet.put("/collections/all")
      |> MapSet.put("/")

    assigns = assign(assigns, :page_urls, page_urls)

    ~H"""
    <div class="site-editor-nav-list">
      <ul :if={@items != []} class="site-editor-nav-items">
        <li
          :for={{item, index} <- Enum.with_index(@items)}
          class="site-editor-nav-item"
          data-item-id={item.id}
        >
          <% # Determine if this item links to a known page (by URL match or page_id)
          is_page_link = item.page_id != nil or MapSet.member?(@page_urls, item.url) %>
          <form
            class="site-editor-nav-item-form"
            phx-change={@event_prefix <> "update_nav_item"}
            phx-value-id={item.id}
            phx-value-location={@location}
          >
            <input
              type="text"
              name={"nav_item[#{item.id}][label]"}
              value={item.label}
              class="admin-input admin-input-sm site-editor-nav-label-input"
              placeholder="Label"
              phx-debounce="300"
              aria-label="Link label"
            />
            <div class="site-editor-nav-url-row">
              <select
                name={"nav_item[#{item.id}][link_type]"}
                class="admin-select admin-select-sm site-editor-nav-type"
                aria-label="Link type"
              >
                <option value="page" selected={is_page_link}>Page</option>
                <option value="url" selected={not is_page_link}>Custom URL</option>
              </select>
              <%= if is_page_link or (item.url == "" and @pages != []) do %>
                <select
                  name={"nav_item[#{item.id}][page_id]"}
                  class="admin-select admin-select-sm site-editor-nav-page"
                  aria-label="Page"
                >
                  <option value="">Select a page</option>
                  <optgroup label="Pages">
                    <option value="/" selected={item.url == "/" or item.url == "home"}>
                      Home
                    </option>
                    <option
                      :for={page <- @pages}
                      value={page.slug}
                      selected={item.url == "/#{page.slug}" or item.url == page.slug}
                    >
                      {page.title}
                    </option>
                  </optgroup>
                  <optgroup label="Collections">
                    <option value="/collections/all" selected={item.url == "/collections/all"}>
                      All products
                    </option>
                  </optgroup>
                </select>
              <% else %>
                <input
                  type="text"
                  name={"nav_item[#{item.id}][url]"}
                  value={item.url}
                  class="admin-input admin-input-sm site-editor-nav-url-input"
                  placeholder="/about or https://..."
                  phx-debounce="300"
                  aria-label="URL"
                />
              <% end %>
            </div>
          </form>
          <div class="site-editor-nav-item-actions">
            <button
              type="button"
              class="admin-icon-button"
              phx-click={@event_prefix <> "move_nav_item"}
              phx-value-id={item.id}
              phx-value-location={@location}
              phx-value-dir="up"
              disabled={index == 0}
              aria-label="Move up"
            >
              <.icon name="hero-chevron-up-mini" class="size-4" />
            </button>
            <button
              type="button"
              class="admin-icon-button"
              phx-click={@event_prefix <> "move_nav_item"}
              phx-value-id={item.id}
              phx-value-location={@location}
              phx-value-dir="down"
              disabled={index == length(@items) - 1}
              aria-label="Move down"
            >
              <.icon name="hero-chevron-down-mini" class="size-4" />
            </button>
            <button
              type="button"
              class="admin-icon-button admin-icon-button-danger"
              phx-click={@event_prefix <> "remove_nav_item"}
              phx-value-id={item.id}
              phx-value-location={@location}
              aria-label="Remove"
            >
              <.icon name="hero-x-mark-mini" class="size-4" />
            </button>
          </div>
        </li>
      </ul>
      <p :if={@items == []} class="admin-text-tertiary admin-empty-message">
        No navigation items yet
      </p>
      <button
        type="button"
        class="admin-button admin-button-sm admin-button-outline site-editor-add-button"
        phx-click={@event_prefix <> "add_nav_item"}
        phx-value-location={@location}
      >
        <.icon name="hero-plus-mini" class="size-4" />
        <span>Add link</span>
      </button>
    </div>
    """
  end

  # ── Social Links Editor ──────────────────────────────────────────────

  @platform_groups Berrypod.Site.SocialLink.platform_groups()

  attr :links, :list, required: true
  attr :event_prefix, :string, default: "site_"

  defp social_links_editor(assigns) do
    assigns = assign(assigns, :platform_groups, @platform_groups)

    ~H"""
    <div class="site-editor-social-list">
      <ul :if={@links != []} class="site-editor-social-items">
        <li
          :for={{link, index} <- Enum.with_index(@links)}
          class="site-editor-social-item"
          data-link-id={link.id}
        >
          <form
            class="site-editor-social-item-content"
            phx-change={@event_prefix <> "update_social_link"}
            phx-value-id={link.id}
          >
            <input
              type="text"
              name={"social_link[#{link.id}][url]"}
              value={link.url}
              class="admin-input admin-input-sm site-editor-social-url"
              placeholder="Paste your link..."
              phx-debounce="300"
            />
            <select
              name={"social_link[#{link.id}][platform]"}
              class="admin-select admin-select-sm site-editor-social-platform"
              aria-label="Platform"
            >
              <optgroup :for={{group_name, platforms} <- @platform_groups} label={group_name}>
                <option
                  :for={platform <- platforms}
                  value={platform}
                  selected={link.platform == platform}
                >
                  {platform_label(platform)}
                </option>
              </optgroup>
            </select>
          </form>
          <div class="site-editor-social-item-actions">
            <button
              type="button"
              class="admin-icon-button"
              phx-click={@event_prefix <> "move_social_link"}
              phx-value-id={link.id}
              phx-value-dir="up"
              disabled={index == 0}
              aria-label="Move up"
            >
              <.icon name="hero-chevron-up-mini" class="size-4" />
            </button>
            <button
              type="button"
              class="admin-icon-button"
              phx-click={@event_prefix <> "move_social_link"}
              phx-value-id={link.id}
              phx-value-dir="down"
              disabled={index == length(@links) - 1}
              aria-label="Move down"
            >
              <.icon name="hero-chevron-down-mini" class="size-4" />
            </button>
            <button
              type="button"
              class="admin-icon-button admin-icon-button-danger"
              phx-click={@event_prefix <> "remove_social_link"}
              phx-value-id={link.id}
              aria-label="Remove"
            >
              <.icon name="hero-x-mark-mini" class="size-4" />
            </button>
          </div>
        </li>
      </ul>
      <p :if={@links == []} class="admin-text-tertiary admin-empty-message">
        No social links yet
      </p>
      <button
        type="button"
        class="admin-button admin-button-sm admin-button-outline site-editor-add-button"
        phx-click={@event_prefix <> "add_social_link"}
      >
        <.icon name="hero-plus-mini" class="size-4" />
        <span>Add social link</span>
      </button>
    </div>
    """
  end

  # ── Helpers ─────────────────────────────────────────────────────────

  # Social
  defp platform_label("instagram"), do: "Instagram"
  defp platform_label("threads"), do: "Threads"
  defp platform_label("facebook"), do: "Facebook"
  defp platform_label("twitter"), do: "Twitter / X"
  defp platform_label("snapchat"), do: "Snapchat"
  defp platform_label("linkedin"), do: "LinkedIn"

  # Video & streaming
  defp platform_label("youtube"), do: "YouTube"
  defp platform_label("twitch"), do: "Twitch"
  defp platform_label("vimeo"), do: "Vimeo"
  defp platform_label("kick"), do: "Kick"
  defp platform_label("rumble"), do: "Rumble"

  # Music & podcasts
  defp platform_label("spotify"), do: "Spotify"
  defp platform_label("soundcloud"), do: "SoundCloud"
  defp platform_label("bandcamp"), do: "Bandcamp"
  defp platform_label("applepodcasts"), do: "Apple Podcasts"

  # Creative
  defp platform_label("pinterest"), do: "Pinterest"
  defp platform_label("behance"), do: "Behance"
  defp platform_label("dribbble"), do: "Dribbble"
  defp platform_label("tumblr"), do: "Tumblr"
  defp platform_label("medium"), do: "Medium"

  # Support & sales
  defp platform_label("patreon"), do: "Patreon"
  defp platform_label("kofi"), do: "Ko-fi"
  defp platform_label("etsy"), do: "Etsy"
  defp platform_label("gumroad"), do: "Gumroad"
  defp platform_label("substack"), do: "Substack"

  # Federated
  defp platform_label("mastodon"), do: "Mastodon"
  defp platform_label("pixelfed"), do: "Pixelfed"
  defp platform_label("bluesky"), do: "Bluesky"
  defp platform_label("peertube"), do: "PeerTube"
  defp platform_label("lemmy"), do: "Lemmy"
  defp platform_label("matrix"), do: "Matrix"

  # Developer
  defp platform_label("github"), do: "GitHub"
  defp platform_label("gitlab"), do: "GitLab"
  defp platform_label("codeberg"), do: "Codeberg"
  defp platform_label("sourcehut"), do: "SourceHut"
  defp platform_label("reddit"), do: "Reddit"

  # Messaging
  defp platform_label("discord"), do: "Discord"
  defp platform_label("telegram"), do: "Telegram"
  defp platform_label("signal"), do: "Signal"
  defp platform_label("whatsapp"), do: "WhatsApp"

  # Other
  defp platform_label("linktree"), do: "Linktree"
  defp platform_label("rss"), do: "RSS feed"
  defp platform_label("website"), do: "Website"
  defp platform_label("custom"), do: "Custom link"
  defp platform_label(other), do: String.capitalize(other)
end
