<?php
/**
 * Form Validation Functions
 * Homoeopathy System - Kent's Repertory
 */

// ============================================
// REQUIRED VALIDATION
// ============================================

function isRequired($value) {
    return !empty(trim((string)$value));
}

function validateRequired($fields) {
    $missing = [];
    foreach ($fields as $fieldName => $value) {
        if (!isRequired($value)) {
            $missing[] = $fieldName;
        }
    }
    return $missing;
}

// ============================================
// STRING VALIDATION
// ============================================

function isMinLength($value, $min) {
    return strlen(trim($value)) >= $min;
}

function isMaxLength($value, $max) {
    return strlen(trim($value)) <= $max;
}

function isLengthBetween($value, $min, $max) {
    $length = strlen(trim($value));
    return $length >= $min && $length <= $max;
}

function isAlphanumeric($value, $allowSpaces = false) {
    $pattern = $allowSpaces ? '/^[a-zA-Z0-9\s]+$/' : '/^[a-zA-Z0-9]+$/';
    return preg_match($pattern, $value);
}

// ============================================
// EMAIL VALIDATION
// ============================================

function isValidEmail($email) {
    return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
}

function isEmailDomainValid($email) {
    $domain = substr(strrchr($email, "@"), 1);
    return checkdnsrr($domain, "MX");
}

// ============================================
// MOBILE VALIDATION
// ============================================

function isValidMobileIndian($mobile) {
    return preg_match('/^[6-9]\d{9}$/', $mobile);
}

function isValidMobileInternational($mobile) {
    return preg_match('/^\+[1-9]\d{7,14}$/', $mobile);
}

function isValidMobile($mobile) {
    $mobile = preg_replace('/[-\s\(\)]/', '', $mobile);
    return isValidMobileIndian($mobile) || isValidMobileInternational($mobile);
}

// ============================================
// NUMBER VALIDATION
// ============================================

function isInteger($value, $min = null, $max = null) {
    if (!is_numeric($value) || (int)$value != $value) {
        return false;
    }
    $intValue = (int)$value;
    if ($min !== null && $intValue < $min) return false;
    if ($max !== null && $intValue > $max) return false;
    return true;
}

function isFloat($value, $min = null, $max = null) {
    if (!is_numeric($value)) return false;
    $floatValue = (float)$value;
    if ($min !== null && $floatValue < $min) return false;
    if ($max !== null && $floatValue > $max) return false;
    return true;
}

function isPositiveNumber($value) {
    return is_numeric($value) && $value > 0;
}

function isValidAge($age, $min = 0, $max = 120) {
    return isInteger($age, $min, $max);
}

// ============================================
// DATE VALIDATION
// ============================================

function isValidDate($date, $format = 'Y-m-d') {
    $d = DateTime::createFromFormat($format, $date);
    return $d && $d->format($format) === $date;
}

function isNotFutureDate($date) {
    return strtotime($date) <= time();
}

function isNotPastDate($date) {
    return strtotime($date) >= strtotime(date('Y-m-d'));
}

function compareDates($date1, $date2, $operator = '>') {
    $time1 = strtotime($date1);
    $time2 = strtotime($date2);
    switch ($operator) {
        case '>': return $time1 > $time2;
        case '<': return $time1 < $time2;
        case '>=': return $time1 >= $time2;
        case '<=': return $time1 <= $time2;
        case '=': return $time1 == $time2;
        default: return false;
    }
}

// ============================================
// PASSWORD VALIDATION
// ============================================

function validatePasswordStrength($password, $minLength = 6, $requireUpper = true, $requireLower = true, $requireNumber = true, $requireSpecial = false) {
    $errors = [];
    if (strlen($password) < $minLength) {
        $errors[] = "Password must be at least $minLength characters";
    }
    if ($requireUpper && !preg_match('/[A-Z]/', $password)) {
        $errors[] = "Password must contain at least one uppercase letter";
    }
    if ($requireLower && !preg_match('/[a-z]/', $password)) {
        $errors[] = "Password must contain at least one lowercase letter";
    }
    if ($requireNumber && !preg_match('/\d/', $password)) {
        $errors[] = "Password must contain at least one number";
    }
    if ($requireSpecial && !preg_match('/[!@#$%^&*(),.?":{}|<>]/', $password)) {
        $errors[] = "Password must contain at least one special character";
    }
    return [
        'valid' => empty($errors),
        'errors' => $errors
    ];
}
?>