import z from "zod";
import validator from "validator";
import type { ReactNode } from "react";

export interface FormData {
  firstName: string;
  lastName: string;
  email: string;
  phoneNumber: string;
  tickets: number;
  companyName: string;
  cmpFirstName: string;
  cpmLastName: string;
  cpmEmail: string;
  cpmPhoneNumber: string;
  street: string;
  postalCode: string;
  paymentMethod: string;
}

export interface Message {
  type: "primary" | "neutral" | "danger" | "success" | "warning";
  headline: string;
  text: string;
}

export type TextFieldProps = {
  label: string;
  type?: "text" | "email" | "tel" | "number";
  required?: boolean;
  errors: string[];
  onBlur: () => void;
  onChange: (value: string) => void;
  value: string | number | null | undefined;
  slotProps?: { input?: Record<string, unknown> };
  afterInput?: ReactNode;
};

export const createFormSchema = (
  t: (key: string) => string,
  invoice: boolean,
) =>
  z.object({
    firstName: z.string().min(1, t("name-error")),
    lastName: z.string().min(1, t("name-error")),
    email: z.email(t("email-error")),
    phoneNumber: z.string(t("phone-error")).refine(validator.isMobilePhone),
    tickets: z.number(t("ticket-error")).min(1),
    chocolates: z.boolean(t("chocolates-error")),
    companyName: invoice
      ? z.string().min(1, t("name-error"))
      : z.string().optional(),
    cmpFirstName: invoice
      ? z.string().min(1, t("name-error"))
      : z.string().optional(),
    cpmLastName: invoice
      ? z.string().min(1, t("name-error"))
      : z.string().optional(),
    cpmEmail: invoice ? z.email(t("email-error")) : z.string().optional(),
    cpmPhoneNumber: invoice
      ? z.string(t("phone-error")).refine(validator.isMobilePhone)
      : z.string().optional(),
    street: invoice
      ? z.string().min(1, t("name-error"))
      : z.string().optional(),
    postalCode: invoice
      ? z.string().min(1, t("name-error"))
      : z.string().optional(),
    paymentMethod: z.string().min(1, t("name-error")),
  });
