package com.gyt.shr.controller.UserController;

import com.gyt.shr.entity.Person;
import com.gyt.shr.service.CodeRecordService;
import com.gyt.shr.service.PersonService;
import com.gyt.shr.util.AccountCheck;
import com.gyt.shr.util.commonUtils;
import com.gyt.shr.result.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author coyle
 * @description 用于管理员的控制类
 * 202501
 */
@Controller
@CrossOrigin //  允许跨域
public class LoginController {
    @Resource
    private PersonService personService;

    @Autowired
    private AccountCheck accountCheck  ;

    @Autowired
    private CodeRecordService codeRecordService;

    /**
     * @return 返回视图信息。
     * @description 用于管理员登录：普通管理员和超级管理员
     */
    @PostMapping("/user/sendCode")
    public ResponseEntity<?> sendCode(@RequestBody Map<String, Object> loginData, HttpServletRequest request) {
        String phone = StringUtils.defaultString((String) loginData.get("phone"), "");
        //从数据库先查询是否存在该手机号
        Result<Person> result = personService.checkPersonByPhone(phone);
        if (result.getCode() == -1) {
            //不存在用户，返回不存在用户信息
            return ResponseEntity.ok(commonUtils.createImmutableMap("success", false, "message", result.getMsg()));
        }
        return   ResponseEntity.ok(commonUtils.createImmutableMap("success", true, "message",  "成功"));

    }


    /**
     * @return 返回视图信息。
     * @description 用于管理员登录：普通管理员和超级管理员
     */
    @PostMapping("/user/verifyCode")
    public ResponseEntity<?> verifyCode(@RequestBody Map<String, Object> loginData, HttpServletRequest request) {
        String phone = StringUtils.defaultString((String) loginData.get("phone"), "");
        String code = StringUtils.defaultString((String) loginData.get("code"), "");
        //验证格式
        Result<String> stringResult = accountCheck.loginTextCheck(phone, code);
        // 验证验证码
        if (!stringResult.getSuccess()) {
            return ResponseEntity.ok(commonUtils.createImmutableMap("success", false, "message",stringResult.getMsg()));
        }
        //验证验证码是否有效,根据数据库
        Result<Boolean> codeHasResult = codeRecordService.checkHasCode(phone, code, 0);
        if(!codeHasResult.getSuccess()){
            return ResponseEntity.ok(commonUtils.createImmutableMap("success", false, "message", "验证码错误"));
        }
        return   ResponseEntity.ok(commonUtils.createImmutableMap("success", true, "message",  "成功"));
    }



    /**
     * @description  退出登录。
     * @param request 请求对象
     * @return 重定向到index页面。
     */
    @PostMapping("/user/logout")
    public ResponseEntity<?> logout(HttpServletRequest request) {
//        userService.adminLogout(request);
//        // 创建一个可变的 Map
//        Map<String, Object> responseMap = new HashMap<>();
//        responseMap.put("success", true);
//        responseMap.put("message", "登出成功！");
//        // 返回成功响应，使用不可变 Map
//        return ResponseEntity.ok(Collections.unmodifiableMap(responseMap));
        return null;
    }




}
