<?php 
//ppo rest api

function ppo_rest_logged_in_permission() {
    return is_user_logged_in();
}

function ppo_rest_write_permission($request) {
    if (!is_user_logged_in()) {
        return false;
    }

    $nonce = $request->get_header('X-WP-Nonce');
    if (!$nonce) {
        $nonce = $request->get_param('_wpnonce');
    }

    if (!is_scalar($nonce)) {
        return false;
    }

    return (bool) wp_verify_nonce((string) $nonce, 'wp_rest');
}

// 用户中心获取用户发布文章
add_action('rest_api_init', function () {
    register_rest_route('ppo/v1', '/user-posts', [
        'methods'             => 'GET',
        'callback'            => 'ppo_get_user_posts_html',
        'permission_callback' => '__return_true',
        'args' => [
            'target' => [
                'required'          => false,
                'type'              => 'string',
                'default'           => '#user-content',
            ],
            'push_url_base' => [
                'required'          => false,
                'type'              => 'string',
                'default'           => '',
            ],
        ],
    ]);
});

// 用户中心获取用户发布评论
add_action('rest_api_init', function () {
    register_rest_route('ppo/v1', '/user-comments', [
        'methods'  => 'GET',
        'callback' => 'ppo_get_user_comments',
        'permission_callback' => '__return_true',
        'args' => [
            'user_id' => [
                'required' => true,
                'type'     => 'integer',
            ],
            'page' => [
                'required' => false,
                'type'     => 'integer',
                'default'  => 1,
            ],
        ],
    ]);
});

// 用户中心获取用户发布片刻
add_action('rest_api_init', function () {
    register_rest_route('ppo/v1', '/user-moment', [
        'methods'  => 'GET',
        'callback' => 'ppo_get_user_moment',
        'permission_callback' => '__return_true',
    ]);
});

// 用户中心获取用户发布收藏
add_action('rest_api_init', function () {
    register_rest_route('ppo/v1', '/user-collect', [
        'methods'  => 'GET',
        'callback' => 'ppo_get_user_collect',
        'permission_callback' => '__return_true',
    ]);
});

// 用户中心获取用户加入的圈子
add_action('rest_api_init', function () {
    register_rest_route('ppo/v1', '/user-moments', [
        'methods'  => 'GET',
        'callback' => 'ppo_get_user_moments',
        'permission_callback' => '__return_true',
    ]);
});

// 获取用户粉丝和关注
add_action('rest_api_init', function () {
    register_rest_route('ppo/v1', '/user-follow', [
       'methods'  => 'GET',
        'callback' => 'ppo_get_user_follow',
        'permission_callback' => '__return_true',
    ]);
});

// 获取用户经验明细
add_action('rest_api_init', function () {
    register_rest_route('ppo/v1', '/user-exp', [
        'methods'  => 'GET',
        'callback' => 'ppo_display_user_xp_detail_rest',
        'permission_callback' => 'ppo_rest_logged_in_permission'
    ]);
});

// 获取积分记录
add_action('rest_api_init', function () {
    register_rest_route('ppo/v1', '/user-credit-records', [
        'methods'  => 'GET',
        'callback' => 'ppo_get_user_credit_records',
        'permission_callback' => 'ppo_rest_logged_in_permission'
    ]);
});

// 获取余额记录
add_action('rest_api_init', function () {
    register_rest_route('ppo/v1', '/user-balance-records', [
        'methods'  => 'GET',
        'callback' => 'ppo_get_user_balance_records',
        'permission_callback' => 'ppo_rest_logged_in_permission'
    ]);
});

// 获取用户订单列表
add_action('rest_api_init', function () {
    register_rest_route('ppo/v1', '/user-orders', [
        'methods'  => 'GET',
        'callback' => 'ppo_get_user_orders_api',
        'permission_callback' => 'ppo_rest_logged_in_permission'
    ]);
});

// 取消订单
add_action('rest_api_init', function () {
    register_rest_route('ppo/v1', '/user-order-cancel', [
        'methods'  => 'POST',
        'callback' => 'ppo_cancel_order_api',
        'permission_callback' => 'ppo_rest_write_permission'
    ]);
});

// 删除订单
add_action('rest_api_init', function () {
    register_rest_route('ppo/v1', '/user-order-delete', [
        'methods'  => 'POST',
        'callback' => 'ppo_delete_order_api',
        'permission_callback' => 'ppo_rest_write_permission'
    ]);
});

// 继续支付
add_action('rest_api_init', function () {
    register_rest_route('ppo/v1', '/user-order-repay', [
        'methods'  => 'POST',
        'callback' => 'ppo_repay_order_api',
        'permission_callback' => 'ppo_rest_write_permission'
    ]);
});

// 订单详情
add_action('rest_api_init', function () {
    register_rest_route('ppo/v1', '/user-order-detail', [
        'methods'  => 'POST',
        'callback' => 'ppo_get_order_detail_api',
        'permission_callback' => 'ppo_rest_write_permission'
    ]);
});

// ==================== 片刻 REST API ====================

// 获取片刻列表
add_action('rest_api_init', function () {
    register_rest_route('ppo/v1', '/moments', [
        'methods'  => 'GET',
        'callback' => 'ppo_rest_get_moments',
        'permission_callback' => '__return_true',
        'args' => [
            'page' => [
                'required' => false,
                'type' => 'integer',
                'default' => 1,
            ],
            'per_page' => [
                'required' => false,
                'type' => 'integer',
                'default' => 10,
            ],
            'category' => [
                'required' => false,
                'type' => 'integer',
            ],
            'filter' => [
                'required' => false,
                'type' => 'string',
                'default' => 'new',
            ],
        ],
    ]);
});

// 获取单个片刻
add_action('rest_api_init', function () {
    register_rest_route('ppo/v1', '/moments/(?P<id>\d+)', [
        'methods'  => 'GET',
        'callback' => 'ppo_rest_get_moment',
        'permission_callback' => '__return_true',
    ]);
});

// 创建片刻
add_action('rest_api_init', function () {
    register_rest_route('ppo/v1', '/moments', [
        'methods'  => 'POST',
        'callback' => 'ppo_rest_create_moment',
        'permission_callback' => 'ppo_rest_write_permission',
    ]);
});

// 更新片刻
add_action('rest_api_init', function () {
    register_rest_route('ppo/v1', '/moments/(?P<id>\d+)', [
        'methods'  => 'PUT',
        'callback' => 'ppo_rest_update_moment',
        'permission_callback' => 'ppo_rest_write_permission',
    ]);
});

// 删除片刻
add_action('rest_api_init', function () {
    register_rest_route('ppo/v1', '/moments/(?P<id>\d+)', [
        'methods'  => 'DELETE',
        'callback' => 'ppo_rest_delete_moment',
        'permission_callback' => 'ppo_rest_write_permission',
    ]);
});

// 点赞片刻
add_action('rest_api_init', function () {
    register_rest_route('ppo/v1', '/moments/(?P<id>\d+)/like', [
        'methods'  => 'POST',
        'callback' => 'ppo_rest_like_moment',
        'permission_callback' => 'ppo_rest_write_permission',
    ]);
});

// 取消点赞片刻
add_action('rest_api_init', function () {
    register_rest_route('ppo/v1', '/moments/(?P<id>\d+)/like', [
        'methods'  => 'DELETE',
        'callback' => 'ppo_rest_unlike_moment',
        'permission_callback' => 'ppo_rest_write_permission',
    ]);
});

// 举报片刻
add_action('rest_api_init', function () {
    register_rest_route('ppo/v1', '/moments/(?P<id>\d+)/report', [
        'methods'  => 'POST',
        'callback' => 'ppo_report_moment',
        'permission_callback' => 'ppo_rest_write_permission',
    ]);
});

// 获取用户通知列表
add_action('rest_api_init', function () {
    register_rest_route('ppo/v1', '/moment-notifications', [
        'methods'  => 'GET',
        'callback' => 'ppo_get_notifications_api',
        'permission_callback' => 'ppo_rest_logged_in_permission',
    ]);
});

// 标记通知已读
add_action('rest_api_init', function () {
    register_rest_route('ppo/v1', '/moment-notifications/read', [
        'methods'  => 'POST',
        'callback' => 'ppo_mark_notifications_read_api',
        'permission_callback' => 'ppo_rest_write_permission',
    ]);
});

// 获取未读通知数量
add_action('rest_api_init', function () {
    register_rest_route('ppo/v1', '/moment-notifications/count', [
        'methods'  => 'GET',
        'callback' => 'ppo_get_notification_count_api',
        'permission_callback' => 'ppo_rest_logged_in_permission',
    ]);
});


