import { z } from "zod";

//#region  Service Project
export const projectSchema = z.object({
  name: z.string().min(1, "Name is required"),
  shortName: z.string().min(1, "Short name is required"),
  clientId: z.string().min(1, { message: "Client is required" }),
  services: z
    .array(z.string())
    .min(1, { message: "At least one service required" }),
  address: z.string().min(1, "Address is required"),
  assignedDate: z.string().min(1, { message: "Date is required" }),
  statusId: z.string().min(1, "Status is required"),
  contactName: z.string().min(1, "Contact name is required"),
  contactPhone: z
    .string()
    .min(7, "Invalid phone number")
    .max(15, "Phone number too long"),
  contactEmail: z.string().email("Invalid email address"),
});

export const defaultProjectValues = {
  name: "",
  shortName: "",
  clientId: "",
  services: [],
  address: "",
  assignedDate: "",
  statusId: "",
  contactName: "",
  contactPhone: "",
  contactEmail: "",
};

//#endregion

//#region JobSchema

export const TagSchema = z.object({
  name: z.string().min(1, "Tag name is required"),
  isActive: z.boolean().default(true),
});

export const jobSchema = z.object({
  title: z.string().min(1, "Title is required"),
  description: z.string().min(1, "Description is required"),
  projectId: z.string().min(1, "Project is required"),

  assignees: z.array(z.string()).nonempty("At least one assignee is required"),

  startDate: z.string(),
  dueDate: z.string(),

  tags: z.array(TagSchema).optional().default([]),
});

const MAX_FILE_SIZE = 5 * 1024 * 1024; // 5MB
const ALLOWED_TYPES = [
  "application/pdf",
  "image/png",
  "image/jpg",
  "image/jpeg",
];

export const JobCommentSchema = z.object({
  comment: z.string().min(1, { message: "Please leave comment" }),
  attachments: z
    .array(
      z.object({
        fileName: z.string().min(1),
        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),
        description: z.string().optional(),
        isActive: z.boolean().default(true),
      })
    )
    .optional()
    .default([]),
});

export const defaultJobValue = {
  title: "",
  description: "",
  projectId: "",
  assignees: [],
  startDate: null,
  dueDate: null,
  tags: [],
};

//#endregion
