package com.hotwj.platform.integration.sms;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.hotwj.platform.integration.sms.domain.SmsRecord;
import com.hotwj.platform.integration.sms.mapper.SmsRecordMapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.service.ConfigService;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Slf4j
@Component
@RequiredArgsConstructor
public class SmsBaoClient {

    private final SmsBaoProperties properties;
    private final ConfigService configService;
    @Autowired
    private SmsRecordMapper smsRecordMapper;

    @Data
    @AllArgsConstructor
    public static class SmsSendResult {
        private boolean success;
        private String code;
        private String raw;
    }

    public SmsSendResult send(String mobile, String content) {
        String tokens = configService.getConfigValue("sys.sms.lock");
        if (StringUtils.isNotBlank(tokens) && "false".equals(tokens)) {
            return new SmsSendResult(false, "sms_locked", "短信开关未开启");
        }
//        HttpRequest request = HttpRequest.get(properties.getBaseUrl())
//                .form("u", properties.getU())
//                .form("p", properties.getP())
//                .form("m", mobile)
//                .form("c", "【洛克石油】" + content)
//                .timeout(properties.getTimeoutMs());
        HttpRequest request = HttpRequest.get(properties.getBaseUrl())
                .form("u", properties.getU())
                .form("p", properties.getP())
                .form("m", mobile)
                .form("c", "【五彩行】" + content)
                .timeout(properties.getTimeoutMs());
        HttpResponse resp = request.execute();
        String body = resp.body() == null ? "" : resp.body().trim();
        boolean ok = "0".equals(body);
        if (!ok) {
            log.warn("短信宝发送失败 code={} mobile={}", body, mobile);
        } else {
            SmsRecord record = new SmsRecord();
            if (LoginHelper.getUserId() == null) {
                record.setSenderUserId(0L);
            } else {
                record.setSenderUserId(LoginHelper.getUserId());
            }
            if (LoginHelper.getUsername() == null) {
                record.setSenderUsername("系统发送短信");
            } else {
                record.setSenderUsername(LoginHelper.getUsername());
            }

            record.setReceiverMobile(mobile);
            record.setContent(content);
            record.setSendTime(new Date());
            record.setSuccess(true);
            record.setCode(body);
            record.setProvider("smsbao");
            record.setRaw(resp.body());
            try {
                smsRecordMapper.insert(record);
            } catch (Exception e) {
                log.warn("短信记录入库失败 mobile={} err={}", mobile, e.getMessage());
            }
        }
        return new SmsSendResult(ok, body, resp.body());
    }
}
