import * as Yup from 'yup';

function createTouchFields<T>(fields: (keyof T)[]): Record<keyof T, boolean> {
    const touchFields: Record<keyof T, boolean> = {} as Record<keyof T, boolean>;
  
    fields.forEach((field) => {
      touchFields[field] = true;
    });
  
    return touchFields;
}

export interface SigninFormValues {
    email: string;
    password: string;
}
export const SigninValidationSchema = Yup.object<SigninFormValues>().shape({
    email: Yup.string().email('Invalid email format').required('Username is required'),
    password: Yup.string().min(8, 'Password must be at least 8 characters long').required('Password is required'),
});

export const SigninFrontValidationSchema = Yup.object<SigninFormValues>().shape({
  email: Yup.string().email('Invalid email format').required('Username or email is required'),
  password: Yup.string().required('Password is required'),
});
const signinFields: (keyof SigninFormValues)[] = ['email', 'password'];
export const SigninTouchFields = createTouchFields<SigninFormValues>(signinFields);

export interface ForgotPasswordinFormValues {
    email: string;
}

export const ForgotPasswordValidationSchema = Yup.object<ForgotPasswordinFormValues>().shape({
    email: Yup.string().required('Email or Username is required'),
});

const ForgotPasswordFields: (keyof ForgotPasswordinFormValues)[] = ['email'];
export const ForgotPasswordTouchFields = createTouchFields<ForgotPasswordinFormValues>(ForgotPasswordFields);

export interface ForgotRecoverPasswordFormValues {
  identify: string;            
  identify_confirm: string;    
  un_pe_pm: string;            
}

export const ForgotRecoverPasswordValidationSchema = Yup.object<ForgotPasswordinFormValues>().shape({
    un_pe_pm: Yup.string().required('Email or Username is required'),
    identify: Yup.string().min(8, 'Password must be at least 8 characters long').required('Password is required'),
    identify_confirm: Yup.string().min(8, 'Password must be at least 8 characters long').required('Password is required').oneOf([Yup.ref("identify")], "Passwords must match"),
});

const ForgotRecoverPasswordFields: (keyof ForgotRecoverPasswordFormValues)[] = ['un_pe_pm', 'identify', 'identify_confirm'];
export const ForgotRecoverPasswordTouchFields = createTouchFields<ForgotRecoverPasswordFormValues>(ForgotRecoverPasswordFields);

export interface CommonFormValues {
    title: string;
    desc: string;
    id? : number;
}
export interface RoomTypeFormValues extends CommonFormValues{}; 
export const RoomTypeValidationSchema = Yup.object<RoomTypeFormValues>().shape({
    title: Yup.string().required('Title is required'),
    desc:Yup.string()
    .max(128, "Description must be at most 128 characters.")
});
const roomTypeFields: (keyof RoomTypeFormValues)[] = ['title', 'desc'];
export const RoomTypeTouchFields = createTouchFields<RoomTypeFormValues>(roomTypeFields);


export interface FeaturesFormValues extends CommonFormValues{};
export const FeaturesValidationSchema = Yup.object<FeaturesFormValues>().shape({
    title: Yup.string().required('Title is required'),
    desc: Yup.string()
    .max(128, "Description must be at most 128 characters.")
});
const featuresFields: (keyof FeaturesFormValues)[] = ['title', 'desc'];
export const FeaturesTouchFields = createTouchFields<FeaturesFormValues>(featuresFields);

export interface ArchitectureStyleFormValues extends CommonFormValues{};
export const ArchitectureStyleValidationSchema = Yup.object<ArchitectureStyleFormValues>().shape({
    title: Yup.string().required('Title is required'),
    desc: Yup.string()
    .max(128, "Description must be at most 128 characters.")
});
const architectureStyleFields: (keyof ArchitectureStyleFormValues)[] = ['title', 'desc'];
export const ArchitectureStyleTouchFields = createTouchFields<ArchitectureStyleFormValues>(architectureStyleFields);

export interface HomeSectionFormValues extends CommonFormValues{};
export const HomeSectionValidationSchema = Yup.object<HomeSectionFormValues>().shape({
    title: Yup.string().required('Title is required'),
   /*  desc: Yup.string()
    .max(128, "Description must be at most 128 characters.")
    .required("Description is required."), */
});
const homeSectionFields: (keyof HomeSectionFormValues)[] = ['title', 'desc'];
export const HomeSectionTouchFields = createTouchFields<HomeSectionFormValues>(homeSectionFields);

export interface InspirationFormValues{
    id?: number;
    housesec_id : string;
    file_image:string;
    cover:string;
    housesec_name : string;
    is_cover:boolean;
};
export const InspirationValidationSchema = Yup.object<InspirationFormValues>().shape({
    housesec_id: Yup.string().required('Please select house section'),
    file_image: Yup.string().required('File image is required'),
    cover: Yup.string(),
    is_cover: Yup.string(),
});
const inspirationFields: (keyof InspirationFormValues)[] = ['housesec_id', 'file_image', 'cover'];
export const InpirationSectionTouchFields = createTouchFields<InspirationFormValues>(inspirationFields);

export interface PastWorkGalleryFormValues{
    id?: number;
    housesec_id : string;
    file_image:string;
    cover:string;
    housesec_name : string;
    is_cover:boolean;
};
export const PastWorkGalleryValidationSchema = Yup.object<PastWorkGalleryFormValues>().shape({
    housesec_id: Yup.string().required('Please select house section'),
    file_image: Yup.string().required('File Image is required'),
    cover: Yup.string(),
    is_cover: Yup.string(),
});
const PastWorkGalleryFields: (keyof PastWorkGalleryFormValues)[] = ['housesec_id', 'file_image', 'cover'];
export const PastWorkGalleryTouchFields = createTouchFields<PastWorkGalleryFormValues>(PastWorkGalleryFields);


export interface floorPlanImageAttchFormValues{
  id?: number;
  file_image: any | string |string[];
  cover:string;
  FimgID?:number|undefined
};
export const FoorplanAttchImgValidationSchema = Yup.object<floorPlanImageAttchFormValues>().shape({
  file_image: Yup.string().required('File Image is required'),
});
const floorplanImgFields: (keyof PastWorkGalleryFormValues)[] = ['file_image'];
export const floorplanattchTouchFields = createTouchFields<PastWorkGalleryFormValues>(floorplanImgFields);

export interface FloorPlanFormValues {
  id?: number;
  title: string;
  short_desc: string;
  number: string;
  bed: number | string;
  bath: number | string;
  sqft: number | string;
  garage: number | string;
  garage_type:number | string;
  dimensions: string;
  front_porch: string;
  back_porch: string;
  room_type_id: string | string[];
  feature_id: string | string[];
  architecture_style_id: string | string[];
  room_type_name:string;
  feature_name:string;
  architecture_style_name:string;
  file_image:string,
  file_pdf:string,

}
export const FloorPlanValidationSchema = Yup.object<FloorPlanFormValues>().shape({
  title: Yup.string().required('Title is required'),
 /*  short_desc: Yup.string()
  .max(128, "Description must be at most 128 characters.")
  .required("Description is required."), */
  number: Yup.string().required('Plan number is required'),
  bed: Yup.string().matches(/^\d{1,3}(\.\d{1,3})?$/, "Number must be up to 6 digits and 3 decimal places")
  .required('Bed is required'),
  bath: Yup.string().matches(/^\d{1,3}(\.\d{1,3})?$/, "Number must be up to 6 digits and 3 decimal places").required('Bath is required'),
  sqft: Yup.number().integer('Please enter a valid integer').required('Square Feet is required'),
  garage_type: Yup.number()
  .integer('Please enter a valid integer')
  .required('Garage Type is required'),
 /*  dimensions: Yup.string().required('Dimensions is required'), */
 /*  front_porch: Yup.string().required('Front porch is required'),
  back_porch: Yup.string().required('Back porch is required'), */
 /*  room_type_id:  Yup.mixed()
  .test('is-valid', 'Please select a rooms', (value) => {
    if (Array.isArray(value)) {
      return value.length > 0;
    }
    return typeof value === 'string' && value !== '';
  }).required('Please select rooms'), */
/*   feature_id: Yup.mixed()
  .test('is-valid', 'Please select a feature', (value) => {
    if (Array.isArray(value)) {
      return value.length > 0;
    }
    return typeof value === 'string' && value !== '';
  })
  .required('Please select a feature'), */
 /*  architecture_style_id:Yup.mixed()
  .test('is-valid', 'Please select a architectural style', (value) => {
    if (Array.isArray(value)) {
      return value.length > 0;
    }
    return typeof value === 'string' && value !== '';
  }).required('Please select architectural style'), */
  /* file_pdf: Yup.string().required('PDF is required'), */
});
  
  const FloorPlanFields: (keyof FloorPlanFormValues)[] = ['title', 'short_desc', 'number', 'bed', 'bath', 'sqft', 'garage', 'dimensions', 'front_porch', 'back_porch', 'room_type_id', 'feature_id', 'architecture_style_id', 'file_pdf', 'garage_type' ];
  
  export const FloorPlanTouchFields = createTouchFields<FloorPlanFormValues>(FloorPlanFields);
  
  export interface CustomerFormValues{
    id?:number;
    fname: string;
    lname:string;
    primary_email : string;
    primary_mobile: number |string;
    address:string;
    city_id: number | string ;
    country_id:number | string;
    signup_source:string
    state_id:number | string 
};
export const CustomerValidationSchema = Yup.object<CustomerFormValues>().shape({
    fname: Yup.string().required('First Name is required'),
    lname: Yup.string().required('Last Name is required'),
    primary_email:  Yup.string()
    .email('Invalid Email Format') 
    .required('Email is required'),
    primary_mobile: Yup.string()
    .required('Phone Number is required')
    .matches(/^\D*?(\d\D*?){10}$/, 'Please enter a valid Phone Number (10 digits).'),
    address: Yup.string().required('Address is required'),
    country_id: Yup.string().required('Please select country'),
    city_id: Yup.string().required('Please select city'),
    state_id:Yup.string().required('Please select state'),
});
const customerFields: (keyof CustomerFormValues)[] = ['fname','lname', 'primary_email','primary_mobile','address','country_id','city_id','state_id'];
export const customerTouchFields = createTouchFields<CustomerFormValues>(customerFields);


export interface GeneralSettingsFormValues{
  id: number;
  website_banner: string,
  key:string
};
export const GeneralSettingsValidationSchema = Yup.object<GeneralSettingsFormValues>().shape({
  website_banner: Yup.string().required('Website Banner is required'),
});
const GeneralSettingsFields: (keyof GeneralSettingsFormValues)[] = ['website_banner'];
export const GeneralSettingsTouchFields = createTouchFields<GeneralSettingsFormValues>(GeneralSettingsFields);