import re

# Read the backup file
with open("resources/js/components/AdminDashboard.tsx.bak", "r", encoding="utf-8") as f:
    content_bak = f.read()

# Extract the big else block from AdminDashboard.tsx.bak
# It starts at:
#              ) : (
#                <div className="pb-10 animate-fade-in-up max-w-xl mx-auto md:max-w-5xl md:grid md:grid-cols-[340px_1fr] md:gap-6 md:items-start space-y-4 md:space-y-0">
# And ends before the outer `) : null}`
# Let's find this specific `else` block using a regex.

match = re.search(r'(\) : \(\s*<div className="pb-10 animate-fade-in-up max-w-xl mx-auto md:max-w-5xl md:grid md:grid-cols-\[340px_1fr\] md:gap-6 md:items-start space-y-4 md:space-y-0">[\s\S]+?)(\n\s*\)\s*\}[\s\S]*?</div>\s*</div>\s*</div>\s*\)\s*;\s*\}\s*)$', content_bak)

if match:
    else_block = match.group(1)
    
    # Now read the current AdminDashboard.tsx
    with open("resources/js/components/AdminDashboard.tsx", "r", encoding="utf-8") as f:
        content_curr = f.read()
    
    # We want to replace everything from:
    # ) : activeTab === 'products' ? ( ... ) : activeTab === 'reviews' ? ( ... )
    # up to the end.
    
    # Actually, we can just replace the ) : activeTab === 'products' ? ... block we just added
    # with `else_block`
    
    curr_match = re.search(r"\) : activeTab === 'products' \? \([\s\S]+?\) : activeTab === 'reviews' \? \([\s\S]+?\)\s*:\s*\(\s*null\s*\)", content_curr)
    
    if curr_match:
        # Re-inject the reviews block and then the else block!
        # Wait, let's extract the reviews block from the current file first.
        reviews_match = re.search(r"(\) : activeTab === 'reviews' \? \([\s\S]+?)\)\s*:\s*\(\s*null\s*\)", content_curr)
        if reviews_match:
            reviews_block = reviews_match.group(1)
            
            # The replacement will be the reviews block followed by the else block
            new_content = re.sub(r"\) : activeTab === 'products' \? \([\s\S]+?\)\s*:\s*\(\s*null\s*\)", reviews_block + else_block, content_curr)
            
            with open("resources/js/components/AdminDashboard.tsx", "w", encoding="utf-8") as f:
                f.write(new_content)
            print("Successfully patched!")
        else:
            print("Failed to find reviews block in current file.")
    else:
        print("Failed to find the products block to replace in current file.")
else:
    print("Failed to match the else block from backup.")

