<?php
// إضافة REST API Endpoints
add_action('rest_api_init', function () {
    register_rest_route('csm-auth/v1', '/authenticate', [
        'methods' => 'POST',
        'callback' => ['CSM_Auth_Manager_API', 'authenticate_client'],
        'permission_callback' => '__return_true',
    ]);

    register_rest_route('csm-auth/v1', '/validate-token', [
        'methods' => 'GET',
        'callback' => ['CSM_Auth_Manager_API', 'validate_token'],
        'permission_callback' => '__return_true',
    ]);
    register_rest_route('csm-auth/v1', '/revoke-key', [
        'methods' => 'POST',
        'callback' => ['CSM_Auth_Manager_API', 'revoke_key'],
        'permission_callback' => '__return_true',
    ]);

    register_rest_route('csm-auth/v1', '/auth-stats', [
        'methods' => 'GET',
        'callback' => ['CSM_Auth_Manager_API', 'get_auth_statistics'],
        'permission_callback' => '__return_true',
    ]);
    

    register_rest_route('csm-auth/v1', '/auth-test', [
        'methods' => 'GET',
        'callback' => ['CSM_Auth_Manager_API', 'auth_test'],
        'permission_callback' => '__return_true',
    ]);

});

class CSM_Auth_Manager_API {

    public static function authenticate_client($request) {
        $api_key = $request->get_param('api_key');
        $api_secret = $request->get_param('api_secret');

        if (empty($api_key) || empty($api_secret)) {
            return new WP_Error('missing_credentials', 'API key and secret are required', ['status' => 400]);
        }

        $auth = new CSM_Auth_Manager();
        $result = $auth->validate_request($request, [
            'check_token' => false
        ]);

        if (is_wp_error($result)) {
            return $result;
        }

        return $auth->authenticate_client($api_key, $api_secret);
    }

    public static function validate_token($request) {
        $auth = new CSM_Auth_Manager();
        $result = $auth->validate_request($request);

        if (is_wp_error($result)) {
            return $result;
        }

        return rest_ensure_response([
            'status' => 'valid',
        ]);
    }

    public static function revoke_key($request) {
        $api_key = $request->get_param('api_key');
        if (empty($api_key)) {
            return new WP_Error('missing_credentials', 'API key is required', ['status' => 400]);
        }

        $auth = new CSM_Auth_Manager();
        $result = $auth->validate_request($request);
        if (is_wp_error($result)) {
            return $result;
        }
        
        return $auth->revoke_api_key($api_key);
    }

    public static function get_auth_statistics($request) {
        $auth = new CSM_Auth_Manager();
        $result = $auth->validate_request($request);
        if (is_wp_error($result)) {
            return $result;
        }
        return $auth->get_auth_statistics();
    }




    
    public static function auth_test($request) {
        $auth = new CSM_Auth_Manager();
        $result = $auth->validate_request($request);

        if (is_wp_error($result)) {
            return $result;
        }

        $users = get_users(['number' => 5]);
        return rest_ensure_response(['status' => 'ok', 'users' => $users]);
    }

}
