package com.smppw.modaq;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DateUtil;
import com.smppw.modaq.application.service.EmailParseApiService;
import com.smppw.modaq.common.conts.DateConst;
import com.smppw.modaq.common.conts.EmailTypeConst;
import com.smppw.modaq.domain.dto.MailboxInfoDTO;
import com.smppw.modaq.domain.service.EmailParseService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Date;
import java.util.List;

@SpringBootTest(classes = MoDaqApplication.class)
public class MoDaqApplicationTests {

    @Autowired
    private EmailParseService emailParseService;

    @Autowired
    private EmailParseApiService emailParseApiService;

    @Test
    public void letterTest() {
        MailboxInfoDTO emailInfoDTO = this.buildMailbox("**@simuwang.com", "**");
        Date startDate = DateUtil.parse("2025-06-18 08:47:00", DateConst.YYYY_MM_DD_HH_MM_SS);
        Date endDate = DateUtil.parse("2025-06-18 18:56:00", DateConst.YYYY_MM_DD_HH_MM_SS);
        try {
            List<String> folderNames = ListUtil.list(false);
//            folderNames.add("其他文件夹/报告公告");
            folderNames.add("INBOX");
            emailParseService.parseEmail(emailInfoDTO, startDate, endDate,
                    folderNames, ListUtil.of(EmailTypeConst.REPORT_LETTER_EMAIL_TYPE));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void reportTest() {
        MailboxInfoDTO emailInfoDTO = this.buildMailbox("**@simuwang.com", "**");
        Date startDate = DateUtil.parse("2025-06-18 08:47:00", DateConst.YYYY_MM_DD_HH_MM_SS);
        Date endDate = DateUtil.parse("2025-06-18 13:57:00", DateConst.YYYY_MM_DD_HH_MM_SS);
        try {
            List<String> folderNames = ListUtil.list(false);
//            folderNames.add("其他文件夹/报告公告");
            folderNames.add("INBOX");
            emailParseService.parseEmail(emailInfoDTO, startDate, endDate,
                    folderNames, EmailTypeConst.REPORT_EMAIL_TYPES);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

//    @Test
//    public void testReparseEmail() {
//        emailParseApiService.reparseEmail(23);
//    }


    private MailboxInfoDTO buildMailbox(String account, String pwd) {
        MailboxInfoDTO emailInfoDTO = new MailboxInfoDTO();
        emailInfoDTO.setUserId(1);
        emailInfoDTO.setAccount(account);
        emailInfoDTO.setPassword(pwd);
        emailInfoDTO.setHost("imap.exmail.qq.com");
        emailInfoDTO.setPort("993");
        emailInfoDTO.setProtocol("imap");
        return emailInfoDTO;
    }
}
