import { FormField, Input, Select } from '@cameleer/design-system';
import type { FormState } from '../form-state';
import { JVM_AGGREGATION_OPTIONS, COMPARATOR_OPTIONS } from '../../enums';

export function JvmMetricForm({ form, setForm }: { form: FormState; setForm: (f: FormState) => void }) {
  const c = form.condition as Record<string, unknown>;
  const patch = (p: Record<string, unknown>) =>
    setForm({ ...form, condition: { ...(form.condition as Record<string, unknown>), ...p } as FormState['condition'] });

  return (
    <>
      <FormField label="Metric name">
        <Input
          value={(c.metric as string | undefined) ?? ''}
          onChange={(e) => patch({ metric: e.target.value })}
          placeholder="heap_used_percent"
        />
      </FormField>
      <FormField label="Aggregation">
        <Select
          value={(c.aggregation as string) ?? 'MAX'}
          onChange={(e) => patch({ aggregation: e.target.value })}
          options={JVM_AGGREGATION_OPTIONS}
        />
      </FormField>
      <FormField label="Comparator">
        <Select
          value={(c.comparator as string) ?? 'GT'}
          onChange={(e) => patch({ comparator: e.target.value })}
          options={COMPARATOR_OPTIONS}
        />
      </FormField>
      <FormField label="Threshold">
        <Input
          type="number"
          value={(c.threshold as number | undefined) ?? ''}
          onChange={(e) => patch({ threshold: Number(e.target.value) })}
        />
      </FormField>
      <FormField label="Window (seconds)">
        <Input
          type="number"
          value={(c.windowSeconds as number | undefined) ?? 300}
          onChange={(e) => patch({ windowSeconds: Number(e.target.value) })}
        />
      </FormField>
    </>
  );
}
