require "test_helper"
require "benchmark"

class SearchPerformanceTest < ActionDispatch::IntegrationTest
  setup do
    # Create a substantial number of test entries for performance testing
    @test_entries = []
    50.times do |i|
      @test_entries << Entry.create!(
        fi: "Testi sana #{i}",
        en: "Test word #{i}",
        sv: "Test ord #{i}",
        category: :word,
        status: :active
      )
    end
  end

  teardown do
    # Clean up test entries
    Entry.where(id: @test_entries.map(&:id)).delete_all
  end

  test "full text search completes in reasonable time" do
    measure_time("Full text search") do
      get entries_path, params: { q: "test" }
      assert_response :success
    end
  end

  test "language-specific search is performant" do
    measure_time("Language-specific search") do
      get entries_path, params: { q: "test", language: "en" }
      assert_response :success
    end
  end

  test "alphabetical browsing is performant" do
    measure_time("Alphabetical browsing") do
      get entries_path, params: { language: "fi", starts_with: "t" }
      assert_response :success
    end
  end

  test "category filtering is performant" do
    measure_time("Category filtering") do
      get entries_path, params: { category: "word" }
      assert_response :success
    end
  end

  test "combined filters are performant" do
    measure_time("Combined filters") do
      get entries_path, params: { q: "test", language: "fi", category: "word" }
      assert_response :success
    end
  end

  test "pagination does not degrade performance" do
    measure_time("Pagination") do
      get entries_path, params: { page: 2 }
      assert_response :success
    end
  end

  test "entry show page loads quickly" do
    entry = @test_entries.first

    measure_time("Entry show page") do
      get entry_path(entry)
      assert_response :success
    end
  end

  test "XLSX download handles large datasets" do
    measure_time("XLSX download") do
      get download_entries_path(format: :xlsx)
      assert_response :success
    end
  end

  test "statistics calculation is performant" do
    measure_time("Statistics calculation") do
      get entries_path
      assert_response :success
      assert_select "div", text: /entries/i
      assert_select "div", text: /% complete/i
    end
  end

  test "search with no results is fast" do
    measure_time("No results search") do
      get entries_path, params: { q: "nonexistentword12345xyz" }
      assert_response :success
    end
  end

  test "multiple sequential searches maintain performance" do
    searches = [ "test", "sana", "word", "ord" ]

    total_time = Benchmark.measure do
      searches.each do |query|
        get entries_path, params: { q: query }
        assert_response :success
      end
    end

    assert total_time.real < 2.0, "Multiple searches took too long: #{total_time.real}s"
  end

  test "turbo stream responses are performant" do
    measure_time("Turbo stream response") do
      get entries_path, as: :turbo_stream, params: { q: "test" }
      assert_response :success
    end
  end

  private

  def measure_time(description, max_time_ms: 500)
    time = Benchmark.measure do
      yield
    end

    time_ms = (time.real * 1000).round(2)
    assert time_ms < max_time_ms,
           "#{description} took too long: #{time_ms}ms (max: #{max_time_ms}ms)"
  end
end
