package com.erp.service;

import com.erp.common.BaseService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.*;

@Service
@RequiredArgsConstructor
@Slf4j
public class MailReceiveBasicService extends BaseService {

    private static final String NS = "mailReceiveBasic.";

    private final CommonService commonService;

    public Map<String, Object> getMailReceiveBasicList(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        commonService.applyPagination(params);
        int totalCount = sqlSession.selectOne(NS + "getMailReceiveBasicListCnt", params);
        List<Map<String, Object>> list = sqlSession.selectList(NS + "getMailReceiveBasicList", params);
        return commonService.buildListResponse(list, totalCount, params);
    }

    public int getMailReceiveBasicTodayCount(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        return sqlSession.selectOne(NS + "getMailReceiveBasicTodayCount", params);
    }

    public Map<String, Object> getMailReceiveBasicInfo(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        return sqlSession.selectOne(NS + "getMailReceiveBasicInfo", params);
    }

    @Transactional
    public Map<String, Object> markMailReceiveBasicAsRead(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        sqlSession.update(NS + "updateMailReceiveBasicAsRead", params);
        return params;
    }

    @Transactional
    public Map<String, Object> deleteMailReceiveBasic(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        sqlSession.update(NS + "deleteMailReceiveBasic", params);
        return params;
    }

    public Map<String, Object> getMailReceiveBasicAttachment(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        return sqlSession.selectOne(NS + "getMailReceiveBasicAttachment", params);
    }

    public Map<String, Object> testMailReceiveBasicImapConnection(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        Map<String, Object> account = sqlSession.selectOne(NS + "getMailReceiveBasicAccountInfo", params);
        if (account == null) {
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", "메일 계정을 찾을 수 없습니다.");
            return result;
        }
        String host = (String) account.get("imap_host");
        Object portObj = account.get("imap_port");
        int port = portObj != null ? ((Number) portObj).intValue() : 993;
        try (Socket socket = new Socket()) {
            socket.connect(new InetSocketAddress(host, port), 5000);
            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("message", "IMAP 연결 성공");
            result.put("host", host);
            result.put("port", port);
            return result;
        } catch (Exception e) {
            log.warn("IMAP 연결 실패: {}:{} - {}", host, port, e.getMessage());
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", "IMAP 연결 실패: " + e.getMessage());
            result.put("host", host);
            result.put("port", port);
            return result;
        }
    }
}
