export const validators = {
  username: (value: string): { valid: boolean; error?: string } => {
    if (!value || value.length < 3) {
      return { valid: false, error: 'Username must be at least 3 characters' };
    }
    if (value.length > 50) {
      return { valid: false, error: 'Username must not exceed 50 characters' };
    }
    if (!/^[a-zA-Z0-9_]+$/.test(value)) {
      return { valid: false, error: 'Username can only contain letters, numbers, and underscores' };
    }
    return { valid: true };
  },

  email: (value: string): { valid: boolean; error?: string } => {
    if (!value) {
      return { valid: false, error: 'Email is required' };
    }
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    if (!emailRegex.test(value)) {
      return { valid: false, error: 'Please enter a valid email address' };
    }
    if (value.length > 255) {
      return { valid: false, error: 'Email must not exceed 255 characters' };
    }
    return { valid: true };
  },

  password: (value: string): { valid: boolean; error?: string } => {
    if (!value) {
      return { valid: false, error: 'Password is required' };
    }
    if (value.length < 8) {
      return { valid: false, error: 'Password must be at least 8 characters' };
    }
    if (!/[A-Z]/.test(value)) {
      return { valid: false, error: 'Password must contain at least one uppercase letter' };
    }
    if (!/[a-z]/.test(value)) {
      return { valid: false, error: 'Password must contain at least one lowercase letter' };
    }
    if (!/[0-9]/.test(value)) {
      return { valid: false, error: 'Password must contain at least one digit' };
    }
    if (!/[!@#$%^&*(),.?":{}|<>]/.test(value)) {
      return { valid: false, error: 'Password must contain at least one special character' };
    }
    return { valid: true };
  },

  host: (value: string): { valid: boolean; error?: string } => {
    if (!value) {
      return { valid: false, error: 'Host is required' };
    }
    const hostRegex = /^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/;
    if (!hostRegex.test(value)) {
      return { valid: false, error: 'Please enter a valid host name or IP address' };
    }
    return { valid: true };
  },

  port: (value: number): { valid: boolean; error?: string } => {
    if (value < 1 || value > 65535) {
      return { valid: false, error: 'Port must be between 1 and 65535' };
    }
    return { valid: true };
  },

  syncDirection: (value: string): { valid: boolean; error?: string } => {
    const validDirections = ['bidirectional', 'sap_to_plesk', 'plesk_to_sap'];
    if (!validDirections.includes(value)) {
      return { valid: false, error: 'Invalid sync direction' };
    }
    return { valid: true };
  },

  syncInterval: (value: number): { valid: boolean; error?: string } => {
    if (value < 1 || value > 1440) {
      return { valid: false, error: 'Sync interval must be between 1 and 1440 minutes' };
    }
    return { valid: true };
  },

  conflictResolution: (value: string): { valid: boolean; error?: string } => {
    const validResolutions = ['sap_first', 'plesk_first', 'manual', 'timestamp_based'];
    if (!validResolutions.includes(value)) {
      return { valid: false, error: 'Invalid conflict resolution strategy' };
    }
    return { valid: true };
  },

  required: (value: string): { valid: boolean; error?: string } => {
    if (!value || value.trim() === '') {
      return { valid: false, error: 'This field is required' };
    }
    return { valid: true };
  },

  url: (value: string): { valid: boolean; error?: string } => {
    if (!value) {
      return { valid: false, error: 'URL is required' };
    }
    try {
      new URL(value);
      return { valid: true };
    } catch {
      return { valid: false, error: 'Please enter a valid URL' };
    }
  },
};
