import * as z from "zod";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { FormProps } from "./types";

export const formSchema = z.object({
    buildID: z.string({ error: "Build ID is required" }),
    userTypeID: z.string({ error: "User Type ID is required" }),
    partID: z.string({ error: "Part ID is required" }),
    companyID: z.string({ error: "Company ID is required" }),
    personID: z.string({ error: "Person ID is required" }),
    expiryStarts: z.string().optional(),
    expiryEnds: z.string().optional(),
});

export type FormValues = z.infer<typeof formSchema>;

export function createForm({ buildID, userTypeID, partID, companyID, personID }: FormProps) {
    return useForm<FormValues>({
        resolver: zodResolver(formSchema),
        defaultValues: {
            buildID: buildID || "",
            userTypeID: userTypeID || "",
            partID: partID || "",
            companyID: companyID || "",
            personID: personID || "",
            expiryStarts: "",
            expiryEnds: ""
        }
    });
}
