import 'package:flutter/cupertino.dart';

class Validators {
  static String? requiredField(String? value, String label) {
    if (value == null || value.trim().isEmpty) {
      return "Required";
      // return "$label is required";
    }
    return null;
  }

  static String? requiredVechileNum(String? value, String label) {
    if (value == null || value.trim().isEmpty) {
      return "Required";
    }

    // Convert to uppercase and remove extra spaces
    value = value.trim().toUpperCase();

    // Allow pattern like: TN 01 AB 1234 or TN01AB1234
    final RegExp vehiclePattern = RegExp(
      r'^[A-Z]{2}\s?\d{1,2}\s?[A-Z]{1,2}\s?\d{1,4}$',
    );

    if (!vehiclePattern.hasMatch(value)) {
      return "Enter valid.no.(e.g.TN01AB1234 or TN 01 AB 1234)";
    }

    return null; // ✅ valid
  }

  static String? email(String? value, String label) {
    if (value == null || value.trim().isEmpty) {
      return "Required";
    }

    final emailRegex = r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$';

    if (!RegExp(emailRegex).hasMatch(value)) {
      return "Invalid $label"; // 👈 will show: "Invalid Email"
    }
    return null;
  }

  static String? passwordSignIn(String? value, String label) {
    if (value == null || value.trim().isEmpty) {
      return "Required";
    }

    if (value.length < 6) {
      return "Enter at least 6 characters";
    }

    List<String> errors = [];
    if (!RegExp(r'[0-9]').hasMatch(value)) errors.add("number");
    if (!RegExp(r'[A-Z]').hasMatch(value)) errors.add("uppercase letter");
    if (!RegExp(r'[a-z]').hasMatch(value)) errors.add("lowercase letter");
    if (!RegExp(r'[!@#\$&*~%^.,?]').hasMatch(value))
      errors.add("special character");

    if (errors.isNotEmpty) {
      return "$label must include ${errors.join(", ")}";
    }

    return null;
  }

  static String? passwordlogIn(String? value, String label) {
    if (value == null || value.trim().isEmpty) {
      return "Required";
    }

    if (value.length < 6) {
      return "Enter at least 6 characters";
    }
    return null;
  }

  static String? minLength(String? value, String label, int length) {
    if (value == null || value.trim().isEmpty) {
      return "$label is required";
    }
    if (value.length < length) {
      return "$label must be at least $length characters";
    }
    return null;
  }

  static String? number(String? value, String label) {
    if (value == null || value.trim().isEmpty) {
      return "Required";
    }
    if (!RegExp(r'^[0-9]+$').hasMatch(value)) {
      return "$label must contain only numbers";
    }
    return null;
  }

  static String? doubleNumber(String? value, String label) {
    if (value == null || value.trim().isEmpty) {
      return "Required";
    }
    // allow integers or decimals with optional fraction part
    if (!RegExp(r'^[0-9]+(\.[0-9]+)?$').hasMatch(value.trim())) {
      return "$label must be a valid number";
    }
    return null;
  }

  static String? phone(String? value, String label) {
    if (value == null || value.trim().isEmpty) {
      return "Required";
    }

    if (!RegExp(r'^[0-9]{10}$').hasMatch(value!.trim())) {
      return "Enter a valid 10-digit phone number";
    }
    return null;
  }

  static String? nonReqemail(String? value, String label) {
    // ✅ Skip validation if empty
    if (value == null || value.trim().isEmpty) return null;

    final emailRegex = r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$';
    if (!RegExp(emailRegex).hasMatch(value.trim())) {
      return "Invalid $label";
    }
    return null;
  }

  static String? nonReqphone(String? value, String label) {
    // ✅ Skip validation if empty
    if (value == null || value.trim().isEmpty) return null;

    if (!RegExp(r'^[0-9]{10}$').hasMatch(value.trim())) {
      return "Enter a valid 10-digit phone number";
    }
    return null;
  }
}
