require "application_system_test_case"

class AdminWorkflowTest < ApplicationSystemTestCase
  setup do
    @admin = users(:admin_user)
    @admin.update!(invitation_accepted_at: Time.current)
  end

  test "admin can access dashboard" do
    login_as(@admin)

    visit admin_root_path

    assert_text "Dashboard"
    assert_text "Total Users"
    assert_text "Total Entries"
  end

  test "admin sees admin button in header" do
    login_as(@admin)
    visit root_path

    within "header" do
      assert_link "Admin"
    end
  end

  test "admin can send invitation" do
    login_as(@admin)

    visit admin_invitations_path
    click_link "Send New Invitation"

    assert_current_path new_admin_invitation_path

    fill_in "Name", with: "New User"
    fill_in "Email", with: "newuser@example.com"
    select "Contributor", from: "Role"

    click_button "Send Invitation"

    assert_current_path admin_invitations_path
    assert_text "Invitation sent"
    assert_text "newuser@example.com"
  end

  test "admin can view users list" do
    login_as(@admin)

    visit admin_users_path

    assert_text "Users"
    assert_selector "table"
    assert_text @admin.email
  end

  test "admin can edit user role" do
    user = users(:contributor_user)
    login_as(@admin)

    visit admin_users_path
    within "#user_#{user.id}" do
      click_link "Edit"
    end

    assert_current_path edit_admin_user_path(user)

    select "Reviewer", from: "Role"
    click_button "Update User"

    assert_current_path admin_users_path
    assert_text "User updated"

    user.reload
    assert_equal "reviewer", user.role
  end

  test "admin can delete user" do
    user = users(:contributor_user)
    login_as(@admin)

    visit admin_users_path

    within "#user_#{user.id}" do
      click_button "Delete"
    end

    assert_current_path admin_users_path
    assert_text "User deleted"
    assert_no_text user.email
  end

  test "admin can view entry requests" do
    requested_entry = Entry.create!(
      fi: "Requested Entry",
      category: :word,
      status: :requested,
      requested_by: users(:contributor_user)
    )

    login_as(@admin)

    visit admin_requests_path

    assert_text "Requested Entry"
    assert_link "View"
    assert_link "Edit"
    assert_button "Approve"
  end

  test "admin can approve entry request" do
    requester = users(:contributor_user)
    requested_entry = Entry.create!(
      fi: "Requested Entry",
      category: :word,
      status: :requested,
      requested_by: requester
    )

    login_as(@admin)

    visit admin_requests_path

    within "#entry_#{requested_entry.id}" do
      click_button "Approve"
    end

    assert_current_path admin_requests_path
    assert_text "approved"

    requested_entry.reload
    assert_equal "approved", requested_entry.status
  end

  test "admin can edit entry request before approval" do
    requested_entry = Entry.create!(
      fi: "Requested Entry",
      category: :word,
      status: :requested,
      requested_by: users(:contributor_user)
    )

    login_as(@admin)

    visit edit_admin_request_path(requested_entry)

    fill_in "Finnish", with: "Edited Finnish"
    fill_in "English", with: "Added English"
    click_button "Save Changes"

    assert_current_path admin_requests_path
    requested_entry.reload
    assert_equal "Edited Finnish", requested_entry.fi
    assert_equal "Added English", requested_entry.en
  end

  test "admin can reject entry request" do
    requested_entry = Entry.create!(
      fi: "Requested Entry",
      category: :word,
      status: :requested,
      requested_by: users(:contributor_user)
    )

    login_as(@admin)

    visit admin_requests_path

    within "#entry_#{requested_entry.id}" do
      click_button "Reject"
    end

    assert_current_path admin_requests_path
    assert_text "rejected"
    assert_nil Entry.find_by(id: requested_entry.id)
  end

  test "admin can resend invitation" do
    pending_user = users(:pending_invitation)
    login_as(@admin)

    visit admin_invitations_path

    within "#invitation_#{pending_user.id}" do
      click_button "Resend"
    end

    assert_current_path admin_invitations_path
    assert_text "Invitation resent"
  end

  test "admin can cancel invitation" do
    pending_user = users(:pending_invitation)
    login_as(@admin)

    visit admin_invitations_path

    within "#invitation_#{pending_user.id}" do
      click_button "Cancel"
    end

    assert_current_path admin_invitations_path
    assert_text "deleted"
    assert_nil User.find_by(id: pending_user.id)
  end

  test "admin sees request count badge" do
    Entry.create!(
      fi: "Requested Entry 1",
      category: :word,
      status: :requested,
      requested_by: users(:contributor_user)
    )
    Entry.create!(
      fi: "Requested Entry 2",
      category: :word,
      status: :requested,
      requested_by: users(:contributor_user)
    )

    login_as(@admin)
    visit admin_root_path

    within "header" do
      assert_text "2"
    end
  end

  test "admin navigation is responsive" do
    login_as(@admin)
    visit admin_root_path

    # Test mobile menu if visible
    if page.has_selector?("#admin-mobile-menu-button", visible: true)
      click_button id: "admin-mobile-menu-button"
      assert_selector "#admin-mobile-menu", visible: true
      assert_link "Dashboard"
      assert_link "Users"
      assert_link "Invitations"
    end
  end

  test "admin cannot delete themselves" do
    login_as(@admin)

    visit admin_users_path

    within "#user_#{@admin.id}" do
      click_button "Delete"
    end

    assert_text "cannot delete yourself"
    assert User.exists?(@admin.id)
  end

  test "admin can view dashboard statistics" do
    login_as(@admin)

    visit admin_dashboard_path

    assert_text "Total Users"
    assert_text "Total Entries"
    assert_text "Pending Invitations"
    assert_text "Entry Requests"
  end

  test "admin can navigate between admin sections" do
    login_as(@admin)

    visit admin_root_path

    click_link "Users"
    assert_current_path admin_users_path

    click_link "Invitations"
    assert_current_path admin_invitations_path

    click_link "Requests"
    assert_current_path admin_requests_path

    click_link "Dashboard"
    assert_current_path admin_dashboard_path
  end

  test "admin can return to main site from admin" do
    login_as(@admin)

    visit admin_root_path

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

    click_link "Back to Site"

    assert_current_path root_path
  end
end
