const fs = require('fs');
let code = fs.readFileSync('src/components/Dashboard.js', 'utf8');

const imports = `import SidebarControls from './dashboard/SidebarControls';
import StatsPanel from './dashboard/StatsPanel';
import ApprovalQueueModal from './dashboard/modals/ApprovalQueueModal';
import UserProvisioningModal from './dashboard/modals/UserProvisioningModal';
import DonationModal from './dashboard/modals/DonationModal';
import HubFormModal from './dashboard/modals/HubFormModal';
import HouseholdFormModal from './dashboard/modals/HouseholdFormModal';
`;

code = code.replace("import { useRouter } from 'next/navigation';", "import { useRouter } from 'next/navigation';\n" + imports);

const leftPanelStart = code.indexOf('{/* Left Panel (Controls) */}');
const leftPanelEnd = code.indexOf('{/* Right Panel (Stats) */}');

if (leftPanelStart > -1 && leftPanelEnd > -1) {
  const newLeftPanel = `{/* Left Panel (Controls) */}
      <div className={\`left-panel glass-panel \${showControls ? '' : 'hidden'}\`} id="controlsPanel">
        <div className="panel-title">
          <span className="title-text"><i className="fas fa-sliders-h"></i> Controls</span>
          <button className="btn-toggle-panel" onClick={() => setShowControls(false)} title="Hide Controls">
            <i className="fas fa-chevron-left"></i>
          </button>
        </div>
        
        <SidebarControls 
          user={user} profile={profile} router={router}
          isEditMode={isEditMode} setIsEditMode={setIsEditMode}
          setShowApprovalModal={setShowApprovalModal} setShowUserModal={setShowUserModal}
          fetchUsers={fetchUsers} setPlacementMode={setPlacementMode}
          setShowToast={setShowToast} filters={filters} setFilters={setFilters}
          hubStyles={hubStyles}
        />
      </div>

      `;
      
  code = code.substring(0, leftPanelStart) + newLeftPanel + code.substring(leftPanelEnd);
}

const rightPanelStart = code.indexOf('{/* Right Panel (Stats) */}');
const rightPanelEnd = code.indexOf('{/* Add Location Floating Button */}');

if (rightPanelStart > -1 && rightPanelEnd > -1) {
  const newRightPanel = `{/* Right Panel (Stats) */}
      <StatsPanel 
        profile={profile} showStats={showStats}
        setShowStats={setShowStats} stats={stats}
      />

      `;
      
  code = code.substring(0, rightPanelStart) + newRightPanel + code.substring(rightPanelEnd);
}

const modalStartIndex = code.indexOf('{/* Hub Modal */}');

if (modalStartIndex > -1) {
  const newModals = `
      <HubFormModal 
        showHubModal={showHubModal} setShowHubModal={setShowHubModal}
        hubFormData={hubFormData} setHubFormData={setHubFormData}
        handleSaveHub={handleSaveHub} isEditMode={isEditMode}
      />

      <HouseholdFormModal 
        showHhModal={showHhModal} setShowHhModal={setShowHhModal}
        hhFormData={hhFormData} setHhFormData={setHhFormData}
        handleSaveHh={handleSaveHh} hubs={hubs}
        imageFile={imageFile} setImageFile={setImageFile}
        isUploading={isUploading} isEditMode={isEditMode}
      />

      <DonationModal 
        showDonationModal={showDonationModal} setShowDonationModal={setShowDonationModal}
        donationFormData={donationFormData} setDonationFormData={setDonationFormData}
        hubs={hubs} isSubmittingDonation={isSubmittingDonation}
        handleSaveDonation={handleSaveDonation} donationsList={donationsList}
      />

      <ApprovalQueueModal 
        showApprovalModal={showApprovalModal} setShowApprovalModal={setShowApprovalModal}
        households={households} selectedPhotoUrl={selectedPhotoUrl}
        setSelectedPhotoUrl={setSelectedPhotoUrl} handleVerifyAction={handleVerifyAction}
      />

      <UserProvisioningModal 
        showUserModal={showUserModal} setShowUserModal={setShowUserModal}
        usersList={usersList} profile={profile}
        userFormData={userFormData} setUserFormData={setUserFormData}
        handleCreateUser={handleCreateUser} isSubmittingUser={isSubmittingUser}
        handleDeleteUser={handleDeleteUser}
      />
    </div>
  );
}`;
  
  code = code.substring(0, modalStartIndex) + newModals;
}

fs.writeFileSync('src/components/Dashboard.js', code);
console.log('Dashboard.js successfully refactored');
