defmodule MvWeb.SidebarAuthorizationTest do
  @moduledoc """
  Tests for sidebar menu visibility based on user permissions (can_access_page?).
  """
  use MvWeb.ConnCase, async: false

  import Phoenix.LiveViewTest
  import MvWeb.Layouts.Sidebar

  alias Mv.Fixtures

  defp render_sidebar(assigns) do
    render_component(&sidebar/1, assigns)
  end

  defp sidebar_assigns(current_user, opts \\ []) do
    mobile = Keyword.get(opts, :mobile, false)
    club_name = Keyword.get(opts, :club_name, "Test Club")

    %{
      current_user: current_user,
      club_name: club_name,
      mobile: mobile
    }
  end

  describe "sidebar menu with admin user" do
    test "shows Members, Fee Types, Statistics and Administration with all subitems" do
      user = Fixtures.user_with_role_fixture("admin")
      html = render_sidebar(sidebar_assigns(user))

      assert html =~ ~s(href="/members")
      assert html =~ ~s(href="/membership_fee_types")
      assert html =~ ~s(href="/statistics")
      assert html =~ ~s(data-testid="sidebar-administration")
      assert html =~ ~s(href="/users")
      assert html =~ ~s(href="/groups")
      assert html =~ ~s(href="/admin/roles")
      assert html =~ ~s(href="/membership_fee_settings")
      assert html =~ ~s(href="/settings")
    end
  end

  describe "sidebar menu with read_only user (Vorstand/Buchhaltung)" do
    test "shows Members, Statistics and Groups (from Administration)" do
      user = Fixtures.user_with_role_fixture("read_only")
      html = render_sidebar(sidebar_assigns(user))

      assert html =~ ~s(href="/members")
      assert html =~ ~s(href="/statistics")
      assert html =~ ~s(href="/groups")
    end

    test "does not show Fee Types, Users, Roles or Settings" do
      user = Fixtures.user_with_role_fixture("read_only")
      html = render_sidebar(sidebar_assigns(user))

      refute html =~ ~s(href="/membership_fee_types")
      refute html =~ ~s(href="/users")
      refute html =~ ~s(href="/admin/roles")
      refute html =~ ~s(href="/settings")
    end
  end

  describe "sidebar menu with normal_user (Kassenwart)" do
    test "shows Members, Statistics and Groups" do
      user = Fixtures.user_with_role_fixture("normal_user")
      html = render_sidebar(sidebar_assigns(user))

      assert html =~ ~s(href="/members")
      assert html =~ ~s(href="/statistics")
      assert html =~ ~s(href="/groups")
    end

    test "does not show Fee Types, Users, Roles or Settings" do
      user = Fixtures.user_with_role_fixture("normal_user")
      html = render_sidebar(sidebar_assigns(user))

      refute html =~ ~s(href="/membership_fee_types")
      refute html =~ ~s(href="/users")
      refute html =~ ~s(href="/admin/roles")
      refute html =~ ~s(href="/settings")
    end
  end

  describe "sidebar menu with own_data user (Mitglied)" do
    test "does not show Members link (no /members page access)" do
      user = Fixtures.user_with_role_fixture("own_data")
      html = render_sidebar(sidebar_assigns(user))

      refute html =~ ~s(href="/members")
    end

    test "does not show Statistics, Fee Types or Administration" do
      user = Fixtures.user_with_role_fixture("own_data")
      html = render_sidebar(sidebar_assigns(user))

      refute html =~ ~s(href="/statistics")
      refute html =~ ~s(href="/membership_fee_types")
      refute html =~ ~s(href="/users")
      refute html =~ ~s(data-testid="sidebar-administration")
    end
  end

  describe "sidebar with nil current_user" do
    test "does not render menu items (only header and footer when present)" do
      html = render_sidebar(sidebar_assigns(nil))

      refute html =~ ~s(role="menubar")
      refute html =~ ~s(href="/members")
    end
  end

  describe "sidebar with user without role" do
    test "does not show any navigation links" do
      user = %{id: "user-no-role", email: "noreply@test.com", role: nil}
      html = render_sidebar(sidebar_assigns(user))

      refute html =~ ~s(href="/members")
      refute html =~ ~s(href="/membership_fee_types")
      refute html =~ ~s(href="/users")
    end
  end
end
