import { z } from "zod";
import { PROJECT_STATUS } from "../../utils/constants";

//#region  Service Project
export const projectSchema = z.object({
  name: z.string().trim().min(1, "Name is required"),

  shortName: z.string().trim().min(1, "Short name is required"),

  clientId: z.string().trim().min(1, { message: "Client is required" }),

  services: z
    .array(z.string())
    .min(1, { message: "At least one service required" }),

  address: z.string().trim().min(1, "Address is required"),

  assignedDate: z.string().trim().min(1, { message: "Date is required" }),

  statusId: z.string().trim().min(1, "Status is required"),

  contactName: z
    .string()
    .trim()
    .min(1, "Contact name is required")
    .regex(/^[A-Za-z\s]+$/, "Contact name can contain only letters"),

  contactPhone: z
    .string()
    .trim()
    .regex(/^\d+$/, "Phone number must contain only digits")
    .min(7, "Invalid phone number")
    .max(15, "Phone number too long"),

  contactEmail: z.string().trim().email("Invalid email address"),
});

export const defaultProjectValues = {
  name: "",
  shortName: "",
  clientId: "",
  services: [],
  address: "",
  assignedDate: "",
  statusId: PROJECT_STATUS.find((s) => s.label === "Active")?.id,
  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()).optional(),

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

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

  statusId: z.string().optional().nullable(),
});

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: [],
  statusId: null,
  startDate: null,
  dueDate: null,
  tags: [],
};

//#endregion
