defmodule ToweropsWeb.OrgLiveTest do
  use ToweropsWeb.ConnCase

  import Phoenix.LiveViewTest

  setup :register_and_log_in_user

  describe "Index" do
    test "lists all user organizations", %{conn: conn, user: user} do
      {:ok, organization} =
        Towerops.Organizations.create_organization(%{name: "Test Org"}, user.id)

      {:ok, _view, html} = live(conn, ~p"/orgs")

      assert html =~ "Organizations"
      assert html =~ organization.name
    end

    test "displays empty state when user has no organizations", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/orgs")

      assert html =~ "No organizations"
    end

    test "has link to create new organization", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/orgs")

      assert html =~ "New Organization"
    end

    test "navigates to organization dashboard when clicking organization card", %{
      conn: conn,
      user: user
    } do
      {:ok, organization} =
        Towerops.Organizations.create_organization(%{name: "Test Org"}, user.id)

      {:ok, view, _html} = live(conn, ~p"/orgs")

      {:ok, _, html} =
        view
        |> element("button[phx-click='select_org'][phx-value-org-id='#{organization.id}']")
        |> render_click()
        |> follow_redirect(conn, ~p"/dashboard")

      assert html =~ organization.name
    end

    test "requires authentication" do
      conn = build_conn()
      {:error, redirect} = live(conn, ~p"/orgs")

      assert {:redirect, %{to: path}} = redirect
      assert path == ~p"/users/log-in"
    end
  end

  describe "New" do
    test "renders new organization form", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/orgs/new")

      assert html =~ "New Organization"
    end

    test "creates new organization", %{conn: conn, user: user} do
      {:ok, view, _html} = live(conn, ~p"/orgs/new")

      view
      |> form("#organization-form",
        organization: %{
          name: "New Organization"
        }
      )
      |> render_submit()

      # Verify organization was created
      organizations = Towerops.Organizations.list_user_organizations(user.id)
      assert Enum.any?(organizations, &(&1.name == "New Organization"))
    end

    test "validates required fields", %{conn: conn} do
      {:ok, view, _html} = live(conn, ~p"/orgs/new")

      html =
        view
        |> form("#organization-form", organization: %{name: ""})
        |> render_submit()

      assert html =~ "can&#39;t be blank"
    end

    test "allows duplicate organization names with different slugs", %{conn: conn, user: user} do
      # Make user a superuser to bypass free org limits
      user = Towerops.Repo.update!(Ecto.Changeset.change(user, is_superuser: true))

      {:ok, _organization} =
        Towerops.Organizations.create_organization(%{name: "Existing Org"}, user.id, bypass_limits: true)

      # Need to reconnect with updated user
      conn = log_in_user(conn, user)
      {:ok, view, _html} = live(conn, ~p"/orgs/new")

      view
      |> form("#organization-form",
        organization: %{
          name: "Existing Org"
        }
      )
      |> render_submit()

      # Verify both organizations exist with same name but different slugs
      organizations = Towerops.Organizations.list_user_organizations(user.id)
      existing_orgs = Enum.filter(organizations, &(&1.name == "Existing Org"))
      assert length(existing_orgs) == 2
      assert length(Enum.uniq_by(existing_orgs, & &1.slug)) == 2
    end

    test "requires authentication" do
      conn = build_conn()
      {:error, redirect} = live(conn, ~p"/orgs/new")

      assert {:redirect, %{to: path}} = redirect
      assert path == ~p"/users/log-in"
    end
  end
end
