/**
 * AssignActionDialog — dialog for assigning an action to a fail2ban jail.
 *
 * Fetches the list of active jails for the dropdown, then calls
 * ``POST /api/config/jails/{jail_name}/action`` on confirmation.
 */

import { useCallback, useEffect, useState } from "react";
import {
  Button,
  Checkbox,
  Dialog,
  DialogActions,
  DialogBody,
  DialogContent,
  DialogSurface,
  DialogTitle,
  Field,
  MessageBar,
  MessageBarBody,
  Select,
  Spinner,
  Text,
  tokens,
} from "@fluentui/react-components";
import { useJails } from "../../hooks/useJails";
import type { AssignActionRequest } from "../../types/config";
import { ApiError } from "../../api/client";

// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------

export interface AssignActionDialogProps {
  /**
   * The action base name to assign (e.g. ``"iptables"``).  Set to ``null``
   * when the dialog is closed.
   */
  actionName: string | null;
  /** Whether the dialog is currently open. */
  open: boolean;
  /** Called when the dialog should close without taking action. */
  onClose: () => void;
  /** Called after the action has been successfully assigned. */
  onAssigned: () => void;
  /** Assigns the action to a running jail. */
  onAssign: (jailName: string, payload: AssignActionRequest, reload: boolean) => Promise<void>;
}

// ---------------------------------------------------------------------------
// Component
// ---------------------------------------------------------------------------

/**
 * Confirmation dialog for assigning an action to a jail.
 *
 * Fetches running jails from the API for the jail selector.  The user can
 * optionally request a fail2ban reload immediately after the assignment is
 * written.
 *
 * @param props - Component props.
 * @returns JSX element.
 */
export function AssignActionDialog({
  actionName,
  open,
  onClose,
  onAssigned,
  onAssign,
}: AssignActionDialogProps): React.JSX.Element {
  const { jails, loading: jailsLoading, error: jailsError } = useJails();
  const [selectedJail, setSelectedJail] = useState("");
  const [reload, setReload] = useState(false);
  const [submitting, setSubmitting] = useState(false);
  const [error, setError] = useState<string | null>(null);

  const activeJails = jails.filter((j) => j.enabled);

  useEffect(() => {
    if (!open) return;
    setError(null);
    setSelectedJail("");
    setReload(false);
  }, [open]);

  useEffect(() => {
    if (jailsError && open) {
      setError(jailsError);
    }
  }, [jailsError, open]);

  const handleClose = useCallback((): void => {
    if (submitting) return;
    setError(null);
    onClose();
  }, [submitting, onClose]);

  const handleConfirm = useCallback((): void => {
    if (!actionName || !selectedJail || submitting) return;

    const req: AssignActionRequest = { action_name: actionName };
    setSubmitting(true);
    setError(null);

    onAssign(selectedJail, req, reload)
      .then(() => {
        onAssigned();
      })
      .catch((err: unknown) => {
        setError(
          err instanceof ApiError ? err.message : "Failed to assign action.",
        );
      })
      .finally(() => {
        setSubmitting(false);
      });
  }, [actionName, selectedJail, reload, submitting, onAssigned, onAssign]);

  const canConfirm = selectedJail !== "" && !submitting && !jailsLoading;

  return (
    <Dialog open={open} onOpenChange={(_e, data) => { if (!data.open) handleClose(); }}>
      <DialogSurface>
        <DialogBody>
          <DialogTitle>Assign Action to Jail</DialogTitle>
          <DialogContent>
            {actionName !== null && (
              <Text
                as="p"
                size={300}
                style={{ marginBottom: tokens.spacingVerticalM }}
              >
                Assign action{" "}
                <Text weight="semibold" style={{ fontFamily: "monospace" }}>
                  {actionName}
                </Text>{" "}
                to a jail.  This appends the action to the jail&apos;s{" "}
                <code>action</code> list in its <code>.local</code> override file.
              </Text>
            )}

            {error !== null && (
              <MessageBar
                intent="error"
                style={{ marginBottom: tokens.spacingVerticalS }}
              >
                <MessageBarBody>{error}</MessageBarBody>
              </MessageBar>
            )}

            <Field
              label="Target jail"
              required
              hint="Only currently enabled jails are listed."
            >
              {jailsLoading ? (
                <Spinner size="extra-small" label="Loading jails…" />
              ) : (
                <Select
                  value={selectedJail}
                  onChange={(_e, d) => { setSelectedJail(d.value); }}
                  aria-label="Target jail"
                >
                  <option value="" disabled>
                    — select a jail —
                  </option>
                  {activeJails.map((j) => (
                    <option key={j.name} value={j.name}>
                      {j.name}
                    </option>
                  ))}
                </Select>
              )}
            </Field>

            <Checkbox
              label="Reload fail2ban after assigning"
              checked={reload}
              onChange={(_e, d) => { setReload(Boolean(d.checked)); }}
              style={{ marginTop: tokens.spacingVerticalS }}
            />
          </DialogContent>
          <DialogActions>
            <Button
              appearance="secondary"
              onClick={handleClose}
              disabled={submitting}
            >
              Cancel
            </Button>
            <Button
              appearance="primary"
              onClick={handleConfirm}
              disabled={!canConfirm}
              icon={submitting ? <Spinner size="extra-small" /> : undefined}
            >
              {submitting ? "Assigning…" : "Assign"}
            </Button>
          </DialogActions>
        </DialogBody>
      </DialogSurface>
    </Dialog>
  );
}
