// /components/AddCollectionDialog.jsx
import React, { useState } from 'react';
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from '@/components/ui/dialog';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import ErrorAlertDialog from '@/components/shared/ErrorAlertDialog.jsx';

const AddCollectionDialog = ({ open, onOpenChange, onSubmit }) => {
  const [collectionName, setCollectionName] = useState('');
  const [error, setError] = useState('');
  const [isSubmitting, setIsSubmitting] = useState(false);
  const [showErrorDialog, setShowErrorDialog] = useState(false);

  const handleSubmit = async (e) => {
    e.preventDefault();

    if (!collectionName.trim()) {
      setError('컬렉션 이름을 입력해주세요');
      return;
    }

    setIsSubmitting(true);
    try {
      const success = await onSubmit(collectionName.trim());
      if (success) {
        setCollectionName('');
        setError('');
        onOpenChange(false);
      } else {
        setError('컬렉션 생성에 실패했습니다');
        setShowErrorDialog(true);
      }
    } catch (err) {
      setError(err.message || '컬렉션 생성에 실패했습니다');
      setShowErrorDialog(true);
    } finally {
      setIsSubmitting(false);
    }
  };

  // Reset form when dialog is closed
  const handleOpenChange = (open) => {
    if (!open) {
      setCollectionName('');
      setError('');
    }
    onOpenChange(open);
  };

  return (
      <>
        <Dialog open={open} onOpenChange={handleOpenChange}>
          <DialogContent>
            <DialogHeader>
              <DialogTitle>새 컬렉션 추가</DialogTitle>
              <DialogDescription>
                새로운 컬렉션의 이름을 입력해주세요
              </DialogDescription>
            </DialogHeader>
            <form onSubmit={handleSubmit}>
              <div className="grid gap-4 py-4">
                <div className="grid gap-2">
                  <Label htmlFor="name">컬렉션 이름</Label>
                  <Input
                      id="name"
                      value={collectionName}
                      onChange={(e) => {
                        setCollectionName(e.target.value);
                        setError('');
                      }}
                      placeholder="컬렉션 이름을 입력하세요"
                  />
                </div>
              </div>
              <DialogFooter>
                <Button
                    type="button"
                    variant="outline"
                    onClick={() => handleOpenChange(false)}
                    disabled={isSubmitting}
                >
                  취소
                </Button>
                <Button
                    type="submit"
                    disabled={isSubmitting}
                >
                  {isSubmitting ? '생성 중...' : '생성'}
                </Button>
              </DialogFooter>
            </form>
          </DialogContent>
        </Dialog>

        <ErrorAlertDialog
            open={showErrorDialog}
            onOpenChange={setShowErrorDialog}
            title="컬렉션 생성 오류"
            description={error}
        />
      </>
  );
};

export default AddCollectionDialog;
