<?php

namespace EasyContentManager\Ajax;

use EasyContentManager\Classes\AbstractAjaxHandler;

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}

class User extends AbstractAjaxHandler {
    public function __construct() {
        $this->actions = [
            'get_user_meta' => [ 
                'callback'   => [ $this, 'get_user_meta' ],
                'fields'     => [ 'user_id' => 'integer' ],
                'capability' => 'manage_options',
            ],
        ];
    }

    public function get_user_meta( $payload ) {
        $user_id = isset( $payload['user_id'] ) ? intval( $payload['user_id'] ) : 0;

        if ( $user_id <= 0 ) {
            wp_send_json_error( [ 'message' => 'Invalid user ID.' ] );
        }

        $user_meta = get_user_meta( $user_id );
        
        $meta_data = [];
        foreach ( $user_meta as $key => $value ) {
            if ( strpos( $key, '_' ) === 0 ) {
                continue;
            }
            
            $meta_data[ $key ] = maybe_unserialize( $value[0] );
        }
        
        wp_send_json_success( [ 'meta' => $meta_data ] );
    }
}