/**
 * Copyright (c) 2023-present Plane Software, Inc. and contributors
 * SPDX-License-Identifier: AGPL-3.0-only
 * See the LICENSE file for details.
 */

import { useState } from "react";
import { useTranslation } from "@plane/i18n";
import { Button } from "@plane/propel/button";
import { EModalPosition, EModalWidth, ModalCore, TextArea } from "@plane/ui";

type Props = {
  isOpen: boolean;
  isSubmitting?: boolean;
  onClose: () => void;
  onSubmit: (comment: string) => Promise<void>;
};

export function ExternalContourDeclineModal(props: Props) {
  const { isOpen, isSubmitting = false, onClose, onSubmit } = props;
  const { t } = useTranslation();
  const [comment, setComment] = useState("");

  const handleClose = () => {
    if (isSubmitting) return;
    setComment("");
    onClose();
  };

  const handleSubmit = async () => {
    if (!comment.trim() || isSubmitting) return;
    await onSubmit(comment.trim());
    setComment("");
  };

  return (
    <ModalCore
      isOpen={isOpen}
      handleClose={handleClose}
      position={EModalPosition.CENTER}
      width={EModalWidth.LG}
      className="nodedc-glass-modal rounded-[1.75rem]"
    >
      <div className="space-y-4 p-6">
        <div className="space-y-1">
          <h3 className="text-18 font-semibold text-primary">{t("external_contours_page.decline_modal.title")}</h3>
          <p className="text-13 text-secondary">{t("external_contours_page.decline_modal.description")}</p>
        </div>

        <TextArea
          value={comment}
          onChange={(e) => setComment(e.target.value)}
          placeholder={t("external_contours_page.decline_modal.placeholder")}
          rows={5}
          disabled={isSubmitting}
          autoFocus
          className="nodedc-modal-input min-h-[8rem] resize-none"
        />

        <div className="flex justify-end gap-2">
          <Button variant="secondary" onClick={handleClose} disabled={isSubmitting} className="nodedc-modal-secondary-button">
            {t("cancel")}
          </Button>
          <Button variant="primary" onClick={handleSubmit} disabled={isSubmitting || !comment.trim()} className="nodedc-modal-primary-button">
            {t("external_contours_page.decline_modal.submit")}
          </Button>
        </div>
      </div>
    </ModalCore>
  );
}
