import { z } from "zod";
import { interests } from "./contact-schema";
export const employmentOptions = [
  "Employed full-time",
  "Employed part-time",
  "Self-employed",
  "Not currently employed",
  "Retired",
] as const;
export const applicationFieldSchema = z
  .object({
    name: z
      .string()
      .trim()
      .min(1, "Enter your full name.")
      .max(200)
      .refine(
        (v) => !/[\r\n\u0000-\u001f]/.test(v),
        "Enter a single-line name.",
      ),
    email: z.email("Enter a valid email address.").max(254),
    phone: z
      .string()
      .trim()
      .regex(/^\+?[\d ()-]{7,30}$/, "Enter a valid contact number.")
      .refine((v) => {
        const n = v.replace(/\D/g, "").length;
        return n >= 7 && n <= 15;
      }, "Enter 7 to 15 digits."),
    interest: z.enum(interests, "Choose a loan type."),
    amount: z
      .string()
      .regex(
        /^\d{1,8}(\.\d{1,2})?$/,
        "Enter an amount with up to two decimal places.",
      )
      .refine(
        (v) => Number(v) > 0 && Number(v) <= 10000000,
        "Enter an amount between $0.01 and $10,000,000.",
      ),
    employment: z.enum(employmentOptions, "Choose your employment status."),
    monthlyIncome: z
      .string()
      .regex(
        /^\d{1,8}(\.\d{1,2})?$/,
        "Enter your monthly income, or 0 if none.",
      )
      .refine(
        (v) => Number(v) <= 10000000,
        "Enter an income up to $10,000,000.",
      ),
  })
  .strict();
export const applicationDraftFieldsSchema = z
  .object({
    name: z.string().max(200),
    email: z.string().max(254),
    phone: z.string().max(30),
    interest: z.string().max(80),
    amount: z.string().max(12),
    employment: z.string().max(80),
    monthlyIncome: z.string().max(12),
  })
  .strict();
export type ApplicationFields = z.infer<typeof applicationDraftFieldsSchema>;
export const emptyApplication: ApplicationFields = {
  name: "",
  email: "",
  phone: "",
  interest: "",
  amount: "",
  employment: "",
  monthlyIncome: "",
};
export const stepFields = [
  ["name", "email", "phone"],
  ["interest", "amount", "employment", "monthlyIncome"],
] as const;
export const documentKinds = [
  "identity",
  "payslip",
  "cpf",
  "tax",
  "purpose",
] as const;
export const documentLabels = {
  identity: "NRIC / identity document",
  payslip: "Payslip",
  cpf: "CPF statement",
  tax: "Income tax assessment (NOA)",
  purpose: "Proof of loan purpose",
} as const;
export const MAX_DOCUMENT_BYTES = 10 * 1024 * 1024;
export const documentMetadataSchema = z
  .object({
    kind: z.enum(documentKinds),
    name: z.string().min(1).max(200),
    type: z.enum(["application/pdf", "image/jpeg", "image/png"]),
    size: z.number().int().min(1).max(MAX_DOCUMENT_BYTES),
  })
  .strict();
export type ApplicationDocument = z.infer<typeof documentMetadataSchema> & {
  blob: Blob;
};
export function documentError(file: File): string | null {
  if (
    !documentMetadataSchema.safeParse({
      kind: "identity",
      name: file.name,
      type: file.type,
      size: file.size,
    }).success
  )
    return "Choose a PDF, JPG or PNG between 1 byte and 10 MB, with a filename of at most 200 characters.";
  return null;
}
export const staffDocumentKinds = [...documentKinds, "declaration"] as const;
export type StaffDocumentKind = (typeof staffDocumentKinds)[number];
export const staffDocumentLabels: Record<StaffDocumentKind, string> = {
  ...documentLabels,
  declaration: "Signed declaration form",
};
