
import React, { useState, useEffect } from 'react';
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, DialogFooter } from '@/components/ui/dialog';
import { Button } from '@/components/ui/button';
import { ListOrdered } from 'lucide-react';
import TableOfContentsManager from './TableOfContentsManager';

export default function TableOfContentsDialog({ open, onOpenChange, initialData, onSave }) {
  const [currentData, setCurrentData] = useState([]);

  useEffect(() => {
    if (open) {
      setCurrentData(initialData || []);
    }
  }, [open, initialData]);

  const handleSave = () => {
    onSave(currentData);
    onOpenChange(false);
  };

  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      <DialogContent className="sm:max-w-[700px]">
        <DialogHeader>
          <DialogTitle className="flex items-center gap-2">
            <ListOrdered className="w-5 h-5" /> Table of Contents Management
          </DialogTitle>
          <DialogDescription>
            Manually define the table of contents structure for your report. Drag to reorder.
          </DialogDescription>
        </DialogHeader>
        
        <div className="py-4">
            <TableOfContentsManager 
                initialData={initialData} 
                onChange={setCurrentData} 
            />
        </div>

        <DialogFooter>
          <Button variant="outline" onClick={() => onOpenChange(false)}>Cancel</Button>
          <Button onClick={handleSave}>Save Changes</Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  );
}
