import { useState } from 'react';
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription } from '@/components/ui/dialog';
import { Button } from '@/components/ui/button';

type WebpageDialogProps = {
  open: boolean;
  onOpenChange: (open: boolean) => void;
  onSubmit: (url: string) => void;
};

export function WebpageDialog({ open, onOpenChange, onSubmit }: WebpageDialogProps) {
  const [urlInput, setUrlInput] = useState('');

  const handleSubmit = () => {
    const url = urlInput.trim();
    if (!url) return;
    onSubmit(url);
    setUrlInput('');
    onOpenChange(false);
  };

  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      <DialogContent className="sm:max-w-md z-[700]">
        <DialogHeader>
          <DialogTitle>Attach Webpage</DialogTitle>
          <DialogDescription>Enter a URL to scrape and attach as context.</DialogDescription>
        </DialogHeader>
        <div className="flex gap-2">
          <input
            type="url"
            value={urlInput}
            onChange={(ev) => setUrlInput(ev.target.value)}
            onKeyDown={(ev) => {
              if (ev.key === 'Enter') {
                ev.preventDefault();
                handleSubmit();
              }
            }}
            placeholder="https://example.com"
            className="flex-1 rounded-lg border border-input bg-background px-3 py-2 text-sm text-foreground placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-duck-teal/30 focus:border-duck-teal/50"
            autoFocus
          />
          <Button
            onClick={handleSubmit}
            disabled={!urlInput.trim()}
            className="bg-duck-teal hover:bg-duck-teal/90 cursor-pointer disabled:opacity-40"
          >
            Attach
          </Button>
        </div>
      </DialogContent>
    </Dialog>
  );
}
