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

# Extract marketing tab
m_block, m_s, m_e = get_block("          {activeTab === 'marketing' && (", ["                  {activeTab === 'landing-editor'"])
if m_block:
    m_content = "import React from 'react';\n"
    m_content += "import { Gift, Plus, Trash2 } from 'lucide-react';\n\n"
    m_content += "interface MarketingManagerProps {\n"
    m_content += "  settings: any;\n"
    m_content += "  updateSettings: (updates: any) => void;\n"
    m_content += "  isSaving: boolean;\n"
    m_content += "  saveSiteSettings: (updates?: any) => Promise<void>;\n"
    m_content += "}\n\n"
    m_content += "export default function MarketingManager({ settings, updateSettings, isSaving, saveSiteSettings }: MarketingManagerProps) {\n"
    m_content += "  return (\n"
    m_content += "    <>\n"
    
    clean_m = "".join(m_block).replace("          {activeTab === 'marketing' && (", "").rstrip()
    if clean_m.endswith(")}"):
        clean_m = clean_m[:-2]
        
    m_content += clean_m + "\n"
    m_content += "    </>\n"
    m_content += "  );\n}\n"
    
    with open("resources/js/components/admin/MarketingManager.tsx", "w", encoding="utf-8") as f:
        f.write(m_content)
    print("MarketingManager.tsx created successfully!")
    
    # Replace in AdminDashboard.tsx
    for i in range(len(lines)):
        if "          {activeTab === 'marketing' && (" in lines[i]:
            for j in range(m_s, m_e):
                lines[j] = ""
            lines[i] = """          {activeTab === 'marketing' && (
            <MarketingManager
              settings={settings}
              updateSettings={updateSettings}
              isSaving={isSaving}
              saveSiteSettings={saveSiteSettings}
            />
          )}\n"""
            break

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

print("AdminDashboard.tsx updated successfully!")
