lines = []
lines.append('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1200 820" width="1200" height="820">')
lines.append('  <style>')
lines.append('    text { font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Helvetica Neue, Arial, sans-serif; }')
lines.append('    .title { font-size: 20px; font-weight: 600; fill: #111827; }')
lines.append('    .subtitle { font-size: 12px; font-weight: 400; fill: #6b7280; }')
lines.append('    .node-label { font-size: 13px; font-weight: 500; fill: #111827; }')
lines.append('    .node-sub { font-size: 11px; font-weight: 400; fill: #6b7280; }')
lines.append('    .type-label { font-size: 10px; font-weight: 500; fill: #9ca3af; letter-spacing: 0.08em; }')
lines.append('    .section-label { font-size: 11px; font-weight: 500; fill: #6b7280; letter-spacing: 0.06em; }')
lines.append('    .arrow-label { font-size: 10px; font-weight: 500; fill: #6b7280; }')
lines.append('    .legend-label { font-size: 11px; fill: #374151; }')
lines.append('    .tier-num { font-size: 11px; font-weight: 600; fill: #374151; }')
lines.append('    .tier-sub { font-size: 10px; fill: #9ca3af; }')
lines.append('  </style>')
lines.append('  <defs>')
lines.append('    <marker id="arr-blue" markerWidth="8" markerHeight="6" refX="7" refY="3" orient="auto">')
lines.append('      <polygon points="0 0, 8 3, 0 6" fill="#3b82f6"/>')
lines.append('    </marker>')
lines.append('    <marker id="arr-green" markerWidth="8" markerHeight="6" refX="7" refY="3" orient="auto">')
lines.append('      <polygon points="0 0, 8 3, 0 6" fill="#059669"/>')
lines.append('    </marker>')
lines.append('    <marker id="arr-gray" markerWidth="8" markerHeight="6" refX="7" refY="3" orient="auto">')
lines.append('      <polygon points="0 0, 8 3, 0 6" fill="#9ca3af"/>')
lines.append('    </marker>')
lines.append('  </defs>')
lines.append('  <rect width="1200" height="820" fill="#ffffff"/>')
lines.append('  <text x="48" y="44" class="title">Lead Search Tool \u2014 Ki\u1ebfn tr\u00fac 7-T\u1ea7ng &amp; T\u1ed1i \u01b0u Token</text>')
lines.append('  <text x="48" y="64" class="subtitle">Canifa AI Stylist \u00b7 Cascading Search Strategy \u00b7 T4/2026</text>')
lines.append('  <rect x="32" y="82" width="1136" height="80" rx="4" fill="none" stroke="#e5e7eb" stroke-width="1" stroke-dasharray="4,3"/>')
lines.append('  <text x="46" y="96" class="section-label">INPUT</text>')
lines.append('  <rect x="60" y="102" width="160" height="48" rx="4" fill="#f9fafb" stroke="#e5e7eb" stroke-width="1"/>')
lines.append('  <text x="140" y="123" class="type-label" text-anchor="middle">AGENT OUTPUT</text>')
lines.append('  <text x="140" y="139" class="node-label" text-anchor="middle">AI Intent</text>')
lines.append('  <rect x="260" y="102" width="140" height="48" rx="4" fill="#f9fafb" stroke="#e5e7eb" stroke-width="1"/>')
lines.append('  <text x="330" y="123" class="type-label" text-anchor="middle">TEXT</text>')
lines.append('  <text x="330" y="139" class="node-label" text-anchor="middle">Keywords</text>')
lines.append('  <rect x="440" y="102" width="140" height="48" rx="4" fill="#f9fafb" stroke="#e5e7eb" stroke-width="1"/>')
lines.append('  <text x="510" y="123" class="type-label" text-anchor="middle">ARRAY</text>')
lines.append('  <text x="510" y="139" class="node-label" text-anchor="middle">Tags</text>')
lines.append('  <rect x="620" y="102" width="300" height="48" rx="4" fill="#f9fafb" stroke="#e5e7eb" stroke-width="1"/>')
lines.append('  <text x="770" y="123" class="type-label" text-anchor="middle">HARD FILTERS</text>')
lines.append('  <text x="770" y="139" class="node-label" text-anchor="middle">Color \u00b7 Size \u00b7 Price \u00b7 Gender \u00b7 Line</text>')
lines.append('  <line x1="220" y1="126" x2="256" y2="126" stroke="#3b82f6" stroke-width="1.5" marker-end="url(#arr-blue)"/>')
lines.append('  <line x1="220" y1="126" x2="436" y2="126" stroke="#3b82f6" stroke-width="1.5" marker-end="url(#arr-blue)"/>')
lines.append('  <line x1="220" y1="126" x2="616" y2="126" stroke="#3b82f6" stroke-width="1.5" marker-end="url(#arr-blue)"/>')
lines.append('  <rect x="32" y="182" width="1136" height="290" rx="4" fill="none" stroke="#e5e7eb" stroke-width="1" stroke-dasharray="4,3"/>')
lines.append('  <text x="46" y="196" class="section-label">CASCADING SEARCH \u2014 STARROCKS</text>')

tier_data = [
    ('T1', 'Keywords', 'NGRAMBF idx', '#dbeafe', '#1d4ed8'),
    ('T2', 'Tags', 'BITMAP idx', '#dbeafe', '#1d4ed8'),
    ('T3', 'Hard Filters', 'Fixed attrs only', '#e0f2fe', '#0369a1'),
    ('T4', 'Drop Gender', 'Bo gender filter', '#fef3c7', '#b45309'),
    ('T5', 'Price x1.5', 'Noi rong gia', '#fce7f3', '#9d174d'),
    ('T6', 'Price x2.0', 'Noi rong hon', '#fce7f3', '#9d174d'),
    ('T7', 'Bo Price', 'Khong gioi han', '#f3f4f6', '#374151'),
]

for i, (num, name, sub, bg, stroke) in enumerate(tier_data):
    x = 48 + i * 158
    y = 208
    lines.append(f'  <rect x="{x}" y="{y}" width="144" height="56" rx="4" fill="{bg}" stroke="{stroke}" stroke-width="1.5"/>')
    lines.append(f'  <text x="{x+12}" y="{y+18}" class="tier-num">{num} // {name}</text>')
    lines.append(f'  <text x="{x+12}" y="{y+34}" class="tier-sub">{sub}</text>')
    if i < 6:
        lines.append(f'  <line x1="{x+144}" y1="{y+28}" x2="{x+155}" y2="{y+28}" stroke="#e5e7eb" stroke-width="1" stroke-dasharray="3,2" marker-end="url(#arr-gray)"/>')

lines.append('  <rect x="200" y="296" width="800" height="48" rx="4" fill="#f0fdf4" stroke="#059669" stroke-width="1"/>')
lines.append('  <text x="600" y="317" class="type-label" text-anchor="middle">RESULT</text>')
lines.append('  <text x="600" y="333" class="node-label" text-anchor="middle">Top 12 rows tu tang co ket qua → deduplicate → top 3 base_codes</text>')

for i in range(7):
    x_mid = 48 + i * 158 + 72
    lines.append(f'  <line x1="{x_mid}" y1="264" x2="{x_mid}" y2="293" stroke="#059669" stroke-width="1" stroke-dasharray="3,2" marker-end="url(#arr-green)"/>')

lines.append('  <rect x="32" y="492" width="1136" height="120" rx="4" fill="none" stroke="#e5e7eb" stroke-width="1" stroke-dasharray="4,3"/>')
lines.append('  <text x="46" y="506" class="section-label">ENRICHMENT</text>')
lines.append('  <rect x="60" y="516" width="220" height="72" rx="4" fill="#f9fafb" stroke="#e5e7eb" stroke-width="1"/>')
lines.append('  <text x="170" y="535" class="type-label" text-anchor="middle">EXTERNAL API</text>')
lines.append('  <text x="170" y="551" class="node-label" text-anchor="middle">Canifa Stock API</text>')
lines.append('  <text x="170" y="567" class="node-sub" text-anchor="middle">canifa.com/v1/middleware/...</text>')
lines.append('  <text x="170" y="580" class="node-sub" text-anchor="middle">Loc con hang \u00b7 Size \u00b7 Qty</text>')
lines.append('  <rect x="340" y="516" width="200" height="72" rx="4" fill="#f9fafb" stroke="#e5e7eb" stroke-width="1"/>')
lines.append('  <text x="440" y="535" class="type-label" text-anchor="middle">LOCAL DB</text>')
lines.append('  <text x="440" y="551" class="node-label" text-anchor="middle">SQLite 123.db</text>')
lines.append('  <text x="440" y="567" class="node-sub" text-anchor="middle">ai_description</text>')
lines.append('  <text x="440" y="580" class="node-sub" text-anchor="middle">ai_matches (phoi do)</text>')
lines.append('  <line x1="600" y1="344" x2="600" y2="490" stroke="#3b82f6" stroke-width="1.5" marker-end="url(#arr-blue)"/>')
lines.append('  <line x1="560" y1="492" x2="282" y2="514" stroke="#3b82f6" stroke-width="1.5" marker-end="url(#arr-blue)"/>')
lines.append('  <line x1="600" y1="492" x2="442" y2="514" stroke="#3b82f6" stroke-width="1.5" marker-end="url(#arr-blue)"/>')
lines.append('  <rect x="32" y="632" width="1136" height="160" rx="4" fill="none" stroke="#e5e7eb" stroke-width="1" stroke-dasharray="4,3"/>')
lines.append('  <text x="46" y="646" class="section-label">JSON OUTPUT \u2014 TOKEN-SAVING STRUCTURE</text>')
lines.append('  <rect x="60" y="658" width="240" height="120" rx="4" fill="#f9fafb" stroke="#e5e7eb" stroke-width="1"/>')
lines.append('  <text x="180" y="676" class="type-label" text-anchor="middle">MAIN RESULTS</text>')
lines.append('  <text x="180" y="693" class="node-label" text-anchor="middle">Main Products (Max 3)</text>')
lines.append('  <text x="180" y="710" class="node-sub" text-anchor="middle">sku \u00b7 name \u00b7 price \u00b7 description</text>')
lines.append('  <text x="180" y="724" class="node-sub" text-anchor="middle">stock[ ] \u00b7 in_stock: bool</text>')
lines.append('  <text x="180" y="738" class="node-sub" text-anchor="middle">ai_matches \u00b7 reason</text>')
lines.append('  <text x="180" y="752" class="node-sub" text-anchor="middle">~900 tokens / SP</text>')
lines.append('  <rect x="360" y="658" width="200" height="120" rx="4" fill="#f0fdf4" stroke="#059669" stroke-width="1"/>')
lines.append('  <text x="460" y="676" class="type-label" text-anchor="middle">SUGGEST</text>')
lines.append('  <text x="460" y="693" class="node-label" text-anchor="middle">Suggest Items</text>')
lines.append('  <text x="460" y="710" class="node-sub" text-anchor="middle">Max 2 SP phoi cung</text>')
lines.append('  <text x="460" y="726" class="node-sub" text-anchor="middle">sku \u00b7 name \u00b7 price</text>')
lines.append('  <text x="460" y="742" class="node-sub" text-anchor="middle">~50 tokens / SP</text>')
lines.append('  <rect x="620" y="658" width="200" height="120" rx="4" fill="#fefce8" stroke="#ca8a04" stroke-width="1"/>')
lines.append('  <text x="720" y="676" class="type-label" text-anchor="middle">SIMILAR</text>')
lines.append('  <text x="720" y="693" class="node-label" text-anchor="middle">Similar Items</text>')
lines.append('  <text x="720" y="710" class="node-sub" text-anchor="middle">Max 3 SP thay the</text>')
lines.append('  <text x="720" y="726" class="node-sub" text-anchor="middle">sku \u00b7 name \u00b7 price</text>')
lines.append('  <text x="720" y="742" class="node-sub" text-anchor="middle">~50 tokens / SP</text>')
lines.append('  <rect x="880" y="658" width="260" height="120" rx="4" fill="#fff7ed" stroke="#ea580c" stroke-width="1.5"/>')
lines.append('  <text x="1010" y="676" class="type-label" text-anchor="middle">TOKEN SAVING</text>')
lines.append('  <text x="1010" y="693" class="node-label" text-anchor="middle">Giam 75% Token</text>')
lines.append('  <text x="1010" y="710" class="node-sub" text-anchor="middle">Cu: 15-20 SP full detail</text>')
lines.append('  <text x="1010" y="726" class="node-sub" text-anchor="middle">Moi: 3 SP + 15 SP phu</text>')
lines.append('  <text x="1010" y="742" class="node-sub" text-anchor="middle">Context Window: an toan</text>')
lines.append('  <line x1="170" y1="588" x2="180" y2="656" stroke="#059669" stroke-width="1.5" marker-end="url(#arr-green)"/>')
lines.append('  <line x1="440" y1="588" x2="460" y2="656" stroke="#059669" stroke-width="1.5" marker-end="url(#arr-green)"/>')
lines.append('  <text x="48" y="808" class="legend-label">Legend:</text>')
lines.append('  <line x1="108" y1="803" x2="138" y2="803" stroke="#3b82f6" stroke-width="2" marker-end="url(#arr-blue)"/>')
lines.append('  <text x="144" y="808" class="legend-label">Primary data flow</text>')
lines.append('  <line x1="278" y1="803" x2="308" y2="803" stroke="#059669" stroke-width="2" marker-end="url(#arr-green)"/>')
lines.append('  <text x="314" y="808" class="legend-label">Enrich / Store</text>')
lines.append('  <line x1="430" y1="803" x2="460" y2="803" stroke="#9ca3af" stroke-width="1.5" stroke-dasharray="4,2" marker-end="url(#arr-gray)"/>')
lines.append('  <text x="466" y="808" class="legend-label">Fallback (tang rot)</text>')
lines.append('</svg>')

with open(r'd:\cnf\chatbot-canifa-feedback\backend\static\lead_flow\lead_search_diagram.svg', 'w', encoding='utf-8') as f:
    f.write('\n'.join(lines))

print('SVG generated successfully, lines:', len(lines))
