# typed: false
require "rails_helper"

RSpec.describe Domain::PostFile::Thumbnail, type: :model do
  describe "validation" do
    it "validates thumbable content types" do
      post_file = create(:domain_post_file, :image_file)

      # Create a PostFileThumbnail with valid params
      thumbnail =
        described_class.new(post_file:, thumb_type: :size_32_32, frame: 0)

      expect(thumbnail).to be_valid
      expect(thumbnail.save).to be true
    end

    it "rejects non-thumbable content types" do
      post_file = create(:domain_post_file, :text_file)

      # Create a PostFileThumbnail with invalid content type
      thumbnail =
        described_class.new(post_file:, thumb_type: :size_32_32, frame: 0)

      expect(thumbnail).not_to be_valid
      expect(thumbnail.errors[:post_file]).to include(
        "must be a thumbnailable content type",
      )
    end
  end

  describe "thumbnailing" do
    context "with JPEG images" do
      it "successfully creates a thumbnail from a JPEG image" do
        post_file = create(:domain_post_file, :image_file)

        thumbnails = described_class.create_for_post_file!(post_file)
        # 1 for the 32x32
        # 1 for the content-container
        expect(thumbnails.size).to eq(2)

        # expect the 32x32 thumbnail
        thumbs_32_32 = post_file.thumbnails.where(thumb_type: :size_32_32)
        expect(thumbs_32_32.size).to eq(1)
        expect(thumbs_32_32.first.frame).to eq(0)

        # expect the content-container thumbnail
        thumbs_content_container =
          post_file.thumbnails.where(thumb_type: :content_container)
        expect(thumbs_content_container.size).to eq(1)
        expect(thumbs_content_container.first.frame).to eq(0)
      end
    end

    context "with GIF images" do
      it "successfully creates a thumbnails from a GIF image" do
        # 37 frames in the gif
        post_file = create(:domain_post_file, :gif_file)
        thumbnails = described_class.create_for_post_file!(post_file)
        # 5 for the 32x32
        # 1 for content-container
        expect(thumbnails.size).to eq(6)

        # expect the frames at 0, 0.1, 0.5, 0.9 and 1.0 through the gif
        # so frame numbers 0, 3, 18, 32 and 36
        thumbs_32_32 = post_file.thumbnails.where(thumb_type: :size_32_32)
        expect(thumbs_32_32.map(&:frame)).to eq([0, 3, 18, 32, 36])

        # expect the content-container thumbnail
        thumbs_content_container =
          post_file.thumbnails.where(thumb_type: :content_container)
        expect(thumbs_content_container.size).to eq(1)
        expect(thumbs_content_container.first.frame).to eq(0)
      end
    end

    context "with webm videos" do
      it "successfully creates a thumbnail from a webm video" do
        post_file = create(:domain_post_file, :webm_file)
        thumbnails = described_class.create_for_post_file!(post_file)
        expect(thumbnails.size).to eq(6)

        # expect 5x 32x32 thumbnails, 1 for the content-container
        thumbs_32_32 = post_file.thumbnails.where(thumb_type: :size_32_32)
        expect(thumbs_32_32.size).to eq(5)
        expect(thumbs_32_32.map(&:frame)).to eq([0, 90, 453, 816, 907])

        # expect the content-container thumbnail
        thumbs_content_container =
          post_file.thumbnails.where(thumb_type: :content_container)
        expect(thumbs_content_container.size).to eq(1)
        expect(thumbs_content_container.first.frame).to eq(0)
      end
    end

    context "with a .gif file that is actually a .png" do
      let(:post_file) do
        create(
          :domain_post_file,
          :gif_file,
          log_entry:
            create(
              :http_log_entry,
              content_type: "image/gif",
              response:
                create(
                  :blob_file,
                  content_type: "image/gif",
                  contents:
                    File.binread(
                      Rails.root.join(
                        "test/fixtures/files/images/fa.24326406.gif",
                      ),
                    ),
                ),
            ),
        )
      end

      it "successfully creates a thumbnail from a .gif file that is actually a .png" do
        thumbnails = described_class.create_for_post_file!(post_file)
        expect(thumbnails.size).to eq(2)
      end
    end
  end
end
