<?php

namespace App\Http\Controllers\Client;

use App\Http\Controllers\Controller;
use App\Services\Client\AccountService;
use Illuminate\Http\Request;

/**
 * 账户相关的API接口
 */
class AccountController extends Controller
{
    protected AccountService $service;

    public function __construct(AccountService $service)
    {
        $this->service = $service;
    }

    /**
     * [账户管理] 发送验证码
     * 
     * 向指定手机号发送验证码
     * 
     * @bodyParam mobile string required 手机号码. Example: 13800138000
     * @response {
     *   "code": 200,
     *   "message": "验证码发送成功",
     *   "data": null
     * }
     */
    public function sendVerifyCode(Request $request)
    {
        $mobile = $request->input('mobile');
        return $this->service->sendVerifyCode($mobile);
    }

    /**
     * [账户管理] 用户登录
     * 
     * 使用手机号和验证码登录账户
     * 
     * @bodyParam mobile string required 手机号码. Example: 13800138000
     * @bodyParam code string required 验证码. Example: 123456
     * @response {
     *   "code": 200,
     *   "message": "登录成功",
     *   "data": {
     *     "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...",
     *     "user": {
     *       "id": 1,
     *       "mobile": "13800138000",
     *       "nickname": "用户昵称"
     *     }
     *   }
     * }
     */
    public function login(Request $request)
    {
        $mobile = $request->input('mobile');
        $code = $request->input('code');
        return $this->service->login($mobile, $code);
    }

    /**
     * [账户管理] 微信登录
     * 
     * 使用微信openid登录账户
     * 
     * @bodyParam openid string required 微信openid. Example: wx_123456789
     * @response {
     *   "code": 200,
     *   "message": "登录成功",
     *   "data": {
     *     "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...",
     *     "user": {
     *       "id": 1,
     *       "openid": "wx_123456789",
     *       "nickname": "微信昵称"
     *     }
     *   }
     * }
     */
    public function wxLogin(Request $request)
    {
        $openid = $request->input('openid');
        return $this->service->wxLogin($openid);
    }

    /**
     * [账户管理] 用户退出
     * 
     * 退出当前账户登录状态
     * 
     * @authenticated
     * @response {
     *   "code": 200,
     *   "message": "退出成功",
     *   "data": null
     * }
     */
    public function logout()
    {
        return $this->service->logout();
    }

    /**
     * [账户管理] 用户注销
     * 
     * 永久注销当前账户
     * 
     * @authenticated
     * @response {
     *   "code": 200,
     *   "message": "注销成功",
     *   "data": null
     * }
     */
    public function destroy()
    {
        return $this->service->deleteAccount();
    }
}