"""Generate 9-stage Scenario Sequence SVG (Dark Terminal style)."""
OUT = 'docs/architecture/scenario-sequence.svg'

BG = '#0f0f1a'
PANEL = '#161628'
PANEL_LT = '#1c1c34'
BORDER = '#2d2d4a'
BORDER_S = '#3a3a5c'
TEXT = '#e4e4ef'
TEXT_DIM = '#8a8aa8'
TEXT_MUTE = '#5a5a78'
CYAN = '#22d3ee'
GREEN = '#34d399'
PURPLE = '#a78bfa'
ORANGE = '#f97316'
BLUE = '#60a5fa'
PINK = '#f472b6'
YELLOW = '#fbbf24'

L = []
a = L.append

# 6 lifelines, viewBox width 1320
LL = [
    (100, 'User', CYAN),
    (320, 'Claude Code Agent', CYAN),
    (540, 'CLI  cli.ts', CYAN),
    (760, 'Step-N Skill', ORANGE),
    (980, 'Governance Engines', PURPLE),
    (1220, 'Team Runtime', GREEN),
]

VB_W = 1320
VB_H = 1440

a(f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {VB_W} {VB_H}" '
  f'font-family="ui-sans-serif,system-ui,-apple-system,Segoe UI,Roboto,Menlo,Consolas,monospace">')
a(f'<rect width="{VB_W}" height="{VB_H}" fill="{BG}"/>')

a('<defs>')
for mid, color in [('cy', CYAN), ('or', ORANGE), ('bl', BLUE),
                   ('gr', GREEN), ('pu', PURPLE), ('dim', TEXT_DIM),
                   ('pk', PINK), ('yl', YELLOW)]:
    a(f'<marker id="arr-{mid}" viewBox="0 0 10 10" refX="9" refY="5" '
      f'markerWidth="9" markerHeight="9" orient="auto">'
      f'<path d="M0,0 L10,5 L0,10 z" fill="{color}"/></marker>')
a('</defs>')

# Title
a(f'<text x="40" y="40" fill="{TEXT}" font-size="22" font-weight="700">HarnessToolKit — Scenario View</text>')
a(f'<text x="40" y="60" fill="{TEXT_DIM}" font-size="13">9-stage pipeline sequence  ·  H0 → step-1..9 → H3</text>')

# Legend top-right
a(f'<rect x="900" y="22" width="380" height="44" rx="6" fill="{PANEL}" stroke="{BORDER}"/>')
a(f'<line x1="916" y1="40" x2="950" y2="40" stroke="{ORANGE}" stroke-width="1.6" marker-end="url(#arr-or)"/>')
a(f'<text x="956" y="44" fill="{TEXT}" font-size="11">sync call</text>')
a(f'<line x1="1030" y1="40" x2="1064" y2="40" stroke="{TEXT_DIM}" stroke-width="1.6" stroke-dasharray="5 3" marker-end="url(#arr-dim)"/>')
a(f'<text x="1070" y="44" fill="{TEXT}" font-size="11">return / async</text>')
a(f'<rect x="916" y="52" width="14" height="6" fill="{YELLOW}" opacity="0.4" stroke="{YELLOW}"/>')
a(f'<text x="936" y="58" fill="{TEXT}" font-size="11">human checkpoint H0–H3</text>')

# Lifeline headers
for x, name, color in LL:
    w = 180
    hx = x - w // 2
    a(f'<rect x="{hx}" y="80" width="{w}" height="36" rx="6" fill="{PANEL_LT}" stroke="{color}" stroke-width="1.4"/>')
    a(f'<text x="{x}" y="103" fill="{color}" font-size="12" font-weight="700" text-anchor="middle">{name}</text>')
    # vertical dashed lifeline
    a(f'<line x1="{x}" y1="116" x2="{x}" y2="{VB_H-20}" stroke="{BORDER_S}" stroke-width="1" stroke-dasharray="4 4"/>')


def mid_for(color):
    return {
        CYAN: 'cy', ORANGE: 'or', BLUE: 'bl',
        GREEN: 'gr', PURPLE: 'pu', TEXT_DIM: 'dim',
        PINK: 'pk', YELLOW: 'yl',
    }[color]


def arrow(y, src_idx, tgt_idx, label, color=TEXT_DIM, dashed=False):
    sx = LL[src_idx][0]
    tx = LL[tgt_idx][0]
    dash = 'stroke-dasharray="5 3" ' if dashed else ''
    mid = mid_for(color)
    a(f'<path d="M{sx} {y} L{tx} {y}" stroke="{color}" stroke-width="1.6" fill="none" {dash}marker-end="url(#arr-{mid})"/>')
    mx = (sx + tx) // 2
    # direction-aware label offset (above arrow)
    a(f'<text x="{mx}" y="{y - 7}" fill="{TEXT}" font-size="11" text-anchor="middle">{label}</text>')


def hbar(y, label, color=YELLOW):
    a(f'<rect x="40" y="{y-12}" width="{VB_W-80}" height="24" rx="4" fill="{color}" opacity="0.12" stroke="{color}" stroke-width="1"/>')
    a(f'<text x="56" y="{y+4}" fill="{color}" font-size="11" font-weight="700">{label}</text>')


def phase_label(y, text, color=ORANGE):
    """Vertical phase tag on the far left."""
    a(f'<text x="48" y="{y}" fill="{color}" font-size="11" font-weight="700">{text}</text>')


# ===== H0 =====
hbar(150, 'H0  ·  kickoff — user submits natural-language requirement')

# Step 1
phase_label(190, 'S1')
arrow(190, 0, 1, '"implement feature X"', ORANGE)
arrow(218, 1, 2, 'harness-toolkit step 1', ORANGE)
arrow(246, 2, 3, '/step-1-req-clarify', CYAN)
arrow(274, 3, 4, 'req extract-dimensions · next-question', PURPLE)
arrow(302, 3, 0, 'clarifying questions', TEXT_DIM, dashed=True)
arrow(330, 0, 3, 'answers', CYAN)
arrow(358, 3, 2, 'step 1 --complete  (writes handoff)', CYAN)

# H1
hbar(392, 'H1  ·  requirements locked — dimensions converged, ready for scan')

# Step 2
phase_label(430, 'S2')
arrow(430, 1, 2, 'harness-toolkit step 2', ORANGE)
arrow(458, 2, 3, '/step-2-code-scan', CYAN)
arrow(486, 3, 4, 'graphify inferProjectGraph · diff', PURPLE)
arrow(514, 3, 2, 'step 2 --complete', CYAN)

# Step 3
phase_label(552, 'S3')
arrow(552, 1, 2, 'step 3', ORANGE)
arrow(580, 2, 3, '/step-3-envelope', CYAN)
arrow(608, 3, 4, 'envelope derive (baseline + delta)', PURPLE)
arrow(636, 3, 2, 'step 3 --complete', CYAN)

# Step 4
phase_label(674, 'S4')
arrow(674, 1, 2, 'step 4', ORANGE)
arrow(702, 2, 3, '/step-4-gate-derive', CYAN)
arrow(730, 3, 4, 'gate-engine ast + static analyzers', PURPLE)
arrow(758, 3, 2, 'step 4 --complete', CYAN)

# Step 5
phase_label(796, 'S5')
arrow(796, 1, 2, 'step 5  (task-decomposition)', ORANGE)
arrow(824, 2, 3, '/step-5-slice', CYAN)
arrow(852, 3, 2, 'step 5 --complete', CYAN)

# Step 6 — TDD with Team
phase_label(890, 'S6')
arrow(890, 1, 2, 'step 6  (TDD cycle)', ORANGE)
arrow(918, 2, 3, '/step-6-implement', CYAN)
arrow(946, 3, 5, 'spawn worker · assign task', GREEN)
arrow(974, 5, 3, 'red-green-refactor results', TEXT_DIM, dashed=True)
arrow(1002, 3, 2, 'step 6 --complete', CYAN)

# H2
hbar(1036, 'H2  ·  implementation done — reconcile review gate before baseline')

# Step 7
phase_label(1076, 'S7')
arrow(1076, 1, 2, 'step 7', ORANGE)
arrow(1104, 2, 3, '/step-7-reconcile', CYAN)
arrow(1132, 3, 4, 'reconcile two-phase · narrative', PURPLE)
arrow(1160, 3, 2, 'step 7 --complete', CYAN)

# Step 8
phase_label(1198, 'S8')
arrow(1198, 1, 2, 'step 8', ORANGE)
arrow(1226, 2, 3, '/step-8-baseline', CYAN)
arrow(1254, 3, 4, 'baseline freeze  (frozen_at_sha)', PURPLE)
arrow(1282, 3, 2, 'step 8 --complete', CYAN)

# Step 9
phase_label(1320, 'S9')
arrow(1320, 1, 2, 'step 9', ORANGE)
arrow(1348, 2, 3, '/step-9-release', CYAN)
arrow(1376, 3, 2, 'release-pack --complete', CYAN)

# H3
hbar(1410, 'H3  ·  ship review — delivery accepted, telemetry flushed')

# Footer
a(f'<text x="40" y="{VB_H-12}" fill="{TEXT_MUTE}" font-size="10">Logical/Development/Physical views: see 4plus1-overview.svg  ·  Style 2 · Dark Terminal</text>')

a('</svg>')

with open(OUT, 'w', encoding='utf-8') as f:
    f.write('\n'.join(L))
print(f'WROTE {OUT} ({len(L)} lines)')
