<?php
namespace UM_Elementor\Helpers;

if (!defined('ABSPATH')) exit;

class UM_User_Query_Legacy
{
    public static function build($settings)
    {
        $number = 4;
        if (($settings['enable_pagination'] ?? 'no') === 'yes') {
            $number = (int) ($settings['users_per_page'] ?? 12);
        } else {
            $number = (int) ($settings['user_numbers'] ?? 4);
        }
        
        $args = [
            'fields' => 'ID',
            'count_total' => false,
            'number' => $number,
            'orderby' => $settings['query_user_order_by'] ?? 'display_name',
            'order' => $settings['query_user_order'] ?? 'ASC',
        ];

        if (($settings['query_show_selected_roles'] ?? false) === 'yes' && !empty($settings['query_user_roles'])) {
            $args['role__in'] = $settings['query_user_roles'];
        }

        if (($settings['show_only_approved_users'] ?? 'no') === 'yes') {
            $args['meta_query'][] = [
                'key' => 'account_status',
                'value' => 'approved',
                'compare' => '='
            ];
        }

        if (($settings['query_user_order_by'] ?? '') === 'custom_field' && !empty($settings['user_query_by_meta_key'])) {
            $args['meta_key'] = $settings['user_query_by_meta_key'];
            $args['orderby'] = 'meta_value';
        }

        return $args;
    }

    public static function buildFiltered($meta_key, $settings = [])
    {
        $args = [
            'role__not_in' => ['administrator'],
            'orderby' => 'user_nicename',
            'order' => 'ASC',
            'count_total' => false,
        ];

        if (!empty($meta_key)) {
            $field_type = UM()->Fields()->get_field_type($meta_key);
            if (!empty($field_type)) {
                $args['meta_key'] = $meta_key;
            }
        }

        if (($settings['show_only_approved_users'] ?? 'no') === 'yes') {
            $args['meta_query'][] = [
                'key' => 'account_status',
                'value' => 'approved',
                'compare' => '='
            ];
        }

        if (!empty($settings)) {
            $args = array_merge($args, $settings);
        }

        return $args;
    }

    public static function getUsers($args)
    {
        $members_query = new \WP_User_Query($args);
        return $members_query->get_results();
    }

    public static function getTotalUsers($args)
    {
        $count_args = $args;
        $count_args['number'] = -1;
        $count_args['fields'] = 'ID';
        unset($count_args['paged']);
        
        $members_query = new \WP_User_Query($count_args);
        $total = $members_query->get_total();
        
        if ($total === 0) {
            $simple_args = [
                'fields' => 'ID',
                'number' => -1,
                'role__not_in' => ['administrator']
            ];
            $simple_query = new \WP_User_Query($simple_args);
            $simple_total = $simple_query->get_total();
            
            if ($simple_total > 0) {
                return $simple_total;
            }
        }
        
        return $total;
    }

    public static function shouldDisplayUser($user_id)
    {
        um_fetch_user($user_id);
        
        if (UM()->user()->is_private_profile($user_id)) {
            return false;
        }

        $hide_in_members = get_user_meta($user_id, 'hide_in_members', true);
        if ($hide_in_members && (is_array($hide_in_members) ? $hide_in_members[0] : $hide_in_members) === "Yes") {
            return false;
        }

        return true;
    }

    public static function filterDisplayableUsers($users)
    {
        return array_filter($users, function($user_id) {
            return self::shouldDisplayUser($user_id);
        });
    }

    public static function buildSearch($search_term, $search_roles = [], $additional_settings = [])
    {
        $args = [
            'search' => '*' . esc_attr($search_term) . '*',
            'search_columns' => ['user_login', 'user_nicename'],
            'count_total' => false,
        ];

        if (!empty($search_roles)) {
            $args['role__in'] = $search_roles;
        }

        if (!empty($additional_settings)) {
            $args = array_merge($args, $additional_settings);
        }

        return $args;
    }

    public static function searchUsers($search_term, $search_roles = [], $additional_settings = [])
    {
        $args = self::buildSearch($search_term, $search_roles, $additional_settings);
        return self::getUsers($args);
    }
}
