'use client';

import { useRouter } from 'next/navigation';
import { useDeleteProject } from '@/lib/hooks/use-projects';
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from '@/components/ui/dialog';
import { Button } from '@/components/ui/button';
import { toast } from 'sonner';

interface ArchiveProjectDialogProps {
  projectId: string;
  projectName: string;
  open: boolean;
  onOpenChange: (open: boolean) => void;
}

export function ArchiveProjectDialog({
  projectId,
  projectName,
  open,
  onOpenChange,
}: ArchiveProjectDialogProps) {
  const router = useRouter();
  const deleteProject = useDeleteProject();

  const handleArchive = async () => {
    try {
      await deleteProject.mutateAsync(projectId);
      toast.success('Project archived successfully');
      router.push('/projects');
    } catch (error) {
      toast.error('Failed to archive project');
      console.error('Archive error:', error);
    }
  };

  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      <DialogContent className="sm:max-w-[425px]">
        <DialogHeader>
          <DialogTitle>Archive Project</DialogTitle>
          <DialogDescription>
            Are you sure you want to archive{' '}
            <strong className="font-semibold">{projectName}</strong>?
          </DialogDescription>
        </DialogHeader>

        <div className="py-4">
          <p className="text-sm text-muted-foreground">
            This action will mark the project as archived, but it can be
            restored later. All associated issues and data will be preserved.
          </p>
        </div>

        <DialogFooter>
          <Button
            type="button"
            variant="outline"
            onClick={() => onOpenChange(false)}
          >
            Cancel
          </Button>
          <Button
            type="button"
            variant="destructive"
            onClick={handleArchive}
            disabled={deleteProject.isPending}
          >
            {deleteProject.isPending ? 'Archiving...' : 'Archive Project'}
          </Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  );
}
