import os

svg_content = """<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 900 700">
  <!-- Styles -->
  <style>
    .title { font: bold 22px sans-serif; fill: #1e293b; }
    .label { font: bold 14px sans-serif; fill: #334155; }
    .subtext { font: 12px sans-serif; fill: #64748b; }
    .code { font: 12px monospace; fill: #475569; }
    .node-bg { stroke-width: 2; }
    .arrow { stroke: #94a3b8; stroke-width: 2; fill: none; }
  </style>

  <defs>
    <marker id="head" markerWidth="10" markerHeight="7" refX="9" refY="3.5" orient="auto">
      <polygon points="0 0, 10 3.5, 0 7" fill="#94a3b8" />
    </marker>
  </defs>

  <rect width="900" height="700" fill="#f8fafc" rx="20"/>

  <!-- TITLE -->
  <text x="450" y="50" text-anchor="middle" class="title">Promotion Search Engine Flow 3.0</text>

  <!-- STEP 1: INPUT -->
  <rect x="325" y="90" width="250" height="70" rx="10" fill="#eff6ff" stroke="#3b82f6" class="node-bg"/>
  <text x="450" y="120" text-anchor="middle" class="label">1. Client Request &amp; Input</text>
  <text x="450" y="140" text-anchor="middle" class="subtext">check_date (null or YYYY-MM-DD)</text>

  <!-- STEP 2: DATE ENGINE -->
  <rect x="325" y="200" width="250" height="70" rx="10" fill="#ffffff" stroke="#0ea5e9" class="node-bg"/>
  <text x="450" y="230" text-anchor="middle" class="label">2. Date Normalization</text>
  <text x="450" y="250" text-anchor="middle" class="subtext">Default: datetime.now()</text>

  <!-- STEP 3: SQL LOGIC (The Core) -->
  <rect x="150" y="310" width="600" height="180" rx="15" fill="#fffbeb" stroke="#f59e0b" class="node-bg"/>
  <text x="450" y="340" text-anchor="middle" class="label" fill="#92400e">3. SQL Ranking &amp; Filtering</text>
  
  <!-- Priority boxes inside SQL -->
  <rect x="180" y="360" width="250" height="100" rx="8" fill="#ffffff" stroke="#fbbf24" stroke-dasharray="4"/>
  <text x="305" y="385" text-anchor="middle" class="label">Channel Priority</text>
  <text x="200" y="410" class="subtext">1. Only Online (Web/App)</text>
  <text x="200" y="430" class="subtext">2. Both > 3. Unknown > 4. Offline</text>

  <rect x="470" y="360" width="250" height="100" rx="8" fill="#ffffff" stroke="#fbbf24" stroke-dasharray="4"/>
  <text x="595" y="385" text-anchor="middle" class="label">Time Priority</text>
  <text x="490" y="415" class="code">ORDER BY to_date ASC</text>
  <text x="490" y="435" class="subtext">(Sắp hết hạn hiện lên đầu)</text>

  <!-- STEP 4: OUTPUT BLOCKS -->
  <rect x="325" y="530" width="250" height="70" rx="10" fill="#f0fdf4" stroke="#22c55e" class="node-bg"/>
  <text x="450" y="560" text-anchor="middle" class="label">4. Output Blocks [CTKM N]</text>
  <text x="450" y="580" text-anchor="middle" class="subtext">Formatted for Agent Stylist</text>

  <!-- STEP 5: FINAL RESPONSE -->
  <rect x="325" y="630" width="250" height="40" rx="20" fill="#166534" />
  <text x="450" y="655" text-anchor="middle" class="label" fill="#ffffff">5. Natural AI Response</text>

  <!-- ARROWS -->
  <path d="M450,160 V200" class="arrow" marker-end="url(#head)"/>
  <path d="M450,270 V310" class="arrow" marker-end="url(#head)"/>
  <path d="M450,490 V530" class="arrow" marker-end="url(#head)"/>
  <path d="M450,600 V630" class="arrow" marker-end="url(#head)"/>
</svg>"""

output_path = r'd:\cnf\chatbot-canifa-feedback\backend\static\cookbook\data\31-promotion-search-tool\promotion_final_flow.svg'
with open(output_path, 'w', encoding='utf-8') as f:
    f.write(svg_content)
print(f"Final SVG generated at {output_path}")
