'use client';

import { useCallback } from 'react';
import type { Profile } from '../types';
import { SettingsForm, type SettingsSaveHandler } from './settings-ui';

type Props = {
  profile: Profile;
  onSave: SettingsSaveHandler;
};

/**
 * Settings › General › Automation (design: settings-profile.php #tab-automation).
 */
export function GeneralAutomationTab({ profile, onSave }: Props) {
  const zoom = profile.preferences.workflowDefaultZoom ?? 100;

  const save = useCallback<SettingsSaveHandler>(
    (form) => {
      const raw = (
        form.elements.namedItem('workflow_default_zoom') as HTMLInputElement | null
      )?.value
        .replace(/%/g, '')
        .trim();
      const parsed = Number(raw);
      if (!Number.isFinite(parsed) || parsed < 25 || parsed > 200) {
        const field = form.querySelector('.set-automation-zoom');
        field?.classList.add('is-invalid');
        return false;
      }
      form.querySelector('.set-automation-zoom')?.classList.remove('is-invalid');
      return onSave(form);
    },
    [onSave],
  );

  // QA #501: Cancel restores the saved zoom, so the old error must go with it.
  const clearZoomError = useCallback((form: HTMLFormElement) => {
    form.querySelector('.set-automation-zoom')?.classList.remove('is-invalid');
  }, []);

  return (
    <>
      <p className="set-preferences-note">These preferences only apply to you.</p>
      <div className="set-automation-page">
        <h2 className="set-security-title">Automation</h2>
        <p className="set-security-subtitle">Set preferences for automation tools.</p>
        <SettingsForm className="set-automation-form" onSave={save} onReset={clearZoomError}>
          <section className="set-automation-section">
            <h3>Workflows</h3>
            <label className="check set-automation-check">
              <input
                type="checkbox"
                name="workflow_minimap"
                value="1"
                defaultChecked={profile.preferences.workflowMinimap !== false}
              />
              <span className="check__box" aria-hidden="true" />
              <span className="set-automation-check-copy">
                <strong>Display the minimap by default</strong>
                <small>
                  The minimap provides a zoomed-out overview of the entire flow canvas, making it
                  easier to navigate large flows.
                </small>
              </span>
            </label>
            <div className="set-automation-zoom">
              <label className="field__label" htmlFor="workflow-default-zoom">
                Default zoom level
              </label>
              <p className="set-security-description" id="workflow-default-zoom-help">
                The default zoom level for the workflow canvas.
              </p>
              <div className="set-automation-zoom-input">
                <input
                  className="input"
                  id="workflow-default-zoom"
                  name="workflow_default_zoom"
                  type="text"
                  defaultValue={`${zoom}%`}
                  inputMode="decimal"
                  autoComplete="off"
                  spellCheck={false}
                  aria-describedby="workflow-default-zoom-help"
                />
              </div>
              <p className="field__error">Enter a zoom level between 25% and 200%.</p>
            </div>
          </section>
        </SettingsForm>
      </div>
    </>
  );
}
