/**
 * Form Builder Plugin Types
 *
 * Key distinction from CMS Plugin:
 * - CMS uses developer-defined Zod schemas in code
 * - Form Builder allows non-technical admins to build forms via drag-and-drop UI
 * - Forms are serialized to/from JSON Schema for storage
 */
/**
 * Form stored in the database
 */
type Form = {
    id: string;
    /** Display name for the form */
    name: string;
    /** URL-friendly slug - unique identifier for public access */
    slug: string;
    /** Optional description for admin UI */
    description?: string;
    /** JSON Schema stored as string (includes steps, fieldType, stepGroup, etc.) */
    schema: string;
    /** Optional custom success message after submission */
    successMessage?: string;
    /** Optional redirect URL after submission */
    redirectUrl?: string;
    /** Form status: active, inactive, archived */
    status: "active" | "inactive" | "archived";
    /** User who created the form */
    createdBy?: string;
    createdAt: Date;
    updatedAt: Date;
};
/**
 * Form submission stored in the database
 */
type FormSubmission = {
    id: string;
    /** Reference to the form */
    formId: string;
    /** Submitted data as JSON string */
    data: string;
    /** Submission timestamp */
    submittedAt: Date;
    /** Optional user ID if authenticated */
    submittedBy?: string;
    /** Client IP address for rate limiting and spam protection */
    ipAddress?: string;
    /** User agent for analytics */
    userAgent?: string;
};
/**
 * Form submission with its parent form joined
 */
type FormSubmissionWithForm = FormSubmission & {
    form?: Form;
};
/**
 * Serialized form for API responses (dates as strings)
 */
interface SerializedForm extends Omit<Form, "createdAt" | "updatedAt" | "status"> {
    status: string;
    createdAt: string;
    updatedAt: string;
}
/**
 * Serialized form submission for API responses (dates as strings)
 */
interface SerializedFormSubmission extends Omit<FormSubmission, "submittedAt"> {
    submittedAt: string;
}
/**
 * Serialized form submission with parsed data
 */
interface SerializedFormSubmissionWithData<TData = Record<string, unknown>> extends SerializedFormSubmission {
    /** Parsed data object (JSON.parse of data field). Null when the stored JSON is corrupted. */
    parsedData: TData | null;
    /** Joined form */
    form?: SerializedForm;
}
/**
 * Paginated list response for forms
 */
interface PaginatedForms {
    items: SerializedForm[];
    total: number;
    limit: number;
    offset: number;
}
/**
 * Paginated list response for form submissions
 */
interface PaginatedFormSubmissions<TData = Record<string, unknown>> {
    items: SerializedFormSubmissionWithData<TData>[];
    total: number;
    limit: number;
    offset: number;
}
/**
 * Context passed to all backend hooks
 */
interface FormBuilderHookContext {
    /** User ID if authenticated */
    userId?: string;
    /** Request headers */
    headers?: Headers;
    /** Client IP address (for rate limiting) */
    ipAddress?: string;
    /** User agent string */
    userAgent?: string;
}
/**
 * Context for submission-specific hooks
 */
interface SubmissionHookContext extends FormBuilderHookContext {
    /** Form slug being submitted */
    formSlug: string;
    /** Form ID */
    formId: string;
}
/**
 * Input data for creating a form
 */
interface FormInput {
    name: string;
    slug: string;
    description?: string;
    schema: string;
    successMessage?: string;
    redirectUrl?: string;
    status?: "active" | "inactive" | "archived";
    createdBy?: string;
}
/**
 * Input data for updating a form
 */
interface FormUpdate {
    name?: string;
    slug?: string;
    description?: string;
    schema?: string;
    successMessage?: string;
    redirectUrl?: string;
    status?: "active" | "inactive" | "archived";
}
/**
 * Backend hooks for Form Builder plugin
 *
 * All CRUD hooks receive ipAddress and headers for auth/rate limiting.
 * Throw an error from onBefore* hooks to reject the operation (throws 403).
 */
interface FormBuilderBackendHooks {
    /** Called before listing forms. Throw an error to deny access (403). */
    onBeforeListForms?: (ctx: FormBuilderHookContext) => Promise<void> | void;
    /** Called before creating a form. Throw an error to deny, or return modified data. */
    onBeforeFormCreated?: (data: FormInput, ctx: FormBuilderHookContext) => Promise<FormInput | void> | FormInput | void;
    /** Called after a form is created */
    onAfterFormCreated?: (form: SerializedForm, ctx: FormBuilderHookContext) => Promise<void> | void;
    /** Called before getting a form by ID or slug. Throw an error to deny access. */
    onBeforeGetForm?: (idOrSlug: string, ctx: FormBuilderHookContext) => Promise<void> | void;
    /** Called before updating a form. Throw an error to deny, or return modified data. */
    onBeforeFormUpdated?: (id: string, data: FormUpdate, ctx: FormBuilderHookContext) => Promise<FormUpdate | void> | FormUpdate | void;
    /** Called after a form is updated */
    onAfterFormUpdated?: (form: SerializedForm, ctx: FormBuilderHookContext) => Promise<void> | void;
    /** Called before deleting a form. Throw an error to deny. */
    onBeforeFormDeleted?: (id: string, ctx: FormBuilderHookContext) => Promise<void> | void;
    /** Called after a form is deleted */
    onAfterFormDeleted?: (id: string, ctx: FormBuilderHookContext) => Promise<void> | void;
    /**
     * Called before processing a form submission.
     * Use for: spam protection, rate limiting, data validation/enrichment.
     *
     * Throw an error to reject submission (400), or return modified data to continue.
     */
    onBeforeSubmission?: (formSlug: string, data: Record<string, unknown>, ctx: SubmissionHookContext) => Promise<Record<string, unknown> | void> | Record<string, unknown> | void;
    /**
     * Called after a submission is saved.
     * Use for: sending emails, storing in CRM, triggering workflows.
     */
    onAfterSubmission?: (submission: SerializedFormSubmission, form: SerializedForm, ctx: SubmissionHookContext) => Promise<void> | void;
    /** Called when a submission fails */
    onSubmissionError?: (error: Error, formSlug: string, data: Record<string, unknown>, ctx: SubmissionHookContext) => Promise<void> | void;
    /** Called before listing submissions. Throw an error to deny access (403). */
    onBeforeListSubmissions?: (formId: string, ctx: FormBuilderHookContext) => Promise<void> | void;
    /** Called before getting a submission. Throw an error to deny access. */
    onBeforeGetSubmission?: (submissionId: string, ctx: FormBuilderHookContext) => Promise<void> | void;
    /** Called before deleting a submission. Throw an error to deny. */
    onBeforeSubmissionDeleted?: (submissionId: string, ctx: FormBuilderHookContext) => Promise<void> | void;
    /** Called after a submission is deleted */
    onAfterSubmissionDeleted?: (submissionId: string, ctx: FormBuilderHookContext) => Promise<void> | void;
    /** Called on any error */
    onError?: (error: Error, operation: "list" | "get" | "create" | "update" | "delete" | "submit" | "listSubmissions" | "getSubmission" | "deleteSubmission", ctx: FormBuilderHookContext) => Promise<void> | void;
}
/**
 * Configuration for the Form Builder backend plugin
 */
interface FormBuilderBackendConfig {
    /** Optional hooks for customizing behavior */
    hooks?: FormBuilderBackendHooks;
}

export type { Form as F, PaginatedForms as P, SerializedForm as S, SerializedFormSubmission as a, SerializedFormSubmissionWithData as b, PaginatedFormSubmissions as c, FormSubmission as d, FormSubmissionWithForm as e, FormBuilderBackendConfig as f };
