# typed: false
require "rails_helper"

RSpec.describe Domain::PostsController, type: :controller do
  render_views

  # Create a real user with admin role
  let(:user) { create(:user, :admin) }

  before do
    # Sign in the user
    sign_in user
    # Mock authorization to allow all actions
    allow(controller).to receive(:authorize).and_return(true)
  end

  describe "GET #show" do
    shared_examples "a post" do
      it "returns a successful response and renders the show template" do
        get :show, params: { id: post.to_param }
        expect(response).to be_successful
        expect(response).to render_template(:show)
      end
    end

    context "with a fa post" do
      let(:post) { create(:domain_post_fa_post) }
      it_behaves_like "a post"
    end

    context "with a ib post" do
      let(:post) { create(:domain_post_inkbunny_post) }
      it_behaves_like "a post"
    end

    context "with an e621 post" do
      let(:post) { create(:domain_post_e621_post) }
      it_behaves_like "a post"
    end
  end

  describe "GET #visual_search" do
    it "returns a successful response and renders the visual_search template" do
      get :visual_search
      expect(response).to be_successful
      expect(response).to render_template(:visual_search)
    end
  end

  describe "POST #visual_results" do
    context "with no image provided" do
      it "renders the visual_search template" do
        post :visual_results
        expect(response).to render_template(:visual_search)
      end
    end

    context "with an image URL" do
      let(:mock_hash_value) { "1010101010101010" }
      let(:mock_detail_hash_value) { "0101010101010101" }
      let(:mock_fingerprints) { Domain::PostFile::BitFingerprint.none }
      let(:temp_file_path) { "/tmp/test_image.jpg" }

      it "uses Phash::Fingerprint model methods for fingerprinting and finding similar images" do
        # We need to mock the image downloading and processing since we can't do that in tests
        allow(controller).to receive(:process_image_input).and_return(
          [temp_file_path, "image/jpeg"],
        )
        allow(controller).to receive(:create_thumbnail).and_return(
          "data:image/jpeg;base64,FAKE",
        )

        # Set up expectations for our model methods - this is what we're really testing
        expect(Domain::PostFile::BitFingerprint).to receive(
          :from_file_path,
        ).with(temp_file_path).and_return(mock_hash_value)

        # Add expectation for detail fingerprint
        expect(Domain::PostFile::BitFingerprint).to receive(
          :detail_from_file_path,
        ).with(temp_file_path).and_return(mock_detail_hash_value)

        # Mock the similar fingerprints search
        expect(controller.helpers).to receive(:find_similar_fingerprints).with(
          fingerprint_value: mock_hash_value,
          fingerprint_detail_value: mock_detail_hash_value,
        ).and_return(mock_fingerprints)

        post :visual_results,
             params: {
               image_url: "https://example.com/image.jpg",
             }

        # Just verify the template was rendered
        expect(response).to render_template(:visual_results)

        # Verify that the correct instance variables are set
        expect(assigns(:uploaded_image_data_uri)).to eq(
          "data:image/jpeg;base64,FAKE",
        )
        expect(assigns(:uploaded_hash_value)).to eq(mock_hash_value)
        expect(assigns(:uploaded_detail_hash_value)).to eq(
          mock_detail_hash_value,
        )
        expect(assigns(:post_file_fingerprints)).to eq(mock_fingerprints.to_a)
      end
    end
  end
end
