# typed: false
require "rails_helper"

describe Domain::Sofurry::Job::ScanGalleryJob do
  let(:http_client_mock) { instance_double("::Scraper::HttpClient") }
  before { Scraper::ClientFactory.http_client_mock = http_client_mock }

  let!(:log_entries) do
    HttpClientMockHelpers.init_from_manifest(
      http_client_mock,
      "test/fixtures/files/domain/sofurry/scan_user_gallery_806079",
    )
  end

  it "scans the gallery" do
    perform_now({ sofurry_id: 806_079 })
    user = Domain::User::SofurryUser.find_by(sofurry_id: 806_079)
    expect(user.username).to eq("CaptainMemes")
    expect(user.useralias).to eq("captainmemes")
    expect(user.scanned_gallery_at).to be_within(10.seconds).of(Time.now)
    expect(user.posts.count).to eq(58)
    expect(user.posts.for_media_type("stories").count).to eq(23)
    expect(user.posts.for_media_type("art").count).to eq(0)
    expect(user.posts.for_media_type("music").count).to eq(0)
    expect(user.posts.for_media_type("journals").count).to eq(35)
    expect(user.posts.for_media_type("photos").count).to eq(0)

    story_post = Domain::Post::SofurryPost.find_by(sofurry_id: 2_219_563)
    expect(story_post.title).to eq("Chapter 1: Beyond Reach")
    expect(story_post.media_type).to eq("stories")
    expect(story_post.posted_at).to be_within(10.seconds).of(
      Time.parse("2025-01-26 04:29:59 -0800"),
    )
    expect(story_post.folders.count).to eq(1)
    expect(story_post.tags_array).to eq(
      [
        "Dragon",
        "F/M",
        "Romance",
        "Slow burn",
        "Adventure",
        "Violence",
        "Gore",
      ],
    )

    folder = story_post.folders.first
    expect(folder.name).to eq("For All of Dragonkind: The Beginning")
    expect(folder.sofurry_id).to eq(96_273)
    expect(folder.owner).to eq(user)

    journal_post = Domain::Post::SofurryPost.find_by(sofurry_id: 2_224_428)
    expect(journal_post.title).to eq("Sick Update")
    expect(journal_post.media_type).to eq("journals")
    expect(journal_post.tags_array).to eq(%w[Update illness life])
    expect(journal_post.folders.count).to eq(1)

    folder = journal_post.folders.first
    expect(folder.name).to eq("The Road We Call Life")
    expect(folder.sofurry_id).to eq(96_425)
    expect(folder.owner).to eq(user)

    folders = user.posts.flat_map(&:folders).compact.uniq
    expect(folders.count).to eq(9)
    folders.each do |folder|
      expect(folder.scanned_folder_at).to(
        be_within(10.seconds).of(Time.now),
        "folder #{folder}",
      )
    end
  end

  it "enqueues job scans" do
    perform_now({ sofurry_id: 806_079 })
    user = Domain::User::SofurryUser.find_by(sofurry_id: 806_079)

    scan_post_jobs =
      SpecUtil.enqueued_job_args(Domain::Sofurry::Job::ScanPostJob)
    expect(scan_post_jobs.count).to eq(58)
    expect(scan_post_jobs.map { |args| args[:post].sofurry_id }).to(
      contain_exactly(*Domain::Post::SofurryPost.pluck(:sofurry_id)),
    )

    scan_user_jobs =
      SpecUtil.enqueued_job_args(Domain::Sofurry::Job::ScanUserJob)
    expect(scan_user_jobs.count).to eq(1)
    expect(scan_user_jobs).to match([{ user: user, caused_by_entry: anything }])
  end
end
