import { z } from "zod";

const MAX_FILE_SIZE = 5 * 1024 * 1024; // 5MB
const ALLOWED_TYPES = [
  "application/pdf",
  "application/doc",
  "application/vnd.openxmlformats-officedocument.wordprocessingml.document", 
  "image/png",
  "image/jpg",
  "image/jpeg",
  "application/vnd.ms-excel", 
  "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", 
];


export const newCollection = z.object({
  title: z.string().trim().min(1, { message: "Title is required" }),
  projectId: z.string().trim().min(1, { message: "Project is required" }),
  invoiceDate: z.string().min(1, { message: "Date is required" }),
  description: z.string().trim().optional(),
  clientSubmitedDate: z.string().min(1, { message: "Date is required" }),
  exceptedPaymentDate: z.string().min(1, { message: "Date is required" }),
  invoiceNumber: z
    .string()
    .trim()
    .min(1, { message: "Invoice is required" })
    .max(17, { message: "Invalid Number" }),
  eInvoiceNumber: z
    .string()
    .trim()
    .min(1, { message: "E-Invoice No is required" }),
  taxAmount: z.coerce
    .number({
      invalid_type_error: "Amount is required and must be a number",
    })
    .min(1, "Amount must be Enter")
    .refine((val) => /^\d+(\.\d{1,2})?$/.test(val.toString()), {
      message: "Amount must have at most 2 decimal places",
    }),
  basicAmount: z.coerce
    .number({
      invalid_type_error: "Amount is required and must be a number",
    })
    .min(1, "Amount must be Enter")
    .refine((val) => /^\d+(\.\d{1,2})?$/.test(val.toString()), {
      message: "Amount must have at most 2 decimal places",
    }),
  attachments: z
    .array(
      z.object({
        fileName: z.string().min(1, { message: "Filename is required" }),
        base64Data: z.string().nullable(),
        contentType: z.string().refine((val) => ALLOWED_TYPES.includes(val), {
          message: "Only PDF, PNG, JPG, or JPEG files are allowed",
        }),
        documentId: z.string().optional(),
        fileSize: z.number().max(MAX_FILE_SIZE, {
          message: "File size must be less than or equal to 5MB",
        }),
        description: z.string().optional(),
        isActive: z.boolean().default(true),
      })
    )
    .nonempty({ message: "At least one file attachment is required" }),
});

export const defaultCollection = {
  projectId: "",
  invoiceNumber: " ",
  eInvoiceNumber: "",
  title: "",
  clientSubmitedDate: null,
  invoiceDate: null,
  exceptedPaymentDate: null,
  taxAmount: "",
  basicAmount: "",
  description: "",
  attachments: [],
};

export const paymentSchema = z.object({
  paymentReceivedDate: z.string().min(1, { message: "Date is required" }),
  transactionId: z.string().min(1, "Transaction ID is required"),
  amount: z.number().min(1, "Amount must be greater than zero"),
  comment:z.string().min(1,{message:"Comment required"}),
  paymentAdjustmentHeadId:z.string().min(1,{message:"Payment Type required"})
});

// Default Value
export const defaultPayment = {
  paymentReceivedDate: null,
  transactionId: "",
  amount: 0,
  comment:"",
  paymentAdjustmentHeadId:""
};


export const CommentSchema = z.object({
  comment:z.string().min(1,{message:"Comment required"})
})
