package com.mosquito.project.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 短信服务实现
 * 支持mock模式和真实短信发送模式切换
 */
@Service
public class SmsServiceImpl implements SmsService {

    private static final Logger log = LoggerFactory.getLogger(SmsServiceImpl.class);

    @Value("${app.notification.sms.provider:mock}")
    private String smsProvider;

    @Value("${app.notification.sms.access-key-id:}")
    private String accessKeyId;

    @Value("${app.notification.sms.access-key-secret:}")
    private String accessKeySecret;

    @Value("${app.notification.sms.sign-name:蚊子系统}")
    private String signName;

    @Value("${app.notification.sms.template-code:}")
    private String templateCode;

    @Override
    public boolean send(String phone, String content) {
        log.info("[SMS] 发送短信: phone={}, provider={}", phone, smsProvider);

        if ("mock".equalsIgnoreCase(smsProvider)) {
            log.info("[SMS-MOCK] 模拟发送短信: phone={}, content={}", phone, content);
            return true;
        }

        // 真实发送模式 - 阿里云短信示例
        try {
            // 这里可以集成阿里云、腾讯云等真实短信服务
            // 以下为示例代码（需要添加对应SDK依赖）
            Map<String, String> params = new HashMap<>();
            params.put("phone", phone);
            params.put("content", content);
            params.put("signName", signName);

            // 调用真实短信API（需根据实际供应商实现）
            // com.aliyun.dysmsapi20170525.Client smsClient = ...
            // SendSmsRequest request = new SendSmsRequest()...

            log.info("[SMS] 短信发送成功: phone={}", phone);
            return true;
        } catch (Exception e) {
            log.error("[SMS] 短信发送失败: phone={}, error={}", phone, e.getMessage());
            return false;
        }
    }

    @Override
    public boolean sendApprovalTimeoutSms(String phone, String approverName, Long recordId) {
        String content = String.format(
            "【%s】尊敬的%s，您有待处理的审批即将超时，请尽快处理。审批记录ID: %d",
            signName, approverName, recordId
        );
        return send(phone, content);
    }
}
