package com.fuyuanshen.web.controller.device;

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.AppDeviceShareVo;
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 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;

/**
 * 设备分享管理
 *
 * @author Lion Li
 * @date 2025-08-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("api/equipment/share")
public class DeviceShareController extends BaseController {

    private final DeviceShareService deviceShareService;


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


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

    /**
     * 权限管理
     */
    @RepeatSubmit()
    @PostMapping("/permission")
    public R<Void> permission(@Validated(AddGroup.class) @RequestBody AppDeviceShareBo bo) {
        return toAjax(deviceShareService.permission(bo));
    }

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