package com.cool.store.controller.webb;

import com.cool.store.dto.login.ModifyPasswordDTO;
import com.cool.store.dto.login.UserLoginDTO;
import com.cool.store.dto.login.UserRefreshLoginDTO;
import com.cool.store.dto.sms.SendSmsCodeDTO;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.response.ResponseResult;
import com.cool.store.service.enterprise.EnterpriseUserService;
import com.cool.store.service.login.LoginBaseService;
import com.cool.store.service.login.LoginStrategy;
import com.cool.store.service.sms.AliyunSmsService;
import com.cool.store.utils.HttpHelper;
import com.cool.store.utils.RedisUtilPool;
import com.cool.store.utils.SpringContextUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;

import static com.cool.store.service.sms.impl.AliyunSmsServiceImpl.getRandNum;

/**
 * <p>
 * 登录 前端控制器
 * </p>
 *
 * @author wangff
 * @since 2025/9/4
 */
@Api(tags = "登录")
@RestController
@RequestMapping("/pc/v3/login")
@RequiredArgsConstructor
public class LoginController {
    @Resource(name = "passwordLoginServiceImpl")
    private LoginBaseService loginBaseService;
    private final EnterpriseUserService enterpriseUserService;
    private final RedisUtilPool redisUtilPool;
    private final AliyunSmsService aliyunSmsService;

    private final int SEND_SMS_LIMIT_COUNT = 10;
    private final int SEND_SMS_IP_LIMIT_COUNT = 100;

    @ApiOperation("登录")
    @PostMapping("/accountLogin")
    public ResponseResult accountLogin(@RequestBody UserLoginDTO param) {
        return SpringContextUtil.getBean(param.getLoginType().getClazzName(), LoginStrategy.class).login(param);
    }

    @ApiOperation("refresh登录")
    @PostMapping("/refreshLogin")
    public ResponseResult refreshLogin(@RequestBody UserRefreshLoginDTO param) {
        return loginBaseService.refreshLogin(param);
    }

    @ApiOperation("登出")
    @PostMapping("/logout")
    public ResponseResult logout() {
        return loginBaseService.logout();
    }

    @ApiOperation("修改密码")
    @PostMapping("/modifyPassword")
    public ResponseResult modifyPassword(@Validated @RequestBody ModifyPasswordDTO param) {
        return enterpriseUserService.modifyPassword(param);
    }

    @ApiOperation("发送验证码")
    @PostMapping("/sendSmsCode")
    public ResponseResult sendSmsCode(@Validated @RequestBody SendSmsCodeDTO param, HttpServletRequest request) {
        //一天同一个手机号限制发送10条短信
        String codeKey = "sendSmsCode_" + LocalDate.now() + ":" + param.getMobile();
        Long sendCount = redisUtilPool.incrby(codeKey, 1, 24 * 60 * 60);
        if (sendCount > SEND_SMS_LIMIT_COUNT) {
            return ResponseResult.fail(ErrorCodeEnum.SEND_SMS_LIMIT_COUNT);
        }
        String ip = HttpHelper.getIpAddr(request);
        String ipCacheKey = "sendSmsCode_IP_" + LocalDate.now() + ":" + ip;
        Long ipSendCount = redisUtilPool.incrby(ipCacheKey, 1, 24 * 60 * 60);
        if (ipSendCount > SEND_SMS_IP_LIMIT_COUNT) {
            return ResponseResult.fail(ErrorCodeEnum.SEND_SMS_LIMIT_COUNT);
        }
        return aliyunSmsService.sendSmsCode(param.getMobile(), param.getCodeType());
    }

    @ApiOperation("测试发送验证码")
    @PostMapping("/sendSmsCode/test")
    public ResponseResult sendSmsCodeTest(@Validated @RequestBody SendSmsCodeDTO param){
        String smsCode = getRandNum();
        redisUtilPool.setString(param.getCodeType() + ":" + param.getMobile(), smsCode, param.getCodeType().getCacheSeconds());
        return ResponseResult.success(smsCode);
    }
}
