import re

with open("resources/js/components/AdminDashboard.tsx", "r", encoding="utf-8") as f:
    lines = f.readlines()

def get_block(start_str, end_strs):
    start_idx = -1
    for i, l in enumerate(lines):
        if start_str in l:
            start_idx = i
            break
    if start_idx == -1: return [], -1, -1
    
    end_idx = -1
    for i in range(start_idx+1, len(lines)):
        for e in end_strs:
            if e in lines[i]:
                end_idx = i
                return lines[start_idx:end_idx], start_idx, end_idx
    return [], -1, -1

r_block, r_s, r_e = get_block("              {activeTab === 'reviews' && canAccess('testimonials') && (", ["            </div>"])

if r_block:
    out = "import React from 'react';\n"
    out += "import { Plus, Edit3, Trash2, Star } from 'lucide-react';\n\n"
    out += "export default function TestimonialsManager({ testimonials, setEditingItem, canAccess, deleteItem, showToast }: any) {\n"
    out += "  return (\n"
    
    # Just render the block inside return
    # remove the `{activeTab...` wrapper
    b = "".join(r_block)
    # The wrapper starts at r_block[0] and ends at the last `}`. Actually let's just return the div
    # Wait, the block:
    # {activeTab === 'reviews' && canAccess('testimonials') && (
    #   <div className="space-y-12">
    #     ...
    #   </div>
    # )}
    out += "    <>\n"
    for l in r_block[1:-1]:
        out += l
    out += "    </>\n  );\n}\n"
    
    with open("resources/js/components/admin/TestimonialsManager.tsx", "w", encoding="utf-8") as f:
        f.write(out)
        
    for i in range(r_s, r_e):
        lines[i] = ""
    lines[r_s] = """              {activeTab === 'reviews' && canAccess('testimonials') && (
                <TestimonialsManager
                  testimonials={testimonials}
                  setEditingItem={setEditingItem}
                  canAccess={canAccess}
                  deleteItem={deleteItem}
                  showToast={showToast}
                />
              )}\n"""
              
    with open("resources/js/components/AdminDashboard.tsx", "w", encoding="utf-8") as f:
        f.writelines(lines)
    print("TestimonialsManager extracted!")
else:
    print("Could not find reviews block")
