defmodule BerrypodWeb.Admin.DashboardTest do
  use BerrypodWeb.ConnCase, async: false

  import Phoenix.LiveViewTest
  import Berrypod.AccountsFixtures
  import Berrypod.OrdersFixtures

  setup do
    user = user_fixture()
    %{user: user}
  end

  describe "unauthenticated" do
    test "redirects to login", %{conn: conn} do
      {:error, redirect} = live(conn, ~p"/admin")
      assert {:redirect, %{to: path}} = redirect
      assert path == ~p"/users/log-in"
    end
  end

  describe "launch checklist" do
    setup %{conn: conn, user: user} do
      %{conn: log_in_user(conn, user)}
    end

    test "shows checklist when site not live", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/admin")

      assert html =~ "Launch checklist"
      assert html =~ "Connect a print provider"
      assert html =~ "Connect Stripe for payments"
      assert html =~ "Import products"
      assert html =~ "Customise your shop"
      assert html =~ "Go live"
    end

    test "hides checklist when site is live", %{conn: conn} do
      {:ok, _} = Berrypod.Settings.set_site_live(true)
      {:ok, _view, html} = live(conn, ~p"/admin")

      refute html =~ "Launch checklist"
    end

    test "collapse and expand checklist", %{conn: conn} do
      {:ok, view, html} = live(conn, ~p"/admin")
      assert html =~ "Import products"

      # Collapse
      html = render_click(view, "toggle_checklist")
      refute html =~ "Import products"
      assert html =~ "Launch checklist"

      # Expand
      html = render_click(view, "toggle_checklist")
      assert html =~ "Import products"
    end

    test "go live button works", %{conn: conn} do
      # Need provider + products + stripe + shipping for go live
      {:ok, conn_record} =
        Berrypod.Products.create_provider_connection(%{
          name: "Test",
          provider_type: "printify",
          api_key: "test_key"
        })

      {:ok, _} =
        Berrypod.Products.create_product(%{
          title: "Test product",
          provider_product_id: "ext-1",
          provider_connection_id: conn_record.id,
          status: "active"
        })

      {:ok, _} = Berrypod.Settings.put_secret("stripe_api_key", "sk_test_123")

      Berrypod.Shipping.upsert_rates(conn_record.id, [
        %{
          blueprint_id: 1,
          print_provider_id: 1,
          country_code: "GB",
          first_item_cost: 350,
          additional_item_cost: 200,
          currency: "GBP"
        }
      ])

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

      html = render_click(view, "go_live")

      assert html =~ "Your shop is live"
      assert Berrypod.Settings.site_live?()
    end

    test "shows test order guidance", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/admin")

      assert html =~ "4242 4242 4242 4242"
    end

    test "shows email setup item", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/admin")

      assert html =~ "Set up email"
    end
  end

  describe "stats" do
    setup %{conn: conn, user: user} do
      {:ok, _} = Berrypod.Settings.set_site_live(true)
      %{conn: log_in_user(conn, user)}
    end

    test "shows stats cards", %{conn: conn} do
      {:ok, view, _html} = live(conn, ~p"/admin")

      assert has_element?(view, "a[href='/admin/orders']", "Orders")
      assert has_element?(view, "a[href='/admin/products']", "Products")
    end

    test "shows zero state for orders", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/admin")

      assert html =~ "No orders yet"
    end

    test "shows recent orders when they exist", %{conn: conn} do
      order = order_fixture(%{payment_status: "paid"})
      {:ok, _view, html} = live(conn, ~p"/admin")

      assert html =~ order.order_number
      assert html =~ "Recent orders"
    end
  end
end
