require "test_helper"

class EntriesHelperTest < ActionView::TestCase
  setup do
    @entry = entries(:one)
  end

  # alphabet_letters tests
  test "alphabet_letters returns A-Z for blank language code" do
    result = alphabet_letters(nil)
    assert_equal ("A".."Z").to_a, result
    assert_equal 26, result.length
  end

  test "alphabet_letters returns A-Z for empty string" do
    result = alphabet_letters("")
    assert_equal ("A".."Z").to_a, result
  end

  test "alphabet_letters returns A-Z for unknown language" do
    result = alphabet_letters("unknown")
    assert_equal ("A".."Z").to_a, result
  end

  test "alphabet_letters returns A-Z for English" do
    result = alphabet_letters("en")
    assert_equal ("A".."Z").to_a, result
    assert_equal 26, result.length
  end

  test "alphabet_letters returns A-Z for German" do
    result = alphabet_letters("de")
    assert_equal ("A".."Z").to_a, result
  end

  test "alphabet_letters returns Cyrillic alphabet for Russian" do
    result = alphabet_letters("ru")
    assert_equal 33, result.length
    assert_includes result, "А"
    assert_includes result, "Я"
    assert_includes result, "Ё"
    assert_not_includes result, "A"
    assert_not_includes result, "Z"
  end

  test "alphabet_letters returns A-Z plus Æ Ø Å for Norwegian" do
    result = alphabet_letters("no")
    assert_equal 29, result.length
    assert_includes result, "A"
    assert_includes result, "Z"
    assert_includes result, "Æ"
    assert_includes result, "Ø"
    assert_includes result, "Å"
    assert_equal [ "Æ", "Ø", "Å" ], result.last(3)
  end

  test "alphabet_letters returns A-Z plus Å Ä Ö for Swedish" do
    result = alphabet_letters("sv")
    assert_equal 29, result.length
    assert_includes result, "A"
    assert_includes result, "Z"
    assert_includes result, "Å"
    assert_includes result, "Ä"
    assert_includes result, "Ö"
    assert_equal [ "Å", "Ä", "Ö" ], result.last(3)
  end

  test "alphabet_letters returns A-Z plus Å Ä Ö for Finnish" do
    result = alphabet_letters("fi")
    assert_equal 29, result.length
    assert_includes result, "A"
    assert_includes result, "Z"
    assert_includes result, "Å"
    assert_includes result, "Ä"
    assert_includes result, "Ö"
    assert_equal [ "Å", "Ä", "Ö" ], result.last(3)
  end

  # entry_translation_for tests
  test "entry_translation_for returns Finnish translation" do
    @entry.fi = "Suomalainen sana"
    result = entry_translation_for(@entry, "fi")
    assert_equal "Suomalainen sana", result
  end

  test "entry_translation_for returns English translation" do
    @entry.en = "English word"
    result = entry_translation_for(@entry, "en")
    assert_equal "English word", result
  end

  test "entry_translation_for returns Swedish translation" do
    @entry.sv = "Svenskt ord"
    result = entry_translation_for(@entry, "sv")
    assert_equal "Svenskt ord", result
  end

  test "entry_translation_for returns Norwegian translation" do
    @entry.no = "Norsk ord"
    result = entry_translation_for(@entry, "no")
    assert_equal "Norsk ord", result
  end

  test "entry_translation_for returns Russian translation" do
    @entry.ru = "Русское слово"
    result = entry_translation_for(@entry, "ru")
    assert_equal "Русское слово", result
  end

  test "entry_translation_for returns German translation" do
    @entry.de = "Deutsches Wort"
    result = entry_translation_for(@entry, "de")
    assert_equal "Deutsches Wort", result
  end

  test "entry_translation_for returns nil for invalid language code" do
    result = entry_translation_for(@entry, "invalid")
    assert_nil result
  end

  test "entry_translation_for returns nil for non-existent attribute" do
    result = entry_translation_for(@entry, "zz")
    assert_nil result
  end

  test "entry_translation_for returns nil for blank translation" do
    @entry.fi = nil
    result = entry_translation_for(@entry, "fi")
    assert_nil result
  end

  test "entry_translation_for handles symbol language code" do
    @entry.fi = "Test"
    result = entry_translation_for(@entry, :fi)
    assert_equal "Test", result
  end

  # format_entry_category tests
  test "format_entry_category formats word category" do
    @entry.category = "word"
    result = format_entry_category(@entry)
    assert_equal "Word", result
  end

  test "format_entry_category formats phrase category" do
    @entry.category = "phrase"
    result = format_entry_category(@entry)
    assert_equal "Phrase", result
  end

  test "format_entry_category formats proper_name category" do
    @entry.category = "proper_name"
    result = format_entry_category(@entry)
    assert_equal "Proper name", result
  end

  test "format_entry_category formats title category" do
    @entry.category = "title"
    result = format_entry_category(@entry)
    assert_equal "Title", result
  end

  test "format_entry_category formats reference category" do
    @entry.category = "reference"
    result = format_entry_category(@entry)
    assert_equal "Reference", result
  end

  test "format_entry_category formats other category" do
    @entry.category = "other"
    result = format_entry_category(@entry)
    assert_equal "Other", result
  end

  # format_entry_status tests
  test "format_entry_status returns requested badge" do
    @entry.status = :requested
    result = format_entry_status(@entry)
    assert_match /Requested/, result
    assert_match /bg-yellow-100/, result
    assert_match /text-yellow-800/, result
  end

  test "format_entry_status returns approved badge" do
    @entry.status = :approved
    result = format_entry_status(@entry)
    assert_match /Approved/, result
    assert_match /bg-blue-100/, result
    assert_match /text-blue-800/, result
  end

  test "format_entry_status returns active badge" do
    @entry.status = :active
    result = format_entry_status(@entry)
    assert_match /Active/, result
    assert_match /bg-green-100/, result
    assert_match /text-green-800/, result
  end

  test "format_entry_status badge contains proper HTML structure" do
    @entry.status = :active
    result = format_entry_status(@entry)
    assert_match /<span/, result
    assert_match /class=/, result
    assert_match /px-2 py-1/, result
    assert_match /text-xs/, result
    assert_match /font-semibold/, result
    assert_match /rounded-full/, result
  end
end
