import re

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

qr_block = "".join(lines[601:681])
grid_block = "".join(lines[4461:4538])

pm_content = f"""import React from 'react';
import {{ ShoppingBag, Edit3, Trash2, Plus, QrCode, Download, Printer, X }} from 'lucide-react';
import {{ Product }} from '../../types';

{qr_block.replace("const displayName = getBanglaName(product.nameBangla, product.name);", "const displayName = product.nameBangla || product.name;")}

interface ProductManagerProps {{
  products: Product[];
  setEditingItem: (item: any) => void;
  canAccess: (section: string, action?: string) => boolean;
  showToast: (msg: string, type: 'success' | 'error') => void;
  deleteItem: (table: string, id: string) => void;
  getBanglaName: (bn: string | undefined, en: string) => string;
}}

export default function ProductManager({{
  products, setEditingItem, canAccess, showToast, deleteItem, getBanglaName
}}: ProductManagerProps) {{
  return (
    <>
{grid_block}
    </>
  );
}}
"""

with open("resources/js/components/admin/ProductManager.tsx", "w", encoding="utf-8") as f:
    f.write(pm_content)

# Update AdminDashboard.tsx
# Import ProductManager
import_idx = -1
for i, l in enumerate(lines):
    if "import DashboardOverview" in l:
        import_idx = i
        break
        
if import_idx != -1:
    lines.insert(import_idx+1, "import ProductManager from './admin/ProductManager';\n")
    
# Remove QR button
for i in range(len(lines)):
    if "const ProductQRButton" in lines[i]:
        for j in range(i, i+80):
            lines[j] = ""
        break

# Replace Grid
for i in range(len(lines)):
    if "{activeTab === 'products' && canAccess('products') && (" in lines[i]:
        for j in range(i+1, i+78):
            lines[j] = ""
        lines[i] = """              {activeTab === 'products' && canAccess('products') && (
                <ProductManager
                  products={products}
                  setEditingItem={setEditingItem}
                  canAccess={canAccess}
                  showToast={showToast}
                  deleteItem={deleteItem}
                  getBanglaName={getBanglaName}
                />
              )}\n"""
        break

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

print("Done finalizing!")
