defmodule BerrypodWeb.SeoControllerTest do
  use BerrypodWeb.ConnCase, async: false

  import Berrypod.AccountsFixtures
  import Berrypod.ProductsFixtures

  alias Berrypod.Pages
  alias Berrypod.Pages.PageCache

  setup do
    PageCache.invalidate_all()
    user_fixture()
    {:ok, _} = Berrypod.Settings.set_site_live(true)
    :ok
  end

  describe "GET /robots.txt" do
    test "returns 200 with text/plain content type", %{conn: conn} do
      conn = get(conn, "/robots.txt")
      assert response_content_type(conn, :text) =~ "text/plain"
      assert response(conn, 200)
    end

    test "allows crawling of shop pages", %{conn: conn} do
      body = get(conn, "/robots.txt") |> response(200)
      assert body =~ "User-agent: *"
      assert body =~ "Allow: /"
    end

    test "disallows admin and sensitive paths", %{conn: conn} do
      body = get(conn, "/robots.txt") |> response(200)
      assert body =~ "Disallow: /admin/"
      assert body =~ "Disallow: /api/"
    end

    test "includes sitemap URL", %{conn: conn} do
      body = get(conn, "/robots.txt") |> response(200)
      assert body =~ "Sitemap:"
      assert body =~ "/sitemap.xml"
    end
  end

  describe "GET /sitemap.xml" do
    test "returns 200 with application/xml content type", %{conn: conn} do
      conn = get(conn, "/sitemap.xml")
      assert response_content_type(conn, :xml) =~ "application/xml"
      assert response(conn, 200)
    end

    test "includes static shop pages", %{conn: conn} do
      body = get(conn, "/sitemap.xml") |> response(200)
      assert body =~ "<loc>"
      assert body =~ "/collections/all"
      assert body =~ "/about"
      assert body =~ "/contact"
    end

    test "includes visible product URLs", %{conn: conn} do
      product = product_fixture(%{slug: "test-sitemap-tee", visible: true, status: "active"})

      body = get(conn, "/sitemap.xml") |> response(200)
      assert body =~ "/products/#{product.slug}"
    end

    test "is valid XML with urlset root element", %{conn: conn} do
      body = get(conn, "/sitemap.xml") |> response(200)
      assert body =~ ~s(xmlns="http://www.sitemaps.org/schemas/sitemap/0.9")
      assert body =~ "<urlset"
      assert body =~ "</urlset>"
    end

    test "includes published custom pages", %{conn: conn} do
      {:ok, _} = Pages.create_custom_page(%{slug: "faq", title: "FAQ", published: true})

      body = get(conn, "/sitemap.xml") |> response(200)
      assert body =~ "/faq"
    end

    test "excludes unpublished custom pages", %{conn: conn} do
      {:ok, _} = Pages.create_custom_page(%{slug: "draft", title: "Draft", published: false})

      body = get(conn, "/sitemap.xml") |> response(200)
      refute body =~ "/draft"
    end
  end
end
