"use client";

import { useRef } from "react";
import { useTranslation } from "react-i18next";
import { Button } from "@/modules/ui/components/button";
import {
  Dialog,
  DialogBody,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from "@/modules/ui/components/dialog";
import { Input } from "@/modules/ui/components/input";
import { Label } from "@/modules/ui/components/label";
import { focusOnOpen } from "@/modules/ui/lib/focus-on-open";

interface CreateDashboardDialogProps {
  open: boolean;
  onOpenChange: (open: boolean) => void;
  dashboardName: string;
  onDashboardNameChange: (name: string) => void;
  onCreate: () => void;
  isCreating: boolean;
}

export const CreateDashboardDialog = ({
  open,
  onOpenChange,
  dashboardName,
  onDashboardNameChange,
  onCreate,
  isCreating,
}: Readonly<CreateDashboardDialogProps>) => {
  const { t } = useTranslation();
  const nameInputRef = useRef<HTMLInputElement>(null);

  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      <DialogContent width="narrow" onOpenAutoFocus={(event) => focusOnOpen(event, nameInputRef.current)}>
        <DialogHeader>
          <DialogTitle>{t("workspace.analysis.dashboards.create_dashboard")}</DialogTitle>
          <DialogDescription>
            {t("workspace.analysis.dashboards.create_dashboard_description")}
          </DialogDescription>
        </DialogHeader>

        <form
          onSubmit={(e) => {
            e.preventDefault();
            if (dashboardName.trim() && !isCreating) {
              onCreate();
            }
          }}
          className="space-y-4">
          <DialogBody className="space-y-4">
            <div className="space-y-2">
              <Label htmlFor="dashboard-name">{t("workspace.analysis.dashboards.dashboard_name")}</Label>
              <Input
                id="dashboard-name"
                ref={nameInputRef}
                placeholder={t("workspace.analysis.dashboards.dashboard_name_placeholder")}
                value={dashboardName}
                onChange={(e) => onDashboardNameChange(e.target.value)}
              />
            </div>
          </DialogBody>
          <DialogFooter>
            <Button
              type="button"
              variant="secondary"
              onClick={() => onOpenChange(false)}
              disabled={isCreating}>
              {t("common.cancel")}
            </Button>
            <Button type="submit" loading={isCreating} disabled={!dashboardName.trim()}>
              {t("common.create")}
            </Button>
          </DialogFooter>
        </form>
      </DialogContent>
    </Dialog>
  );
};
