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

  alias SimpleshopTheme.Theme.CSSGenerator
  alias SimpleshopTheme.Settings.ThemeSettings

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

      assert is_binary(css)
      assert css =~ ".preview-frame, .shop-root"
      assert css =~ "--t-accent-h:"
      assert css =~ "--t-accent-s:"
      assert css =~ "--t-accent-l:"
    end

    test "generates correct mood variables for neutral" do
      settings = %ThemeSettings{mood: "neutral"}
      css = CSSGenerator.generate(settings)

      assert css =~ "--t-surface-base: #ffffff"
      assert css =~ "--t-text-primary: #171717"
      assert css =~ "--t-border-default: #e5e5e5"
    end

    test "generates correct mood variables for dark" do
      settings = %ThemeSettings{mood: "dark"}
      css = CSSGenerator.generate(settings)

      assert css =~ "--t-surface-base: #0a0a0a"
      assert css =~ "--t-text-primary: #fafafa"
      assert css =~ "--t-border-default: #262626"
      assert css =~ "--p-shadow-strength: 0.25"
    end

    test "generates correct mood variables for warm" do
      settings = %ThemeSettings{mood: "warm"}
      css = CSSGenerator.generate(settings)

      assert css =~ "--t-surface-base: #fdf8f3"
      assert css =~ "--t-text-primary: #1c1917"
    end

    test "generates correct mood variables for cool" do
      settings = %ThemeSettings{mood: "cool"}
      css = CSSGenerator.generate(settings)

      assert css =~ "--t-surface-base: #f4f7fb"
      assert css =~ "--t-text-primary: #0f172a"
    end

    test "generates correct typography for clean" do
      settings = %ThemeSettings{typography: "clean"}
      css = CSSGenerator.generate(settings)

      assert css =~ "--t-font-heading: var(--p-font-inter)"
      assert css =~ "--t-font-body: var(--p-font-inter)"
      assert css =~ "--t-heading-weight: 600"
    end

    test "generates correct typography for editorial" do
      settings = %ThemeSettings{typography: "editorial"}
      css = CSSGenerator.generate(settings)

      assert css =~ "--t-font-heading: var(--p-font-fraunces)"
      assert css =~ "--t-font-body: var(--p-font-source)"
    end

    test "generates correct typography for modern" do
      settings = %ThemeSettings{typography: "modern"}
      css = CSSGenerator.generate(settings)

      assert css =~ "--t-font-heading: var(--p-font-space)"
      assert css =~ "--t-heading-weight: 500"
    end

    test "generates correct shape for sharp" do
      settings = %ThemeSettings{shape: "sharp"}
      css = CSSGenerator.generate(settings)

      assert css =~ "--t-radius-button: 0"
      assert css =~ "--t-radius-card: 0"
    end

    test "generates correct shape for soft" do
      settings = %ThemeSettings{shape: "soft"}
      css = CSSGenerator.generate(settings)

      assert css =~ "--t-radius-button: var(--p-radius-md)"
      assert css =~ "--t-radius-card: var(--p-radius-lg)"
    end

    test "generates correct shape for round" do
      settings = %ThemeSettings{shape: "round"}
      css = CSSGenerator.generate(settings)

      assert css =~ "--t-radius-button: var(--p-radius-lg)"
      assert css =~ "--t-radius-card: var(--p-radius-xl)"
    end

    test "generates correct shape for pill" do
      settings = %ThemeSettings{shape: "pill"}
      css = CSSGenerator.generate(settings)

      assert css =~ "--t-radius-button: var(--p-radius-full)"
    end

    test "generates correct density for spacious" do
      settings = %ThemeSettings{density: "spacious"}
      css = CSSGenerator.generate(settings)

      assert css =~ "--t-density: 1.25"
    end

    test "generates correct density for balanced" do
      settings = %ThemeSettings{density: "balanced"}
      css = CSSGenerator.generate(settings)

      assert css =~ "--t-density: 1"
    end

    test "generates correct density for compact" do
      settings = %ThemeSettings{density: "compact"}
      css = CSSGenerator.generate(settings)

      assert css =~ "--t-density: 0.85"
    end

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

      # Red should be H=0, S=100%, L=50%
      assert css =~ "--t-accent-h: 0"
      assert css =~ "--t-accent-s: 100%"
      assert css =~ "--t-accent-l: 50%"
    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
  end
end
