import * as Yup from "yup";

// const isValidPostcode = (value) => {
//   if (typeof value !== "string") return false;

//   // Trim leading and trailing spaces
//   value = value.trim();

//   // Check if it contains only letters, numbers, and spaces
//   if (!/^[A-Za-z0-9 ]+$/.test(value)) return false;

//   // Remove spaces and check length (must be 2 to 6 characters)
//   const withoutSpaces = value.replace(/\s/g, "");
//   if (withoutSpaces.length < 2 || withoutSpaces.length > 8) return false;

//   return true;
// };

// const postCode = Yup.string()
//   .trim()
//   .test("test-postcode", "Invalid postcode format", (value) => isValidPostcode(value))
//   .required("Postcode is required");
// const mobile = Yup.string()
// .matches(/^[0-9]{11}$/, "Mobile number must be exactly 11 digits")
// .required("Mobile number is required")

const postCode = Yup.string()
  .trim()
  .matches(/^[A-Za-z0-9\s]+$/, "Postcode must be alphanumeric")
  .min(2, "Postcode must be at least 2 characters")
  .max(8, "Postcode cannot exceed 8 characters")
  .required("Postcode is required");

const mobile = Yup.string()
  .matches(/^[0-9]{11}$/, "Mobile number must be exactly 11 digits")
  .required("Mobile number is required");

// const mileage = Yup.number().min(10,"Mileage must be between 10 and 999999").max(999999,"Mileage must be between 10 and 999999")
const mileage = Yup.number()
  .min(1000, "Mileage must be between 1000 and 500000 miles")
  .max(500000, "Mileage must be between 1000 and 500000 miles")
  .typeError("Mileage must be a numeric value");
// const postCode =  Yup.string().test("test-postcode",isValidPostcode).required("Postcode is required")
export const carSchema = Yup.object({
  vrm: Yup.string()
    .trim()
    .required("Registration number is required")
    .matches(/^[a-zA-Z0-9 ]+$/, "Registration number must be alphanumeric")
    .min(2, "Registration number must be between 2 and 8 characters")
    .max(8, "Registration number must be between 2 and 8 characters"),
  mileage: Yup.number()
    .min(1000, "Mileage must be between 1000 and 500000 miles")
    .max(500000, "Mileage must be between 1000 and 500000 miles")
    .typeError("Mileage must be a numeric value")
});

// export const halfValuationSchema = Yup.object({
//     // vrm: Yup.string().min(5,"Registration number must be 8 characters").max(8,"Registration number must be 8 characters").required("Registration number Required field"),
//     name: Yup.string().trim().required("Name is Required field"),
//     email: Yup.string().email().trim().required("Email is Required field"),
//     mobile: mobile,
//     post_code: postCode,
//     city: Yup.string().trim().required("City is Required field"),
//     additional_information: Yup.string().trim().required("Additional Information is Required field")
// });

export const halfValuationSchema = Yup.object({
  name: Yup.string()
    .trim()
    .matches(/^[A-Za-z\s]+$/, "Only alphabets are allowed")
    .max(20, "Name cannot exceed 20 characters")
    .required("Name is required"),

  email: Yup.string()
    .email("Invalid email format")
    .trim()
    .required("Email is required"),

  mobile: mobile,

  post_code: postCode,

  city: Yup.string()
    .trim()
    .matches(/^[A-Za-z\s]+$/, "Only alphabets are allowed")
    .max(20, "City cannot exceed 20 characters")
    .required("City is required"),

  additional_information: Yup.string()
    .trim()
    .required("Additional Information is required")
});

export const manualEntrySchema = Yup.object({
  name: Yup.string()
    .trim()
    .matches(/^[A-Za-z\s]+$/, "Only alphabets are allowed")
    .max(20, "Name cannot exceed 20 characters")
    .required("Name is required"),
  email: Yup.string().email().trim().required("Email is Required field"),
  vrm: Yup.string()
    .trim()
    .min(5, "Registration number must be 8 characters")
    .max(8, "Registration number must be 8 characters")
    .required("Registration number Required field"),
  // model: Yup.string().trim().required("Model is Required field"),
  model: Yup.string()
    .trim()
    .required("Model is a required field")
    .matches(/^[a-zA-Z0-9 ]+$/, "Model must be alphanumeric")
    .min(2, "Model must be between 2 and 15 characters")
    .max(15, "Model must be between 2 and 15 characters"),
  make: Yup.string().required("Make is Required field"),
  mileage: mileage,
  mobile: mobile,
  post_code: postCode,
  city: Yup.string()
    .trim()
    .matches(/^[A-Za-z\s]+$/, "Only alphabets are allowed")
    .max(20, "City cannot exceed 20 characters")
    .required("City is required"),
  additional_information: Yup.string()
    .trim()
    .required("Additional Information is Required field")
});

export const contactUsSchema = Yup.object({
  first_name: Yup.string()
    .matches(/^[A-Za-z]+$/, "First name must contain only alphabets")
    .min(2, "First name must be at least 2 characters")
    .max(30, "First name cannot exceed 30 characters")
    .trim()
    .required("First name is required"),
  last_name: Yup.string()
    .matches(/^[A-Za-z]+$/, "Last name must contain only alphabets")
    .min(2, "Last name must be at least 2 characters")
    .max(30, "Last name cannot exceed 30 characters")
    .trim()
    .required("Last name is required"),
  captcha: Yup.string().required("Please verify you are not a robot"),

  //   last_name: Yup.string()
  //     .matches(/^[A-Za-z]+$/, "Last name must contain only alphabets")
  //     .min(2, "Last name must be at least 2 characters")
  //     .max(30, "Last name cannot exceed 30 characters")
  //     .required("Last name is required"),

  email: Yup.string()
    .email("Invalid email format")
    .trim()
    .required("Email is required"),

  mobile: mobile,

  message: Yup.string()
    .min(10, "Message must be at least 10 characters")
    .max(500, "Message cannot exceed 500 characters")
    .trim()
    .required("Message is required")
});
