require "application_system_test_case"

class PublicBrowsingTest < ApplicationSystemTestCase
  test "visitor can browse entries without logging in" do
    visit root_path

    assert_selector "h1", text: "Sanasto Wiki"
    assert_selector ".entry-row", minimum: 1
  end

  test "visitor can search entries" do
    entry = entries(:one)
    visit root_path

    fill_in "q", with: entry.fi
    click_button "Search"

    assert_text entry.fi
  end

  test "visitor can filter by language" do
    visit root_path

    click_button "Finnish"

    assert_current_path entries_path(language: "fi")
  end

  test "visitor can filter by category" do
    visit root_path

    select "Word", from: "category"

    assert_current_path entries_path(category: "word")
  end

  test "visitor can view entry details" do
    entry = entries(:one)
    visit root_path

    click_link entry.fi

    assert_text entry.fi
    assert_text entry.en if entry.en.present?
  end

  test "visitor can browse alphabetically" do
    visit root_path

    click_button "Finnish"
    click_link "A"

    assert_current_path entries_path(language: "fi", starts_with: "a")
  end

  test "visitor can download XLSX" do
    visit root_path

    click_link "Download XLSX"

    assert_equal "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                 page.response_headers["Content-Type"]
  end

  test "visitor sees sign in link in header" do
    visit root_path

    within "header" do
      assert_link "Sign In"
    end
  end

  test "visitor can request new entry" do
    visit root_path

    click_link "Request Entry"

    assert_current_path new_request_path
    assert_field "Name"
    assert_field "Email"
  end

  test "search results show no results message when nothing matches" do
    visit root_path

    fill_in "q", with: "nonexistentword12345xyz"
    click_button "Search"

    assert_text "No entries matched your filters"
  end

  test "visitor can paginate through results" do
    # Create enough entries to require pagination
    26.times do |i|
      Entry.create!(
        fi: "Test Entry #{i}",
        category: :word,
        status: :active
      )
    end

    visit root_path

    assert_selector ".entry-row", count: 25
    assert_link "2"
  end

  test "visitor sees entry statistics" do
    visit root_path

    assert_text "Total Entries"
    assert_text "Verified"
  end
end
