defmodule SimpleshopThemeWeb.ImageControllerTest do
  use SimpleshopThemeWeb.ConnCase

  alias SimpleshopTheme.Media

  # Minimal valid PNG (1x1 transparent pixel)
  @png_binary <<137, 80, 78, 71, 13, 10, 26, 10>>
  @svg_content ~s(<svg xmlns="http://www.w3.org/2000/svg"><circle fill="#000000" r="10"/></svg>)
  @sample_jpeg File.read!("test/fixtures/sample_1200x800.jpg")

  describe "show/2" do
    test "returns 404 for non-existent image", %{conn: conn} do
      conn = get(conn, ~p"/images/#{Ecto.UUID.generate()}")
      assert response(conn, 404) =~ "Image not found"
    end

    test "serves image with proper content type and caching headers", %{conn: conn} do
      # Upload a real JPEG which gets converted to WebP
      {:ok, image} =
        Media.upload_image(%{
          image_type: "logo",
          filename: "test.jpg",
          content_type: "image/jpeg",
          file_size: byte_size(@sample_jpeg),
          data: @sample_jpeg
        })

      conn = get(conn, ~p"/images/#{image.id}")

      assert response(conn, 200)
      # Image is converted to WebP for storage
      assert get_resp_header(conn, "content-type") == ["image/webp; charset=utf-8"]
      assert get_resp_header(conn, "cache-control") == ["public, max-age=31536000, immutable"]
      assert get_resp_header(conn, "etag") == [~s("#{image.id}")]
    end
  end

  describe "thumbnail/2" do
    test "returns 404 for non-existent image", %{conn: conn} do
      conn = get(conn, ~p"/images/#{Ecto.UUID.generate()}/thumbnail")
      assert response(conn, 404) =~ "Image not found"
    end

    test "generates thumbnail on-demand and serves as JPEG", %{conn: conn} do
      # Upload a real image to test thumbnail generation
      {:ok, image} =
        Media.upload_image(%{
          image_type: "logo",
          filename: "test.jpg",
          content_type: "image/jpeg",
          file_size: byte_size(@sample_jpeg),
          data: @sample_jpeg
        })

      conn = get(conn, ~p"/images/#{image.id}/thumbnail")

      assert response(conn, 200)
      # Thumbnail is served as JPEG
      assert get_resp_header(conn, "content-type") == ["image/jpeg; charset=utf-8"]
      assert get_resp_header(conn, "cache-control") == ["public, max-age=31536000, immutable"]
    end

    test "falls back to full image when image data is invalid", %{conn: conn} do
      # This uses an invalid PNG header that can't be processed
      {:ok, image} =
        Media.upload_image(%{
          image_type: "logo",
          filename: "test.png",
          content_type: "image/png",
          file_size: byte_size(@png_binary),
          data: @png_binary
        })

      conn = get(conn, ~p"/images/#{image.id}/thumbnail")

      # Falls back to WebP since that's what we tried to convert to
      assert response(conn, 200)
    end
  end

  describe "recolored_svg/2" do
    test "returns 404 for non-existent image", %{conn: conn} do
      conn = get(conn, ~p"/images/#{Ecto.UUID.generate()}/recolored/ff6600")
      assert response(conn, 404) =~ "Image not found"
    end

    test "returns 400 for non-SVG image", %{conn: conn} do
      {:ok, image} =
        Media.upload_image(%{
          image_type: "logo",
          filename: "test.png",
          content_type: "image/png",
          file_size: byte_size(@png_binary),
          data: @png_binary
        })

      conn = get(conn, ~p"/images/#{image.id}/recolored/ff6600")
      assert response(conn, 400) =~ "not an SVG"
    end

    test "returns 400 for invalid color", %{conn: conn} do
      {:ok, image} =
        Media.upload_image(%{
          image_type: "logo",
          filename: "test.svg",
          content_type: "image/svg+xml",
          file_size: byte_size(@svg_content),
          data: @svg_content
        })

      conn = get(conn, ~p"/images/#{image.id}/recolored/invalid")
      assert response(conn, 400) =~ "Invalid color"
    end

    test "recolors SVG with valid hex color", %{conn: conn} do
      {:ok, image} =
        Media.upload_image(%{
          image_type: "logo",
          filename: "test.svg",
          content_type: "image/svg+xml",
          file_size: byte_size(@svg_content),
          data: @svg_content
        })

      conn = get(conn, ~p"/images/#{image.id}/recolored/ff6600")

      assert response(conn, 200) =~ ~s(fill="#ff6600")
      assert get_resp_header(conn, "content-type") == ["image/svg+xml; charset=utf-8"]
      assert get_resp_header(conn, "cache-control") == ["public, max-age=3600"]
    end

    test "handles color with leading hash", %{conn: conn} do
      {:ok, image} =
        Media.upload_image(%{
          image_type: "logo",
          filename: "test.svg",
          content_type: "image/svg+xml",
          file_size: byte_size(@svg_content),
          data: @svg_content
        })

      # URL encodes # as %23
      conn = get(conn, "/images/#{image.id}/recolored/%23ff6600")

      assert response(conn, 200) =~ ~s(fill="#ff6600")
    end
  end
end
