lines = []
lines.append('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 960 600">')
lines.append('  <style>')
lines.append('    text { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; }')
lines.append('  </style>')
lines.append('  <defs>')
lines.append('    <marker id="arrow-claude" markerWidth="8" markerHeight="8" refX="7" refY="4" orient="auto">')
lines.append('      <polygon points="0 0, 8 4, 0 8" fill="#5a5a5a"/>')
lines.append('    </marker>')
lines.append('    <filter id="shadow-soft">')
lines.append('      <feDropShadow dx="0" dy="2" stdDeviation="4" flood-color="#000000" flood-opacity="0.05"/>')
lines.append('    </filter>')
lines.append('  </defs>')
lines.append('  <rect width="960" height="600" fill="#f8f6f3"/>')
lines.append('  <text x="480" y="40" text-anchor="middle" fill="#1a1a1a" font-size="22" font-weight="700">Database Connection &amp; Routing Logic</text>')

# Layer labels
lines.append('  <text x="40" y="100" fill="#6a6a6a" font-size="14" font-weight="600">Application</text>')
lines.append('  <text x="40" y="240" fill="#6a6a6a" font-size="14" font-weight="600">Router</text>')
lines.append('  <text x="40" y="420" fill="#6a6a6a" font-size="14" font-weight="600">Storage Layer</text>')

# App Layer
lines.append('  <rect x="380" y="70" width="200" height="80" rx="12" fill="#a8c5e6" stroke="#4a4a4a" stroke-width="2.5" filter="url(#shadow-soft)"/>')
lines.append('  <text x="480" y="105" text-anchor="middle" fill="#1a1a1a" font-size="16" font-weight="600">App / Helper</text>')
lines.append('  <text x="480" y="125" text-anchor="middle" fill="#6a6a6a" font-size="13">FastAPI or Class Helpers</text>')

# Logic/Router Layer
lines.append('  <rect x="380" y="200" width="200" height="80" rx="12" fill="#9dd4c7" stroke="#4a4a4a" stroke-width="2.5" filter="url(#shadow-soft)"/>')
lines.append('  <text x="480" y="235" text-anchor="middle" fill="#1a1a1a" font-size="16" font-weight="600">Connection Switch</text>')
lines.append('  <text x="480" y="255" text-anchor="middle" fill="#6a6a6a" font-size="13">Check USE_LOCAL_SQLITE</text>')

# Local Mode
lines.append('  <rect x="180" y="380" width="200" height="100" rx="12" fill="#f4e4c1" stroke="#4a4a4a" stroke-width="2.5" filter="url(#shadow-soft)"/>')
lines.append('  <text x="280" y="415" text-anchor="middle" fill="#1a1a1a" font-size="16" font-weight="600">SQLite Mock</text>')
lines.append('  <text x="280" y="435" text-anchor="middle" fill="#6a6a6a" font-size="13">SQL Translation</text>')
lines.append('  <text x="280" y="455" text-anchor="middle" fill="#6a6a6a" font-size="13">sqlite_mock.py</text>')

# Production Mode
lines.append('  <rect x="580" y="380" width="200" height="100" rx="12" fill="#e8e6e3" stroke="#4a4a4a" stroke-width="2.5" filter="url(#shadow-soft)"/>')
lines.append('  <text x="680" y="415" text-anchor="middle" fill="#1a1a1a" font-size="16" font-weight="600">Postgres Pool</text>')
lines.append('  <text x="680" y="435" text-anchor="middle" fill="#6a6a6a" font-size="13">psycopg_pool / asyncpg</text>')
lines.append('  <text x="680" y="455" text-anchor="middle" fill="#6a6a6a" font-size="13">CanifaDbPool</text>')

# Arrows
lines.append('  <line x1="480" y1="150" x2="480" y2="200" stroke="#5a5a5a" stroke-width="2" marker-end="url(#arrow-claude)"/>')
lines.append('  <text x="490" y="180" fill="#5a5a5a" font-size="13">Connect/Query</text>')

lines.append('  <path d="M 380 240 L 280 240 L 280 380" fill="none" stroke="#5a5a5a" stroke-width="2" marker-end="url(#arrow-claude)"/>')
lines.append('  <text x="200" y="310" fill="#5a5a5a" font-size="13">True (Local)</text>')

lines.append('  <path d="M 580 240 L 680 240 L 680 380" fill="none" stroke="#5a5a5a" stroke-width="2" marker-end="url(#arrow-claude)"/>')
lines.append('  <text x="700" y="310" fill="#5a5a5a" font-size="13">False (Prod)</text>')

lines.append('</svg>')

output_path = "backend/static/cookbook/data/01b-database/diagram.svg"
with open(output_path, "w", encoding="utf-8") as f:
    f.write("\n".join(lines))
print(f"SVG generated at {output_path}")
