import { useState } from 'react';
import { Button, FormField, Input, useToast } from '@cameleer/design-system';
import { useTestEvaluate } from '../../../api/queries/alertRules';
import { describeApiError } from '../../../api/errors';
import type { FormState } from './form-state';

export function TriggerStep({
  form,
  setForm,
  ruleId,
}: {
  form: FormState;
  setForm: (f: FormState) => void;
  ruleId?: string;
}) {
  const testEvaluate = useTestEvaluate();
  const { toast } = useToast();
  const [lastResult, setLastResult] = useState<string | null>(null);

  // PER_EXCHANGE fires exactly once per exchange (Task 3.3 server validator
  // rejects non-zero reNotifyMinutes / forDurationSeconds for this mode).
  // Gate the two inputs here so users can't type values the server will reject.
  const isPerExchange =
    form.conditionKind === 'EXCHANGE_MATCH' &&
    (form.condition as Record<string, unknown>).fireMode === 'PER_EXCHANGE';

  const onTest = async () => {
    if (!ruleId) {
      toast({ title: 'Save rule first to run test evaluate', variant: 'error' });
      return;
    }
    try {
      const result = await testEvaluate.mutateAsync({ id: ruleId, req: {} });
      setLastResult(JSON.stringify(result, null, 2));
    } catch (e) {
      toast({ title: 'Test-evaluate failed', description: describeApiError(e), variant: 'error' });
    }
  };

  return (
    <div style={{ display: 'grid', gap: 12, maxWidth: 600 }}>
      <FormField label="Evaluation interval (seconds, min 5)">
        <Input
          type="number"
          min={5}
          value={form.evaluationIntervalSeconds}
          onChange={(e) => setForm({ ...form, evaluationIntervalSeconds: Number(e.target.value) })}
        />
      </FormField>
      {!isPerExchange && (
        <FormField label="For-duration before firing (seconds, 0 = fire immediately)">
          <Input
            type="number"
            min={0}
            value={form.forDurationSeconds}
            onChange={(e) => setForm({ ...form, forDurationSeconds: Number(e.target.value) })}
          />
        </FormField>
      )}
      <FormField label="Re-notify cadence (minutes, 0 = notify once)">
        <Input
          type="number"
          min={0}
          value={form.reNotifyMinutes}
          onChange={(e) => setForm({ ...form, reNotifyMinutes: Number(e.target.value) })}
          disabled={isPerExchange}
          title={
            isPerExchange
              ? 'Per-exchange rules fire exactly once per exchange — re-notify does not apply.'
              : undefined
          }
        />
      </FormField>
      <div>
        <Button variant="secondary" onClick={onTest} disabled={testEvaluate.isPending}>
          Test evaluate (uses saved rule)
        </Button>
        {!ruleId && (
          <p style={{ marginTop: 8, fontSize: 12, color: 'var(--text-muted)' }}>
            Save the rule first to enable test-evaluate.
          </p>
        )}
        {lastResult && (
          <pre
            style={{
              marginTop: 12,
              padding: 8,
              background: 'var(--code-bg)',
              borderRadius: 6,
              fontSize: 12,
              maxHeight: 240,
              overflow: 'auto',
            }}
          >
            {lastResult}
          </pre>
        )}
      </div>
    </div>
  );
}
