import frappe

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

try:
    # Get Westech workspace
    ws = frappe.get_doc("Workspace", "Westech")
    
    # Create HTML block with template links
    html_block = '''
<div style="border: 2px solid #6f42c1; border-radius: 8px; padding: 15px; margin: 10px 0;">
    <h4 style="margin-top: 0; color: #6f42c1;">📋 Print Format Templates</h4>
    <div style="display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px;">
        <a href="/app/print-format/special-handling-log" target="_blank" 
           style="display: block; padding: 10px; background: #f0f0f0; border-radius: 5px; text-decoration: none; color: #333;">
            <strong>🟣 Purple Sheet</strong><br>
            <small>Special Handling Log</small>
        </a>
        <a href="/app/print-format/r2-special-handling-log" target="_blank" 
           style="display: block; padding: 10px; background: #e8f5e9; border-radius: 5px; text-decoration: none; color: #333;">
            <strong>🟢 Green Sheet</strong><br>
            <small>R2 Special Handling Log</small>
        </a>
        <a href="/app/print-format/pallet-label-4x6" target="_blank" 
           style="display: block; padding: 10px; background: #fff3cd; border-radius: 5px; text-decoration: none; color: #333;">
            <strong>🏷️ Pallet Label</strong><br>
            <small>4x6" with Service Level</small>
        </a>
        <a href="/app/print-format/r2-uw-label" target="_blank" 
           style="display: block; padding: 10px; background: #e3f2fd; border-radius: 5px; text-decoration: none; color: #333;">
            <strong>📦 R2 UW Label</strong><br>
            <small>Controlled Stream</small>
        </a>
    </div>
</div>
'''
    
    # Parse existing content
    import json
    content = json.loads(ws.content) if ws.content else []
    
    # Check if template links section already exists
    existing_idx = None
    for i, block in enumerate(content):
        if block.get('type') == 'custom_block' and 'Print Format Templates' in block.get('data', {}).get('html', ''):
            existing_idx = i
            break
    
    if existing_idx is not None:
        # Update existing block
        content[existing_idx]['data']['html'] = html_block
        print(f"✓ Updated existing template links block")
    else:
        # Add new block after header
        template_block = {
            "type": "custom_block",
            "data": {
                "html": html_block
            }
        }
        # Insert after first header block
        insert_idx = 1
        for i, block in enumerate(content):
            if block.get('type') == 'header':
                insert_idx = i + 1
                break
        content.insert(insert_idx, template_block)
        print(f"✓ Added new template links block")
    
    # Update workspace
    ws.content = json.dumps(content)
    
    # Remove broken shortcuts before saving
    if hasattr(ws, 'shortcuts'):
        ws.shortcuts = [s for s in ws.shortcuts if frappe.db.exists(s.doctype, s.link_to)]
    
    ws.save(ignore_permissions=True)
    frappe.db.commit()
    
    print(f"✓ Workspace 'Westech' updated successfully")
    print(f"✓ View at: https://erp.diagalon.com/app/westech")
    
finally:
    frappe.destroy()
