package com.cyksj.service.mail.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.common.util.TicketCodeCommonUtil;
import com.cyksj.service.mail.CommonMailService;
import com.cyksj.service.mail.CuiQiuMailService;
import com.cyksj.service.mail.MailLuService;
import com.cyksj.service.mail.YhMailService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 项目名: yhlxj
 * 文件名: CommonMailServiceImpl
 * 创建者: JavaZou
 * 创建时间:2025/1/7 14:22
 */
@Service
@RequiredArgsConstructor
public class CommonMailServiceImpl implements CommonMailService {
	private final CuiQiuMailService cuiQiuMailService;

	private final MailLuService mailLuService;

	private final YhMailService yhMailService;

	//GPT修改密码匹配
	private final static String GPT_RESET_PWD_LINK_REGEX = "https://auth0.openai.com/u/reset-verify";
	@Override
	public String getTicketMailCode(Long goodsId, String account, String relateEmail, String emailPwd, Date emailCreatedTime, Integer type, Integer linkType, Boolean isReturnDeviceUrl) throws Exception {
		if (StrUtil.isEmpty(relateEmail)) {
			//25年的GPT账号获取验证码 特殊处理
			//获取失败走下一步
			if (goodsId == 18 && emailCreatedTime.compareTo(DateUtil.parse("2025-01-01")) > 0) {
				try {
					//获取近十封邮件
					String url = "https://ms.lqqq.cc/web/" + account + "----" + emailPwd;
					HttpRequest post = HttpUtil.createGet(url);
					HttpResponse execute = post.execute();
					String body = execute.body();
					if (body.contains("Your ChatGPT code is") || body.contains("Your Sora code is")) {
						String code = TicketCodeCommonUtil.getTicketCodeStr(body, goodsId);
						return code;
					}

				} catch (Exception e) {
				}
			}
		}
		String checkEmail = StrUtil.isNotEmpty(relateEmail) ? relateEmail : account;
		//关联邮箱
		if (cuiQiuMailService.isCuiQiuEmail(checkEmail)) {
			return cuiQiuMailService.getVerifyCode(account, relateEmail, goodsId, type, linkType, false);
		}
		//是否是 mail lu邮箱
		if (mailLuService.isMailLuEmail(checkEmail)) {
			return mailLuService.getVerifyCode(account, relateEmail, goodsId, type, linkType, false);
		}
		//银河域名邮件
		return yhMailService.getVerifyCode(account, relateEmail, goodsId, type, linkType, false);
	}

	@Override
	public String getGptChangePwdLink(String email, String password) throws Exception {
		String url = "https://ms.lqqq.cc/web/" + email + "----" + password;
		HttpRequest post = HttpUtil.createGet(url);
		HttpResponse execute = post.execute();
		String body = execute.body();
		try {
			if (body.contains(GPT_RESET_PWD_LINK_REGEX)) {
				return TicketCodeCommonUtil.extractUrl(body, GPT_RESET_PWD_LINK_REGEX).replaceAll(">", "");
			}
		} catch (Exception e) {
			throw BusinessRuntimeException.getInstance("邮箱或密码错误");
		}
		throw BusinessRuntimeException.getInstance("未收到重置密码链接");
	}
}
