package org.example.mybatisplus.model.auto;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import org.example.mybatisplus.module.AuditResult;
import org.example.mybatisplus.module.BasicInfo;
import org.example.mybatisplus.module.vo.BasicInfoCheck;
import org.example.mybatisplus.module.vo.BasicInfoScore;
import org.example.mybatisplus.module.vo.CheckItemScore;

import lombok.Data;

@Data
public class CmsEnterpriseRiskInfoCheckBO {
    // key: checkItemLabel
    private Map<String, CmsEnterpriseRiskInfoCheckItem> mpCmsEnterpriseRiskInfoCheckItem =
        Collections.emptyMap();
    private CmsEnterpriseRiskInfoCheck cmsEnterpriseRiskInfoCheck =
        new CmsEnterpriseRiskInfoCheck();
    private Map<String, CmsEntRiskInfoCktItemCategory> mpCmsEntRiskInfoCktItemCategory =
        Collections.emptyMap();

    public Long getScore() {
        return mpCmsEntRiskInfoCktItemCategory.values().stream()
            .mapToLong(CmsEntRiskInfoCktItemCategory::getScore).sum();
    }

    public String getRiskAlert() {
        return mpCmsEnterpriseRiskInfoCheckItem.values().stream()
        .filter(item -> !item.getBizResult())
        .map(item -> item.getResultMsg())
        .reduce("", (r, msg) -> { return String.format("%s\n%s", msg); });
    }

    public List<CheckItemScore> getCheckItemScores() {
        return mpCmsEnterpriseRiskInfoCheckItem.values().stream().map(item -> {
            CheckItemScore ret = new CheckItemScore();
            ret.setItemName(item.getItemName());
            ret.setScore(item.getBizScore());
            ret.setCategory(mpCmsEntRiskInfoCktItemCategory.get(item.getCategoryType()).getCategoryName());
            return ret;
        }).collect(Collectors.toList());
    }

    public void fillBasicInfoVO(BasicInfo basicInfo) {
        List<BasicInfoCheck> checks = new ArrayList<>();
        for(String label: new String[] {"donation_revenue_scale", "net_worth_rating", "operation_years", "moca_rating"}) {
            BasicInfoCheck check = new BasicInfoCheck();
            check.setName(mpCmsEnterpriseRiskInfoCheckItem.get(label).getItemName());
            check.setDesc(mpCmsEnterpriseRiskInfoCheckItem.get(label).getDescription());
            check.setFact(mpCmsEnterpriseRiskInfoCheckItem.get(label).getActualValue());
            check.setResult(mpCmsEnterpriseRiskInfoCheckItem.get(label).getBizResultStr());
            check.setScore(mpCmsEnterpriseRiskInfoCheckItem.get(label).getScore());
            checks.add(check);
        }
        basicInfo.setBasicInfoChecks(checks);
        
        basicInfo.setBasicInfoScore(new BasicInfoScore(
            mpCmsEntRiskInfoCktItemCategory.get("BASIC").getScore(),
            cmsEnterpriseRiskInfoCheck.getRemarks(),
            cmsEnterpriseRiskInfoCheck.getAdvice()
        ));
    }

    public void fillAuditResultVO(AuditResult auditResult) {
        auditResult.setScore(Long.toString(this.getScore()));
        auditResult.setOpinion(cmsEnterpriseRiskInfoCheck.getReview());
        auditResult.setAuditDate(cmsEnterpriseRiskInfoCheck.getCheckTime().toString());
        auditResult.setRiskAlert(this.getRiskAlert());
        auditResult.setSuggestion(cmsEnterpriseRiskInfoCheck.getAdvice());
        auditResult.setCheckItemScores(getCheckItemScores());
    }
}
