"""End-to-end tests for the µFrame compile pipeline."""

import uframe


def test_empty_source():
    result = uframe.compile("")
    assert result.ascii == ""
    assert result.micron == ""


def test_simple_heading():
    result = uframe.compile('page "Test" 40\n  heading 1 "Hello World"')
    assert "Hello World" in result.ascii
    assert "Hello World" in result.micron


def test_box_with_title():
    source = '''page "Demo" 40
  box light "Status"
    text "All systems go"'''
    result = uframe.compile(source)
    # ASCII should have box-drawing characters
    assert "┌" in result.ascii
    assert "└" in result.ascii
    assert "Status" in result.ascii
    assert "All systems go" in result.ascii


def test_box_heavy():
    source = '''page "Demo" 40
  box heavy "Alert"
    text "Warning"'''
    result = uframe.compile(source)
    assert "┏" in result.ascii
    assert "Alert" in result.ascii


def test_box_double():
    source = '''page "Demo" 40
  box double "Title"
    text "Content"'''
    result = uframe.compile(source)
    assert "╔" in result.ascii
    assert "Title" in result.ascii


def test_box_rounded():
    source = '''page "Demo" 40
  box rounded "Panel"
    text "Inside"'''
    result = uframe.compile(source)
    assert "╭" in result.ascii
    assert "Panel" in result.ascii


def test_row_with_columns():
    source = '''page "Demo" 40
  row 2
    col 18
      text "Left"
    col 18
      text "Right"'''
    result = uframe.compile(source)
    assert "Left" in result.ascii
    assert "Right" in result.ascii


def test_label():
    source = '''page "Demo" 40
  label "Name" "Alice"'''
    result = uframe.compile(source)
    assert "Name:" in result.ascii
    assert "Alice" in result.ascii


def test_divider():
    source = '''page "Demo" 40
  divider heavy'''
    result = uframe.compile(source)
    assert "━" in result.ascii


def test_link():
    source = '''page "Demo" 40
  link "Home" "/page/index.mu"'''
    result = uframe.compile(source)
    assert "Home" in result.ascii
    # Micron should have link syntax
    assert "index.mu" in result.micron


def test_list():
    source = '''page "Demo" 40
  list bullet
    item "First"
    item "Second"'''
    result = uframe.compile(source)
    assert "First" in result.ascii
    assert "Second" in result.ascii


def test_spacer():
    source = '''page "Demo" 40
  text "Before"
  spacer 2
  text "After"'''
    result = uframe.compile(source)
    lines = result.ascii.split("\n")
    # Should have blank lines between Before and After
    before_idx = next(i for i, l in enumerate(lines) if "Before" in l)
    after_idx = next(i for i, l in enumerate(lines) if "After" in l)
    assert after_idx - before_idx >= 3  # at least 2 blank lines between


def test_nested_boxes():
    source = '''page "Demo" 40
  box light "Outer"
    box heavy "Inner"
      text "Deep"'''
    result = uframe.compile(source)
    assert "Outer" in result.ascii
    assert "Inner" in result.ascii
    assert "Deep" in result.ascii


def test_micron_has_style_tags():
    source = '''page "Demo" 40
  heading 1 "Title"'''
    result = uframe.compile(source)
    # Micron should contain color tags for the heading
    assert "`F" in result.micron or "Title" in result.micron


def test_gauge():
    source = '''page "Demo" 40
  gauge "CPU" 62 100 20 warn=75 crit=90'''
    result = uframe.compile(source)
    assert "CPU" in result.ascii
    assert "█" in result.ascii
    assert "62%" in result.ascii


def test_comment_ignored():
    source = '''page "Demo" 40
  # this is a comment
  text "Visible"'''
    result = uframe.compile(source)
    assert "Visible" in result.ascii
    assert "comment" not in result.ascii


def test_inline_modifiers():
    source = '''page "Demo" 40
  text "Hello @bold{world} today"'''
    result = uframe.compile(source)
    assert "Hello" in result.ascii
    assert "world" in result.ascii
    # Micron should have bold tags around "world"
    assert "`!" in result.micron


def test_status():
    source = '''page "Demo" 40
  status "Server" online'''
    result = uframe.compile(source)
    assert "●" in result.ascii
    assert "Server" in result.ascii


def test_table():
    source = '''page "Demo" 50
  table "Routes"
    columns "Destination" 20 | "Hops" 6 | "Status" 10
    row "relay-east" | "2" | "alive"
    row "bridge-south" | "4" | "alive"
    row "node-gamma" | "7" | "stale"'''
    result = uframe.compile(source)
    assert "┌" in result.ascii
    assert "┼" in result.ascii  # column separators at header line
    assert "Destination" in result.ascii
    assert "relay-east" in result.ascii
    assert "bridge-south" in result.ascii
    assert "stale" in result.ascii
    # Should have proper structure
    lines = result.ascii.split("\n")
    assert len(lines) >= 6  # top border + header + sep + 3 rows + bottom border


def test_table_with_color():
    source = '''page "Demo" 50
  table "Peers"
    columns "Name" 16 | "State" 12
    row "east-relay" | "@color{0f0}{● alive}"
    row "node-gamma" | "@color{f00}{○ stale}"'''
    result = uframe.compile(source)
    assert "●" in result.ascii
    assert "○" in result.ascii
    assert "`F0f0" in result.micron  # green color tag


def test_form_field():
    source = '''page "Demo" 50
  form "test"
    field "name" 20 "Enter name..."'''
    result = uframe.compile(source)
    assert "name:" in result.ascii
    assert "[" in result.ascii
    assert "]" in result.ascii
    # Micron should have form tag
    assert "`<" in result.micron or "<" in result.micron


def test_form_radio():
    source = '''page "Demo" 50
  form "test"
    radio "mode" "Ping" | "Trace" | "Page"'''
    result = uframe.compile(source)
    assert "(•)" in result.ascii  # first option selected
    assert "( )" in result.ascii  # other options unselected
    assert "Ping" in result.ascii
    assert "Trace" in result.ascii


def test_form_checkbox():
    source = '''page "Demo" 50
  form "test"
    checkbox "agree" "I agree to terms"'''
    result = uframe.compile(source)
    assert "[ ]" in result.ascii
    assert "I agree to terms" in result.ascii


def test_form_button():
    source = '''page "Demo" 50
  form "test"
    button "Submit" "/page/submit.mu"'''
    result = uframe.compile(source)
    assert "Submit" in result.ascii
    assert "submit.mu" in result.micron


def test_form_complete():
    source = '''page "Search" 50
  form "search"
    field "query" 24 "Search term..."
    radio "scope" "Local" | "Network"
    checkbox "cache" "Include cached"
    button "Go" "/page/search.mu"'''
    result = uframe.compile(source)
    assert "query:" in result.ascii
    assert "(•)" in result.ascii
    assert "[ ]" in result.ascii
    assert "Go" in result.ascii


def test_full_dashboard():
    """Integration test: a realistic dashboard layout."""
    source = '''page "Node Status" 60
  box double "Relay Alpha-7"
    align center
    text "Reticulum Network Node"

  spacer

  heading 1 "Resources"

  gauge "CPU" 62 100 28 warn=75 crit=90
  gauge "MEM" 84 100 28 warn=80 crit=95

  spacer

  heading 2 "Peers"

  label "Active" "7 / 12"
  status "East Relay" online
  status "South Bridge" online
  status "Node Gamma" degraded

  divider heavy

  link "Home" "/page/index.mu"'''

    result = uframe.compile(source)

    # Verify key elements are present
    assert "Relay Alpha-7" in result.ascii
    assert "╔" in result.ascii  # double box
    assert "CPU" in result.ascii
    assert "MEM" in result.ascii
    assert "█" in result.ascii  # gauge bars
    assert "●" in result.ascii  # status indicators
    assert "━" in result.ascii  # heavy divider
    assert "Home" in result.ascii

    # Micron should have color tags
    assert "`F" in result.micron
    assert result.micron  # non-empty
