import frappe
import json

frappe.init(site="erpnext.local")
frappe.connect()

try:
    # Get current workspace content
    current_content = frappe.db.get_value("Workspace", "Westech", "content")
    content = json.loads(current_content) if current_content else []
    
    # Create HTML block with template links
    html_block = '''
<div style="border: 2px solid #6f42c1; border-radius: 8px; padding: 15px; margin: 10px 0; background: #fff;">
    <h4 style="margin-top: 0; color: #6f42c1;">📋 Print Format Templates</h4>
    <div style="display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px;">
        <div style="padding: 10px; background: #f0f0f0; border-radius: 5px;">
            <strong>🟣 Purple Sheet</strong><br>
            <small>Special Handling Log</small><br>
            <code style="background: #ddd; padding: 2px 5px; border-radius: 3px;">special-handling-log</code>
        </div>
        <div style="padding: 10px; background: #e8f5e9; border-radius: 5px;">
            <strong>🟢 Green Sheet</strong><br>
            <small>R2 Special Handling Log</small><br>
            <code style="background: #ddd; padding: 2px 5px; border-radius: 3px;">r2-special-handling-log</code>
        </div>
        <div style="padding: 10px; background: #fff3cd; border-radius: 5px;">
            <strong>🏷️ Pallet Label</strong><br>
            <small>4x6" with Service Level</small><br>
            <code style="background: #ddd; padding: 2px 5px; border-radius: 3px;">pallet-label-4x6</code>
        </div>
        <div style="padding: 10px; background: #e3f2fd; border-radius: 5px;">
            <strong>📦 R2 UW Label</strong><br>
            <small>Controlled Stream</small><br>
            <code style="background: #ddd; padding: 2px 5px; border-radius: 3px;">r2-uw-label</code>
        </div>
    </div>
    <div style="margin-top: 10px; padding: 10px; background: #fff3cd; border-left: 3px solid #ffc107; font-size: 9pt;">
        ⚠️ <strong>Note:</strong> These are HTML templates. To use them, create Print Formats in ERPNext with these names and paste the HTML content from <code>/home/puddintaim/.openclaw/workspace/westech-erp/templates/</code>
    </div>
</div>
'''
    
    # Add new block at the top (after first header)
    template_block = {
        "type": "custom_block",
        "data": {
            "html": html_block
        }
    }
    
    # Find first header and insert after it
    insert_idx = 0
    for i, block in enumerate(content):
        if block.get('type') == 'header':
            insert_idx = i + 1
            break
    
    content.insert(insert_idx, template_block)
    
    # Update content directly via db.set_value (bypasses validation)
    frappe.db.set_value("Workspace", "Westech", "content", json.dumps(content))
    frappe.db.commit()
    
    print(f"✓ Workspace 'Westech' updated successfully")
    print(f"✓ View at: https://erp.diagalon.com/app/westech")
    print(f"\nTemplate files location:")
    print(f"  /home/puddintaim/.openclaw/workspace/westech-erp/templates/")
    
finally:
    frappe.destroy()
