defmodule Berrypod.Theme.CSSGeneratorTest do
  use ExUnit.Case, async: true

  alias Berrypod.Theme.CSSGenerator
  alias Berrypod.Settings.ThemeSettings

  describe "generate/1" do
    test "generates CSS for default theme settings" do
      settings = %ThemeSettings{}
      css = CSSGenerator.generate(settings)

      assert is_binary(css)
      # CSS targets .themed (used by both shop and preview)
      assert css =~ ".themed {"
      assert css =~ "--t-accent-l:"
      assert css =~ "--t-accent-c:"
      assert css =~ "--t-accent-h:"
      # Should include all theme token categories
      assert css =~ "--t-surface-base:"
      assert css =~ "--t-font-heading:"
      assert css =~ "--t-radius-sm:"
      assert css =~ "--t-density:"
      # Should include color-scheme
      assert css =~ "color-scheme:"
    end

    test "converts hex colours to oklch" do
      settings = %ThemeSettings{accent_color: "#ff0000"}
      css = CSSGenerator.generate(settings)

      # Red in oklch: L≈62.8%, C≈0.2577, H≈29.23°
      assert css =~ "--t-accent-l: 62.8"
      assert css =~ "--t-accent-c: 0.257"
      assert css =~ "--t-accent-h: 29.2"
    end

    test "handles blue accent colour" do
      settings = %ThemeSettings{accent_color: "#0000ff"}
      css = CSSGenerator.generate(settings)

      # Blue in oklch: L≈45.2%, C≈0.3132, H≈264.05°
      assert css =~ "--t-accent-l: 45.2"
      assert css =~ "--t-accent-c: 0.313"
      assert css =~ "--t-accent-h: 264."
    end

    test "handles green accent colour" do
      settings = %ThemeSettings{accent_color: "#00ff00"}
      css = CSSGenerator.generate(settings)

      # Green in oklch: L≈86.6%, C≈0.2948, H≈142.5°
      assert css =~ "--t-accent-l: 86.6"
      assert css =~ "--t-accent-c: 0.294"
      assert css =~ "--t-accent-h: 142."
    end

    test "sets color-scheme based on mood" do
      light = CSSGenerator.generate(%ThemeSettings{mood: "neutral"})
      dark = CSSGenerator.generate(%ThemeSettings{mood: "dark"})

      assert light =~ "color-scheme: light"
      assert dark =~ "color-scheme: dark"
    end

    test "includes secondary colors" do
      settings = %ThemeSettings{
        secondary_accent_color: "#ea580c",
        sale_color: "#dc2626"
      }

      css = CSSGenerator.generate(settings)

      assert css =~ "--t-secondary-accent: #ea580c"
      assert css =~ "--t-sale-color: #dc2626"
    end

    test "generates font size scale for small" do
      settings = %ThemeSettings{font_size: "small"}
      css = CSSGenerator.generate(settings)

      assert css =~ "--t-font-size-scale: 1.125"
    end

    test "generates font size scale for medium" do
      settings = %ThemeSettings{font_size: "medium"}
      css = CSSGenerator.generate(settings)

      assert css =~ "--t-font-size-scale: 1.1875"
    end

    test "generates font size scale for large" do
      settings = %ThemeSettings{font_size: "large"}
      css = CSSGenerator.generate(settings)

      assert css =~ "--t-font-size-scale: 1.25"
    end

    test "generates heading weight override for regular" do
      settings = %ThemeSettings{heading_weight: "regular"}
      css = CSSGenerator.generate(settings)

      assert css =~ "--t-heading-weight-override: 400"
    end

    test "generates heading weight override for bold" do
      settings = %ThemeSettings{heading_weight: "bold"}
      css = CSSGenerator.generate(settings)

      assert css =~ "--t-heading-weight-override: 700"
    end

    test "generates layout width for contained" do
      settings = %ThemeSettings{layout_width: "contained"}
      css = CSSGenerator.generate(settings)

      assert css =~ "--t-layout-max-width: 1100px"
    end

    test "generates layout width for wide" do
      settings = %ThemeSettings{layout_width: "wide"}
      css = CSSGenerator.generate(settings)

      assert css =~ "--t-layout-max-width: 1400px"
    end

    test "generates layout width for full" do
      settings = %ThemeSettings{layout_width: "full"}
      css = CSSGenerator.generate(settings)

      assert css =~ "--t-layout-max-width: 100%"
    end

    test "generates button style for filled" do
      settings = %ThemeSettings{button_style: "filled"}
      css = CSSGenerator.generate(settings)

      assert css =~ "--t-button-style: filled"
    end

    test "generates button style for outline" do
      settings = %ThemeSettings{button_style: "outline"}
      css = CSSGenerator.generate(settings)

      assert css =~ "--t-button-style: outline"
    end

    test "generates product text alignment" do
      settings = %ThemeSettings{product_text_align: "center"}
      css = CSSGenerator.generate(settings)

      assert css =~ "--t-product-text-align: center"
    end

    test "generates image aspect ratio for square" do
      settings = %ThemeSettings{image_aspect_ratio: "square"}
      css = CSSGenerator.generate(settings)

      assert css =~ "--t-image-aspect-ratio: 1 / 1"
    end

    test "generates image aspect ratio for portrait" do
      settings = %ThemeSettings{image_aspect_ratio: "portrait"}
      css = CSSGenerator.generate(settings)

      assert css =~ "--t-image-aspect-ratio: 3 / 4"
    end

    test "generates image aspect ratio for landscape" do
      settings = %ThemeSettings{image_aspect_ratio: "landscape"}
      css = CSSGenerator.generate(settings)

      assert css =~ "--t-image-aspect-ratio: 4 / 3"
    end
  end
end
