import { useEffect, 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 { toast } from "sonner";
import { trpc } from "@/lib/trpc";

interface RegisterProjectDialogProps {
  open: boolean;
  onOpenChange: (open: boolean) => void;
}

export function RegisterProjectDialog({
  open,
  onOpenChange,
}: RegisterProjectDialogProps) {
  const [name, setName] = useState("");
  const [url, setUrl] = useState("");
  const [defaultBranch, setDefaultBranch] = useState("main");
  const [error, setError] = useState<string | null>(null);

  const registerMutation = trpc.registerProject.useMutation({
    onSuccess: () => {
      onOpenChange(false);
      toast.success("Project registered");
    },
    onError: (err) => {
      setError(err.message);
    },
  });

  useEffect(() => {
    if (open) {
      setName("");
      setUrl("");
      setDefaultBranch("main");
      setError(null);
    }
  }, [open]);

  function handleSubmit(e: React.FormEvent) {
    e.preventDefault();
    setError(null);
    registerMutation.mutate({
      name: name.trim(),
      url: url.trim(),
      defaultBranch: defaultBranch.trim() || undefined,
    });
  }

  const canSubmit =
    name.trim().length > 0 &&
    url.trim().length > 0 &&
    !registerMutation.isPending;

  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      <DialogContent>
        <DialogHeader>
          <DialogTitle>Register Project</DialogTitle>
          <DialogDescription>
            Register a git repository as a project.
          </DialogDescription>
        </DialogHeader>
        <form onSubmit={handleSubmit} className="space-y-4">
          <div className="space-y-2">
            <Label htmlFor="project-name">Name</Label>
            <Input
              id="project-name"
              placeholder="e.g. my-app"
              value={name}
              onChange={(e) => setName(e.target.value)}
              autoFocus
            />
          </div>
          <div className="space-y-2">
            <Label htmlFor="project-url">Repository URL</Label>
            <Input
              id="project-url"
              placeholder="e.g. https://github.com/org/repo.git"
              value={url}
              onChange={(e) => setUrl(e.target.value)}
            />
          </div>
          <div className="space-y-2">
            <Label htmlFor="default-branch">Default Branch</Label>
            <Input
              id="default-branch"
              placeholder="main"
              value={defaultBranch}
              onChange={(e) => setDefaultBranch(e.target.value)}
            />
          </div>
          {error && <p className="text-sm text-destructive">{error}</p>}
          <DialogFooter>
            <Button
              type="button"
              variant="outline"
              onClick={() => onOpenChange(false)}
            >
              Cancel
            </Button>
            <Button type="submit" disabled={!canSubmit}>
              {registerMutation.isPending ? "Registering..." : "Register"}
            </Button>
          </DialogFooter>
        </form>
      </DialogContent>
    </Dialog>
  );
}
