<?php

function validateRegistration($data, $userModel)
{
    $errors = [];

    // First Name validation
    if (empty($data['fname'])) {
        $errors['fname'] = 'First name is required.';
    }
    // Last name validation
    if (empty($data['lname'])) {
        $errors['lname'] = 'Last name is required.';
    }
    // Username validation
    if (empty($data['username'])) {
        $errors['username'] = 'Username is required.';
    }
    //Phone validation
    if (empty($data['phone'])) {
        $errors['phone'] = 'Phone number is required.';
    } elseif (!preg_match('/^0[789][01]\d{8}$/', $data['phone'])) {
        $errors['phone'] = 'Enter a valid Nigerian phone number (e.g., 08012345678).';
    }
    // Date of Birth validation
    if (empty($data['dob'])) {
        $errors['dob'] = 'Date of Birth is required.';
    }
    // Region validation
    if (empty($data['region'])) {
        $errors['region'] = 'Region is required.';
    }
    // State validation
    if (empty($data['state'])) {
        $errors['state'] = 'State is required.';
    }
    // Religion validation
    if (empty($data['religion'])) {
        $errors['religion'] = 'Religion is required.';
    }
    // Gender validation
    if (empty($data['gender'])) {
        $errors['gender'] = 'Your gender is required.';
    }

    // Email validation
    if (empty($data['email'])) {
        $errors['email'] = 'Email is required.';
    } elseif ($userModel->findUserByEmail($data['email'])) {
        $errors['email'] = 'Email is already registered.';
    }

    // Password validation
    if (empty($data['password'])) {
        $errors['password'] = 'Password is required.';
    } elseif (strlen($data['password']) < 6) {
        $errors['password'] = 'Password must be at least 6 characters.';
    } elseif (!preg_match('/[a-z]/', $data['password'])) {
        $errors['password'] = 'Password must include at least one lowercase letter.';
    } elseif (!preg_match('/[A-Z]/', $data['password'])) {
        $errors['password'] = 'Password must include at least one uppercase letter.';
    } elseif (!preg_match('/\d/', $data['password'])) {
        $errors['password'] = 'Password must include at least one number.';
    }

    // Confirm password
    if ($data['confirm_password'] !== $data['password']) {
        $errors['confirm_password'] = 'Passwords do not match.';
    }

    return $errors;
}