// packages/core/src/components/form/form.props.ts
import { z } from "zod/v4";
import type { JSX } from "solid-js";
import type { ComponentMeta } from "../../meta";

export const FormRootPropsSchema = z.object({
  disabled: z.boolean().optional().describe("Disable all fields in the form"),
  validateOn: z.enum(["submit", "blur", "change"]).optional().describe("When to validate. Defaults to 'submit'"),
});

export interface FormRootProps extends z.infer<typeof FormRootPropsSchema>, Omit<JSX.FormHTMLAttributes<HTMLFormElement>, "onSubmit"> {
  schema?: z.ZodType;
  onSubmit?: (values: Record<string, unknown>, event: SubmitEvent) => void;
  onValidationError?: (errors: Record<string, string[]>) => void;
  children: JSX.Element;
}

export const FormFieldPropsSchema = z.object({
  name: z.string().describe("Field name matching the schema key"),
});

export interface FormFieldProps extends z.infer<typeof FormFieldPropsSchema> {
  children: JSX.Element;
}

export interface FormLabelProps extends JSX.LabelHTMLAttributes<HTMLLabelElement> { children?: JSX.Element; }
export interface FormControlProps { children: (props: { id: string; name: string; "aria-describedby"?: string; "aria-invalid"?: boolean }) => JSX.Element; }
export interface FormDescriptionProps extends JSX.HTMLAttributes<HTMLParagraphElement> { children?: JSX.Element; }
export interface FormErrorMessageProps extends JSX.HTMLAttributes<HTMLParagraphElement> { children?: JSX.Element | ((errors: string[]) => JSX.Element); }
export interface FormSubmitProps extends JSX.ButtonHTMLAttributes<HTMLButtonElement> { children?: JSX.Element; }

export const FormMeta: ComponentMeta = {
  name: "Form",
  description: "Form with Zod v4 schema validation, field-level error display, and accessible aria linking",
  parts: ["Root", "Field", "Label", "Control", "Description", "ErrorMessage", "Submit"] as const,
  requiredParts: ["Root", "Field"] as const,
} as const;
