package com.ecep.contract.service;

import java.time.LocalDate;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.function.Consumer;

import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.ecep.contract.MessageHolder;
import com.ecep.contract.MyDateTimeUtils;
import com.ecep.contract.VendorFileType;
import com.ecep.contract.model.Vendor;
import com.ecep.contract.model.VendorFileTypeLocal;
import com.ecep.contract.util.ParamUtils;
import com.ecep.contract.vm.CompanyVendorFileViewModel;
import com.ecep.contract.vo.VendorFileVo;
import com.ecep.contract.vo.VendorVo;

@Service
public class VendorFileService extends QueryService<VendorFileVo, CompanyVendorFileViewModel> {

    public LocalDate getNextSignDate(VendorVo companyVendor, Consumer<String> state) {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException("Unimplemented method 'getNextSignDate'");
    }
    public LocalDate getNextSignDate(Vendor vendor, Consumer<String> state) {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException("Unimplemented method 'getNextSignDate'");
    }

    public Map<VendorFileType, VendorFileTypeLocal> getFileTypeLocalMap(Locale locale) {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException("Unimplemented method 'getFileTypeLocalMap'");
    }

    public void verify(VendorVo companyVendor, LocalDate verifyDate, MessageHolder holder) {
        // 查询所有评价表
        List<VendorFileVo> files = findAllByVendorAndType(companyVendor, VendorFileType.EvaluationForm);
        if (files == null || files.isEmpty()) {
            holder.error("未见供应商评价");
            return;
        }

        // 检索 验证日期最近一年内的有效评价表，日期宽限7天
        LocalDate begin = verifyDate.plusYears(-1);
        VendorFileVo vendorFile = files.stream()
                .filter(v -> v.getSignDate() != null && v.isValid())
                .filter(v -> MyDateTimeUtils.dateValidFilter(v.getSignDate(), begin, verifyDate, 7))
                .findFirst().orElse(null);
        if (vendorFile == null) {
            // 检索最后一个有效评价表
            VendorFileVo latestFile = files.stream()
                    .filter(v -> v.getSignDate() != null && v.isValid())
                    .max(Comparator.comparing(VendorFileVo::getSignDate))
                    .orElse(null);

            if (latestFile == null) {
                holder.error("未匹配的供应商评价");
                return;
            }
            // 提示评价表已过期
            holder.error("供应商评价已过期：" + latestFile.getSignDate() + ", 检测日期：" + verifyDate);
        }
    }

    public List<VendorFileVo> findAllByVendorAndType(VendorVo vendor, VendorFileType type) {
        return findAll(ParamUtils.builder()
                .equals("vendor", vendor.getId())
                .equals("type", type.name())
                .build(), Pageable.unpaged()).getContent();
    }
}
