# typed: false
require "rails_helper"

RSpec.describe Domain::Inkbunny::Job::UpdatePoolJob do
  let(:http_client_mock) { instance_double("::Scraper::HttpClient") }
  before { Scraper::ClientFactory.http_client_mock = http_client_mock }
  let(:log_entries) { [] }

  describe "#perform" do
    it "processes all posts in a pool and enqueues update jobs for posts needing updates" do
      pool = create(:domain_post_group_inkbunny_pool, ib_id: 83_746)
      first_page_json =
        JSON.parse(
          SpecUtil.read_fixture_file(
            "domain/inkbunny/job/api_search_pool_83746_page_1.json",
          ),
        )
      log_entries =
        HttpClientMockHelpers.init_http_client_mock(
          http_client_mock,
          [
            {
              method: :post,
              uri:
                "https://inkbunny.net/api_search.php?submissions_per_page=100&page=1&get_rid=yes&orderby=create_datetime&pool_id=83746&keywords=no",
              content_type: "application/json",
              contents:
                SpecUtil.read_fixture_file(
                  "domain/inkbunny/job/api_search_pool_83746_page_1.json",
                ),
              options: {
                caused_by_entry: nil,
                use_http_cache: nil,
              },
            },
            {
              method: :post,
              uri:
                "https://inkbunny.net/api_search.php?submissions_per_page=100&page=2&rid=#{first_page_json["rid"]}",
              content_type: "application/json",
              contents:
                SpecUtil.read_fixture_file(
                  "domain/inkbunny/job/api_search_pool_83746_page_2.json",
                ),
              caused_by_entry_idx: 0,
            },
          ],
        )

      expect { perform_now({ pool: pool }) }.to change {
        Domain::Post::InkbunnyPost.count
      }.by(4)

      # Verify posts were created correctly
      post_3334290 = Domain::Post::InkbunnyPost.find_by!(ib_id: 3_334_290)
      expect(post_3334290.title).to eq("Phantom Touch: Follow-Up")
      expect(post_3334290.posted_at).to be_within(3.seconds).of(
        Time.parse("2024-05-28 00:57:36.597545+00"),
      )
      expect(post_3334290.last_file_updated_at).to be_within(3.seconds).of(
        Time.parse("2024-05-28 00:55:24.811748+00"),
      )
      expect(post_3334290.num_files).to eq(1)
      expect(post_3334290.rating).to eq("general")
      expect(post_3334290.submission_type).to eq("picture_pinup")
      expect(post_3334290.shallow_updated_at).to be_within(3.seconds).of(
        Time.now,
      )
      expect(post_3334290.shallow_update_log_entry).to eq(log_entries[0])
      expect(post_3334290.deep_updated_at).to be_nil

      # Verify user was created
      user_thendyart = Domain::User::InkbunnyUser.find_by!(ib_id: 941_565)
      expect(user_thendyart.name).to eq("ThendyArt")

      # Verify UpdatePostsJob was enqueued for posts without deep_updated_at
      expect(SpecUtil.enqueued_job_args).to match(
        [
          hash_including(
            ib_post_ids:
              array_including(3_334_290, 3_333_345, 3_333_338, 3_327_400),
            caused_by_entry: log_entries[0],
          ),
        ],
      )

      expect(pool.deep_update_log_entry).to eq(log_entries[0])
    end

    it "skips if already deep updated" do
      pool = create(:domain_post_group_inkbunny_pool, ib_id: 83_746)
      log_entry = create(:http_log_entry)
      pool.deep_update_log_entry = log_entry
      pool.save!

      expect { perform_now({ pool: pool }) }.not_to change {
        Domain::Post::InkbunnyPost.count
      }

      expect(pool.deep_update_log_entry).to eq(log_entry)
    end
  end
end
