# typed: false
require "rails_helper"

RSpec.describe Domain::PostsHelper, type: :helper do
  LinkForSource = Domain::PostsHelper::LinkForSource

  describe "#page_str" do
    context "when page is greater than 1" do
      it "returns page string" do
        expect(helper.page_str(page: "2")).to eq("(page 2)")
      end
    end

    context "when page is 1 or not specified" do
      it "returns nil" do
        expect(helper.page_str(page: "1")).to be_nil
        expect(helper.page_str({})).to be_nil
      end
    end
  end

  describe "#link_for_source" do
    it "returns nil for invalid URLs" do
      expect(helper.link_for_source("invalid-url")).to be_nil
    end

    it "returns nil for URLs that don't match any source matchers" do
      expect(helper.link_for_source("https://www.google.com/")).to be_nil
    end

    describe "FA link handling" do
      it "returns nil for FA URLs that are not found" do
        expect(
          helper.link_for_source("https://www.furaffinity.net/view/123456/"),
        ).to be_nil
      end

      %w[
        https://www.furaffinity.net/view/123/
        https://www.furaffinity.net/view/123
        furaffinity.net/view/123/
        furaffinity.net/view/123
        www.furaffinity.net/view/123/
        www.furaffinity.net/view/123
        Furaffinity.net/view/123/
        Furaffinity.net/view/123
      ].each do |url|
        it "returns a link to FA post for #{url}" do
          post = create(:domain_post_fa_post, fa_id: "123", title: "Post Title")
          expect(url).to eq_link_for_source(model: post, title: "Post Title")
        end
      end

      %w[
        https://www.furaffinity.net/user/artistone/
        https://www.furaffinity.net/user/artistone
        https://furaffinity.net/user/artistone/
        https://furaffinity.net/user/artistone
        Furaffinity.net/user/artistone
        furaffinity.net/user/artistone
        www.furaffinity.net/user/artistone
      ].each do |url|
        it "returns a link to FA user for #{url}" do
          user =
            create(
              :domain_user_fa_user,
              url_name: "artistone",
              name: "Artist One",
            )
          expect(url).to eq_link_for_source(model: user, title: "Artist One")
        end
      end

      %w[
        inkbunny.net/s/123456-p3-#pictop
        inkbunny.net/s/123456-p3-#pictop/
        www.inkbunny.net/s/123456-p3-#pictop/
        www.inkbunny.net/s/123456-p3-#pictop
        Inkbunny.net/s/123456-p3-#pictop
        inkbunny.net/submissionview.php?id=123456
        http://inkbunny.net/submissionview.php?id=123456
        http://www.inkbunny.net/submissionview.php?id=123456
      ].each do |url|
        it "returns a link to inkbunny post for #{url}" do
          post =
            create(
              :domain_post_inkbunny_post,
              ib_id: "123456",
              title: "Post Title",
            )
          expect(url).to eq_link_for_source(model: post, title: "Post Title")
        end
      end

      %w[
        https://www.inkbunny.net/user/artistone/
        https://www.inkbunny.net/user/artistone
        https://inkbunny.net/user/artistone/
        https://inkbunny.net/user/artistone
        http://www.inkbunny.net/user/ArtistOne
        www.inkbunny.net/user/ArtistOne
        Inkbunny.net/user/artistone
        inkbunny.net/user/artistone
        inkbunny.net/user/artistone/
        www.inkbunny.net/user/artistone
        www.inkbunny.net/user/artistone/
      ].each do |url|
        it "returns a link to inkbunny user for #{url}" do
          user = create(:domain_user_inkbunny_user, name: "artistone")
          # binding.pry
          expect(url).to eq_link_for_source(model: user, title: "artistone")
        end
      end

      it "has the right avatar url" do
        user =
          create(
            :domain_user_fa_user,
            url_name: "artistone",
            name: "Artist One",
          )
        create(:domain_user_avatar, user:)

        link_for_source =
          helper.link_for_source("https://www.furaffinity.net/user/artistone/")
        expect(link_for_source).to be_present
        expect(link_for_source.icon_path).to eq(
          helper.domain_user_avatar_img_src_path(
            user.avatar,
            thumb: "64-avatar",
          ),
        )
      end

      it "has the right model path" do
        create(:domain_post_fa_post, fa_id: "123456", title: "Post Title")
        link_for_source =
          helper.link_for_source("https://www.furaffinity.net/view/123456/")
        expect(link_for_source).to be_present
        expect(link_for_source.model_path).to eq("/posts/fa@123456")
      end
    end
  end
end
