package com.seer.teach.user.app.user.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.seer.teach.common.ResultBean;
import com.seer.teach.common.annotation.DecryptionAnnotation;
import com.seer.teach.common.annotation.EncryptionAnnotation;
import com.seer.teach.common.annotation.LogOperation;
import com.seer.teach.common.annotation.LogPrint;
import com.seer.teach.common.enums.ModuleEnum;
import com.seer.teach.user.app.user.controller.req.ChildrenInfoUpdateReq;
import com.seer.teach.user.app.user.controller.req.UserRegisterReq;
import com.seer.teach.user.app.user.controller.req.UserResetPasswordReq;
import com.seer.teach.user.app.user.controller.resp.AppUserInfoResp;
import com.seer.teach.user.app.user.service.AppMyChildrenService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@LogPrint
@EncryptionAnnotation
@DecryptionAnnotation
@RestController
@Tag(name = "App 用户孩子接口")
@Validated
@SaCheckLogin(type = StpUtil.TYPE)
@RequiredArgsConstructor
@RequestMapping("/app/my-children")
public class AppMyChildrenController {

    private final AppMyChildrenService appMyChildrenService;

    @Operation(summary = "注册孩子")
    @PostMapping("/register")
    @LogOperation(module = ModuleEnum.SEER_USER, subModule = "注册孩子")
    @SaCheckPermission("app:user:child:register")
    public ResultBean<Boolean> registerChildren(@RequestHeader("device") String device,
                                                @RequestBody @Validated UserRegisterReq userRegisterReq) {
        return ResultBean.success(appMyChildrenService.registerChildren(device, userRegisterReq));
    }

    @Operation(summary = "重置密码接口")
    @PostMapping("/resetPassword")
    @LogOperation(module = ModuleEnum.SEER_USER, subModule = "重置密码")
    @SaCheckPermission("app:user:password:reset")
    public ResultBean<Boolean> resetPassword(@RequestBody @Validated UserResetPasswordReq params) {
        return ResultBean.success(appMyChildrenService.resetPassword(params));
    }

    /**
     * 家长更新孩子信息
     *
     * @param updateReq
     * @return
     */
    @Operation(summary = "更新孩子信息")
    @PostMapping("/updateChildrenInfo")
    @SaCheckPermission("app:user:children:update")
    public ResultBean<Boolean> updateChildrenInfo(@RequestBody @Validated ChildrenInfoUpdateReq updateReq) {
        return ResultBean.success(appMyChildrenService.updateChildrenInfo(updateReq));
    }

    @Operation(summary = "获取默认的孩子信息")
    @GetMapping("/default")
    @SaCheckPermission("app:user:query:default")
    @LogOperation(module = ModuleEnum.SEER_USER, subModule = "获取默认的孩子信息")
    public ResultBean<AppUserInfoResp> getDefaultChildrenInfo() {
        return ResultBean.success(appMyChildrenService.getDefaultChildrenInfo());
    }

    @Operation(summary = "获取孩子详情")
    @GetMapping("/{childId}")
    @SaCheckPermission("app:user:children:detail")
    @LogOperation(module = ModuleEnum.SEER_USER, subModule = "获取孩子的详情")
    public ResultBean<AppUserInfoResp> getChildDetail(@PathVariable("childId") Integer childId) {
        return ResultBean.success(appMyChildrenService.getChildDetail(childId));
    }

    @Operation(summary = "获取孩子列表")
    @PostMapping("/list")
    @LogOperation(module = ModuleEnum.SEER_USER, subModule = "获取孩子列表")
    @SaCheckPermission("app:user:query:list")
    public ResultBean<List<AppUserInfoResp>> list() {
        return ResultBean.success(appMyChildrenService.list());
    }

    @PutMapping("/{childId}/set-default")
    @Operation(summary = "设置为默认孩子")
    public ResultBean<Boolean> setDefaultChild(@PathVariable("childId") Integer childId) {
        return ResultBean.success(appMyChildrenService.setDefaultChild(childId));
    }
}
