defmodule Berrypod.Analytics.ReferrerTest do
  use ExUnit.Case, async: true

  alias Berrypod.Analytics.Referrer

  describe "parse/1" do
    test "extracts Google search" do
      assert Referrer.parse("https://www.google.com/search?q=test") == {"google.com", "Google"}
    end

    test "extracts Google with country TLD" do
      assert Referrer.parse("https://www.google.co.uk/") == {"google.co.uk", "Google"}
    end

    test "extracts Facebook" do
      assert Referrer.parse("https://www.facebook.com/some-page") == {"facebook.com", "Facebook"}
    end

    test "extracts Twitter / X" do
      assert Referrer.parse("https://t.co/abc123") == {"t.co", "Telegram"}
      assert Referrer.parse("https://twitter.com/user") == {"twitter.com", "Twitter"}
      assert Referrer.parse("https://x.com/user") == {"x.com", "Twitter"}
    end

    test "extracts Reddit" do
      assert Referrer.parse("https://www.reddit.com/r/elixir") == {"reddit.com", "Reddit"}
    end

    test "returns nil source for unknown domain" do
      assert Referrer.parse("https://myblog.example.com/post/1") ==
               {"myblog.example.com", nil}
    end

    test "strips www prefix" do
      assert Referrer.parse("https://www.example.com/page") == {"example.com", nil}
    end

    test "returns nil for nil input" do
      assert Referrer.parse(nil) == {nil, nil}
    end

    test "returns nil for empty string" do
      assert Referrer.parse("") == {nil, nil}
    end

    test "returns nil for invalid URL" do
      assert Referrer.parse("not a url") == {nil, nil}
    end

    test "extracts DuckDuckGo" do
      assert Referrer.parse("https://duckduckgo.com/?q=test") == {"duckduckgo.com", "DuckDuckGo"}
    end

    test "extracts YouTube" do
      assert Referrer.parse("https://www.youtube.com/watch?v=abc") == {"youtube.com", "YouTube"}
    end

    test "extracts Instagram" do
      assert Referrer.parse("https://www.instagram.com/user") == {"instagram.com", "Instagram"}
    end

    test "extracts LinkedIn" do
      assert Referrer.parse("https://www.linkedin.com/feed") == {"linkedin.com", "LinkedIn"}
    end

    test "extracts Pinterest" do
      assert Referrer.parse("https://www.pinterest.com/pin/123") == {"pinterest.com", "Pinterest"}
    end

    test "extracts Bing" do
      assert Referrer.parse("https://www.bing.com/search?q=test") == {"bing.com", "Bing"}
    end
  end
end
