require "application_system_test_case"

class ContributorWorkflowTest < ApplicationSystemTestCase
  setup do
    @contributor = users(:contributor_user)
    @contributor.update!(invitation_accepted_at: Time.current)
  end

  test "contributor can sign in" do
    visit login_path

    fill_in "Email", with: @contributor.email
    fill_in "Password", with: "password123456"
    click_button "Sign In"

    assert_text "Welcome back"
    within "header" do
      assert_text @contributor.name.split.first
    end
  end

  test "contributor can edit entry" do
    entry = entries(:one)
    login_as(@contributor)

    visit entry_path(entry)
    click_link "Edit"

    assert_current_path edit_entry_path(entry)

    fill_in "Finnish", with: "Updated Finnish Text"
    fill_in "English", with: "Updated English Text"
    select "Phrase", from: "Category"
    fill_in "Additional Notes", with: "Updated notes"

    click_button "Save Changes"

    assert_current_path entry_path(entry)
    assert_text "Entry updated"
    assert_text "Updated Finnish Text"
    assert_text "Updated English Text"
  end

  test "contributor can add comment to entry" do
    entry = entries(:one)
    login_as(@contributor)

    visit entry_path(entry)

    click_button "Add Comment"

    within "#comment_form_modal" do
      select "Finnish (FI)", from: "Language"
      fill_in "Comment", with: "This is my comment on the Finnish translation"
      click_button "Submit"
    end

    assert_text "This is my comment on the Finnish translation"
    assert_text @contributor.name
  end

  test "contributor sees signed in status in header" do
    login_as(@contributor)
    visit root_path

    within "header" do
      assert_text @contributor.name.split.first
      assert_button "Sign Out", visible: :all
    end
  end

  test "contributor can sign out" do
    login_as(@contributor)
    visit root_path

    # On mobile, click hamburger menu first
    if page.has_selector?("#mobile-menu-button", visible: true)
      click_button id: "mobile-menu-button"
    end

    click_link "Sign Out"

    assert_current_path root_path
    assert_text "You have been logged out"
    within "header" do
      assert_link "Sign In"
    end
  end

  test "contributor cannot access admin pages" do
    login_as(@contributor)

    visit admin_root_path

    assert_current_path root_path
    assert_text "administrator"
  end

  test "contributor can request password reset" do
    visit login_path

    click_link "Forgot password?"

    assert_current_path new_password_reset_path

    fill_in "Email", with: @contributor.email
    click_button "Send Reset Instructions"

    assert_current_path login_path
    assert_text "password reset instructions"
  end

  test "contributor session persists across page loads" do
    login_as(@contributor)

    visit root_path
    assert_text @contributor.name.split.first

    visit entries_path
    within "header" do
      assert_text @contributor.name.split.first
    end
  end

  test "contributor can use remember me" do
    visit login_path

    fill_in "Email", with: @contributor.email
    fill_in "Password", with: "password123456"
    check "Remember me for 2 weeks"
    click_button "Sign In"

    assert_text "Welcome back"
  end

  test "contributor sees validation errors when editing entry incorrectly" do
    entry = entries(:one)
    login_as(@contributor)

    visit edit_entry_path(entry)

    # Clear all translations (should fail validation)
    fill_in "Finnish", with: ""
    fill_in "English", with: ""
    fill_in "Swedish", with: ""
    fill_in "Norwegian", with: ""
    fill_in "Russian", with: ""
    fill_in "German", with: ""

    click_button "Save Changes"

    assert_text "At least one language translation is required"
  end

  test "contributor can filter comments by language tab" do
    entry = entries(:one)
    login_as(@contributor)

    # Create comments in different languages
    Comment.create!(
      commentable: entry,
      user: @contributor,
      body: "Finnish comment",
      language_code: "fi"
    )
    Comment.create!(
      commentable: entry,
      user: @contributor,
      body: "English comment",
      language_code: "en"
    )

    visit entry_path(entry)

    click_link "Finnish"
    assert_text "Finnish comment"

    click_link "English"
    assert_text "English comment"

    click_link "All languages"
    assert_text "Finnish comment"
    assert_text "English comment"
  end
end
