package com.fuyuanshen.app.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.util.RandomUtil;
import com.fuyuanshen.app.domain.bo.AppDeviceShareBo;
import com.fuyuanshen.app.domain.vo.AppDeviceShareDetailVo;
import com.fuyuanshen.app.domain.vo.AppDeviceShareVo;
import com.fuyuanshen.app.service.AppDeviceShareService;
import com.fuyuanshen.app.service.IAppDeviceShareService;
import com.fuyuanshen.common.core.constant.Constants;
import com.fuyuanshen.common.core.domain.R;
import com.fuyuanshen.common.core.validate.AddGroup;
import com.fuyuanshen.common.idempotent.annotation.RepeatSubmit;
import com.fuyuanshen.common.mybatis.core.page.PageQuery;
import com.fuyuanshen.common.mybatis.core.page.TableDataInfo;
import com.fuyuanshen.common.ratelimiter.annotation.RateLimiter;
import com.fuyuanshen.common.redis.utils.RedisUtils;
import com.fuyuanshen.common.web.core.BaseController;
import com.fuyuanshen.web.service.DeviceShareService;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.sms4j.api.SmsBlend;
import org.dromara.sms4j.api.entity.SmsResponse;
import org.dromara.sms4j.core.factory.SmsFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.Duration;
import java.util.LinkedHashMap;

import static com.fuyuanshen.common.core.constant.GlobalConstants.DEVICE_SHARE_CODES_KEY;

/**
 * APP 设备分享
 *
 * @author Lion Li
 * @date 2025-07-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/deviceShare")
public class AppDeviceShareController extends BaseController {

    private final AppDeviceShareService appDeviceShareService;


    /**
     * 分享管理列表
     */
    @GetMapping("/deviceShareList")
    public TableDataInfo<AppDeviceShareVo> list(AppDeviceShareBo bo, PageQuery pageQuery) {
        return appDeviceShareService.queryPageList(bo, pageQuery);
    }

    /**
     * 他人分享管理列表
     */
    @GetMapping("/otherDeviceShareList")
    public TableDataInfo<AppDeviceShareVo> otherDeviceShareList(AppDeviceShareBo bo, PageQuery pageQuery) {
        return appDeviceShareService.otherDeviceShareList(bo, pageQuery);
    }

    /**
     * 获取设备分享详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<AppDeviceShareDetailVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(appDeviceShareService.getInfo(id));
    }

    /**
     * 新增设备分享
     */
    @RepeatSubmit()
    @PostMapping("/deviceShare")
    public R<Void> deviceShare(@Validated(AddGroup.class) @RequestBody AppDeviceShareBo bo) {
        return toAjax(appDeviceShareService.deviceShare(bo));
    }

    /**
     * 移除分享用户
     *
     * @param ids 主键串
     */
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(appDeviceShareService.remove(ids));
    }


    /**
     * 短信验证码
     *
     * @param phonenumber 用户手机号
     */
    @SaIgnore
    @RateLimiter(key = "#phonenumber", time = 60, count = 1)
    @GetMapping("/sms/code")
    public R<Void> smsCode(@NotBlank(message = "{user.phonenumber.not.blank}") String phonenumber) {
        String key = DEVICE_SHARE_CODES_KEY + phonenumber;
        String code = RandomUtil.randomNumbers(4);
        RedisUtils.setCacheObject(key, code, Duration.ofMinutes(Constants.CAPTCHA_EXPIRATION));
        LinkedHashMap<String, String> map = new LinkedHashMap<>(1);
        map.put("code", code);
        SmsBlend smsBlend = SmsFactory.getSmsBlend("config1");
        SmsResponse smsResponse = smsBlend.sendMessage(phonenumber, map);
        if (!smsResponse.isSuccess()) {
            return R.fail(smsResponse.getData().toString());
        }
        return R.ok();
    }

}
