require "test_helper"

class EntryRequestTest < ActiveSupport::TestCase
  test "entry requires at least one translation" do
    entry = Entry.new(
      category: :word,
      status: :requested
    )

    assert_not entry.valid?
    assert_includes entry.errors[:base], "At least one language translation is required"
  end

  test "entry is valid with one translation" do
    entry = Entry.new(
      category: :word,
      fi: "sana",
      status: :requested
    )

    assert entry.valid?
  end

  test "entry is valid with multiple translations" do
    entry = Entry.new(
      category: :word,
      fi: "sana",
      en: "word",
      sv: "ord",
      status: :requested
    )

    assert entry.valid?
  end

  test "entry status defaults to active" do
    entry = Entry.new(
      category: :word,
      fi: "test"
    )

    assert_equal "active", entry.status
  end

  test "requested scope returns only requested entries" do
    requested_entries = Entry.requested
    assert_includes requested_entries, entries(:requested_entry)
    assert_not_includes requested_entries, entries(:one)
    assert_not_includes requested_entries, entries(:approved_entry)
  end

  test "approved scope returns only approved entries" do
    approved_entries = Entry.approved
    assert_includes approved_entries, entries(:approved_entry)
    assert_not_includes approved_entries, entries(:one)
    assert_not_includes approved_entries, entries(:requested_entry)
  end

  test "active_entries scope returns only active entries" do
    active_entries = Entry.active_entries
    assert_includes active_entries, entries(:one)
    assert_includes active_entries, entries(:two)
    assert_not_includes active_entries, entries(:requested_entry)
    assert_not_includes active_entries, entries(:approved_entry)
  end

  test "requested_by association" do
    entry = entries(:requested_entry)
    requester = users(:requester_user)

    assert_equal requester, entry.requested_by
  end

  test "user has requested_entries association" do
    requester = users(:requester_user)

    assert_includes requester.requested_entries, entries(:requested_entry)
  end

  test "can create entry with requested status" do
    user = users(:requester_user)
    entry = Entry.create!(
      category: :word,
      fi: "uusi",
      en: "new",
      status: :requested,
      requested_by: user
    )

    assert_equal "requested", entry.status
    assert_equal user, entry.requested_by
  end

  test "can transition entry from requested to approved to active" do
    entry = entries(:requested_entry)

    assert_equal "requested", entry.status

    entry.update!(status: :approved)
    assert_equal "approved", entry.status

    entry.update!(status: :active)
    assert_equal "active", entry.status
  end

  test "entries count does not include requested or approved" do
    total = Entry.count
    active = Entry.active_entries.count
    requested = Entry.requested.count
    approved = Entry.approved.count

    assert_equal total, active + requested + approved
    assert requested > 0, "Should have at least one requested entry in fixtures"
    assert approved > 0, "Should have at least one approved entry in fixtures"
  end

  test "blank translations are not counted as having translation" do
    entry = Entry.new(
      category: :word,
      fi: "",
      en: "  ",
      sv: nil
    )

    assert_not entry.valid?
    assert_includes entry.errors[:base], "At least one language translation is required"
  end
end
