import re

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

qr_start, qr_end = -1, -1
for i, l in enumerate(lines):
    if "const ProductQRButton" in l: qr_start = i
    if "};" in l and i > qr_start and qr_end == -1 and i - qr_start > 50: qr_end = i

qr_block = "".join(lines[qr_start:qr_end+1])

grid_start, grid_end = -1, -1
for i, l in enumerate(lines):
    if "{activeTab === 'products' && canAccess('products') && (" in l: grid_start = i
    if "{activeTab === 'reviews' && canAccess('testimonials') && (" in l: grid_end = i - 1

grid_block = "".join(lines[grid_start+1:grid_end])

# Remove the surrounding `{activeTab === 'products' ...` and the trailing `)}`
grid_block = grid_block.strip()
if grid_block.endswith(")}"):
    grid_block = grid_block[:-2]

pm_content = f"""import React from 'react';
import {{ ShoppingBag, Edit3, Trash2, Plus }} 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)

print("Created ProductManager.tsx")
