package com.fuyuanshen.mp.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.exception.NotLoginException;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.fuyuanshen.app.model.AppSmsLoginBody;
import com.fuyuanshen.app.service.AppLoginService;
import com.fuyuanshen.common.core.constant.SystemConstants;
import com.fuyuanshen.common.core.domain.R;
import com.fuyuanshen.common.core.domain.model.AppLoginUser;
import com.fuyuanshen.common.core.domain.model.LoginUser;
import com.fuyuanshen.common.core.domain.model.RegisterBody;
import com.fuyuanshen.common.core.domain.model.SmsLoginBody;
import com.fuyuanshen.common.core.utils.*;
import com.fuyuanshen.common.encrypt.annotation.ApiEncrypt;
import com.fuyuanshen.common.json.utils.JsonUtils;
import com.fuyuanshen.common.satoken.utils.LoginHelper;
import com.fuyuanshen.common.tenant.helper.TenantHelper;
import com.fuyuanshen.equipment.domain.UserApp;
import com.fuyuanshen.equipment.enums.AppUserTypeEnum;
import com.fuyuanshen.mp.domian.dto.AuthUserDto;
import com.fuyuanshen.mp.service.MPAuthService;
import com.fuyuanshen.mp.service.MPService;
import com.fuyuanshen.system.domain.bo.SysTenantBo;
import com.fuyuanshen.system.domain.vo.SysClientVo;
import com.fuyuanshen.system.domain.vo.SysTenantVo;
import com.fuyuanshen.system.domain.vo.SysUserVo;
import com.fuyuanshen.system.service.ISysClientService;
import com.fuyuanshen.system.service.ISysConfigService;
import com.fuyuanshen.system.service.ISysTenantService;
import com.fuyuanshen.web.domain.vo.LoginTenantVo;
import com.fuyuanshen.web.domain.vo.LoginVo;
import com.fuyuanshen.web.domain.vo.TenantListVo;
import com.fuyuanshen.web.service.IAuthStrategy;
import com.fuyuanshen.web.service.SysRegisterService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.sms4j.api.SmsBlend;
import org.dromara.sms4j.api.entity.SmsResponse;
import org.dromara.sms4j.core.factory.SmsFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * APP认证
 *
 * @author Lion Li
 */
@Slf4j
@SaIgnore
@RequiredArgsConstructor
@RestController
@RequestMapping("/mp")
public class MPAuthController {

    private final AppLoginService loginService;
    private final SysRegisterService registerService;
    private final ISysConfigService configService;
    private final ISysTenantService tenantService;
    private final ISysClientService clientService;
    private final MPAuthService mpAuthService;
    private final MPService mpService;


    @Operation(summary = "小程序登录授权")
    @PostMapping(value = "/login")
    public ResponseEntity<Object> login(@RequestBody AuthUserDto authUser, HttpServletRequest request) throws Exception {

        Long phoneNumber = authUser.getPhoneNumber();
        // 判断小程序用户是否存在，不存在创建
        UserApp mpUser = mpService.getMpUser(phoneNumber);
        if (mpUser == null) {
            RegisterBody registerBody = new RegisterBody();
            registerBody.setUsername(phoneNumber.toString());
            registerBody.setUserType(AppUserTypeEnum.XCX_USER.getCode());
            registerBody.setTenantId("894078");
            registerBody.setPassword("123456");
            mpAuthService.register(registerBody);
        }

        // 去登录
        SysClientVo client = clientService.queryByClientId("ca839698e245d60aa2f0e59bd52b34f8");
        UserApp user = mpService.loadUserByUsername(phoneNumber.toString());
        LoginUser loginUser = mpAuthService.buildLoginUser(user);
        LoginVo login = mpAuthService.login(loginUser, client);

        // 返回登录信息
        return ResponseEntity.ok(login);
    }


}
