<?php
// +----------------------------------------------------------------------
// | onezan-admin 快速开发的管理平台
// +----------------------------------------------------------------------
// | 官方网址：https://www.onezan.cn
// +----------------------------------------------------------------------
// | onezan团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: 543885307@qq.com
// +----------------------------------------------------------------------
namespace app\service\api\user;

use app\repository\user\UserRepository;
use app\service\StorageService;
use app\service\api\BaseApiService;
use app\service\domain\plugins\CorePluginsAccessService;
use app\service\domain\user\CoreUserLevelService;
use app\service\domain\user\CoreUserService;
use think\Request;


/**
 * User Service
 * @package app\service\api\user
 */
class UserService extends BaseApiService
{

    /**
     * Get enabled user level options
     * @return array
     */
    public function levels(): array
    {
        return ['code' => 0, 'data' => $this->core()->listEnabledOptions()];
    }


    /**
     * Get current user profile information
     * @param Request $request
     * @return array
     */
    public function profile(Request $request): array
    {
        $uid = (int)($request->uid ?? 0);
        if ($uid <= 0) {
            return ['code' => 401, 'msg' => '', '_http_status' => 401];
        }
        $user = $this->userRepository()->findById($uid);
        if (!$user || (int)($user->status ?? 0) !== 1) {
            return ['code' => 1, 'msg' => ''];
        }
        return [
            'code' => 0,
            'data' => [
                'id' => (int)$user->id,
                'username' => (string)$user->username,
                'nickname' => (string)($user->nickname ?? ''),
                'avatar' => StorageService::toFullAssetUrl($request, (string)($user->avatar ?? '')),
                'phone' => (string)($user->phone ?? ''),
                'email' => (string)($user->email ?? ''),
                'level_id' => (int)($user->level_id ?? 0),
                'verify_status' => (int)($user->verify_status ?? 0),
                'balance' => (string)($user->balance ?? '0.0000'),
                'vip_snapshot' => $this->pluginsAccess()->snapshot($uid),
            ],
        ];
    }


    /**
     * Update user profile information
     * @param Request $request
     * @param array $params
     * @return array
     */
    public function updateProfile(Request $request, array $params): array
    {
        $uid = (int)($request->uid ?? 0);
        if ($uid <= 0) {
            return ['code' => 401, 'msg' => '', '_http_status' => 401];
        }

        $result = $this->coreUserService()->updateProfile($uid, $params);
        if ((int)($result['code'] ?? 1) !== 0) {
            return $result;
        }

        return $this->profile($request);
    }


    /**
     * Send email verification code
     * @param Request $request
     * @param array $params
     * @return array
     */
    public function sendEmailCode(Request $request, array $params): array
    {
        $uid = (int)($request->uid ?? 0);
        if ($uid <= 0) {
            return ['code' => 401, 'msg' => '', '_http_status' => 401];
        }

        return $this->coreUserService()->sendEmailCode($uid, (string)($params['email'] ?? ''));
    }


    /**
     * Bind email address to user account
     * @param Request $request
     * @param array $params
     * @return array
     */
    public function bindEmail(Request $request, array $params): array
    {
        $uid = (int)($request->uid ?? 0);
        if ($uid <= 0) {
            return ['code' => 401, 'msg' => '', '_http_status' => 401];
        }

        $result = $this->coreUserService()->bindEmail(
            $uid,
            (string)($params['email'] ?? ''),
            (string)($params['code'] ?? '')
        );
        if ((int)($result['code'] ?? 1) !== 0) {
            return $result;
        }

        return $this->profile($request);
    }


    /**
     * Cancel and delete current user account
     * @param Request $request
     * @return array
     */
    public function cancelAccount(Request $request): array
    {
        $uid = (int)($request->uid ?? 0);
        if ($uid <= 0) {
            return ['code' => 401, 'msg' => '', '_http_status' => 401];
        }

        return $this->coreUserService()->cancelAccount($uid);
    }

    /**
     * 获取用户等级核心服务实例
     * @return CoreUserLevelService
     */
    private function core(): CoreUserLevelService
    {
        return $this->app->make(CoreUserLevelService::class);
    }

    /**
     * 获取用户仓储实例
     * @return UserRepository
     */
    private function userRepository(): UserRepository
    {
        return $this->app->make(UserRepository::class);
    }

    /**
     * 获取插件访问服务实例
     * @return CorePluginsAccessService
     */
    private function pluginsAccess(): CorePluginsAccessService
    {
        return $this->app->make(CorePluginsAccessService::class);
    }

    /**
     * 获取用户核心服务实例
     * @return CoreUserService
     */
    private function coreUserService(): CoreUserService
    {
        return $this->app->make(CoreUserService::class);
    }
}