package cn.stylefeng.team.project.modular.filing.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.entity.BaseEntity;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.team.project.modular.filing.entity.BusinessAiParseLog;
import cn.stylefeng.team.project.modular.filing.enums.ParseStatusEnum;
import cn.stylefeng.team.project.modular.filing.exceptions.BusinessAiParseLogExceptionEnum;
import cn.stylefeng.team.project.modular.filing.mapper.BusinessAiParseLogMapper;
import cn.stylefeng.team.project.modular.filing.pojo.aidesign.aiparse.ParseToBusinessResult;
import cn.stylefeng.team.project.modular.filing.pojo.businessAiParseLog.del.BusinessAiParseLogBatchDelRequest;
import cn.stylefeng.team.project.modular.filing.pojo.businessAiParseLog.del.BusinessAiParseLogDelRequest;
import cn.stylefeng.team.project.modular.filing.pojo.businessAiParseLog.edit.BusinessAiParseLogEditRequest;
import cn.stylefeng.team.project.modular.filing.pojo.businessAiParseLog.query.BusinessAiParseLogDetailRequest;
import cn.stylefeng.team.project.modular.filing.pojo.businessAiParseLog.query.BusinessAiParseLogListRequest;
import cn.stylefeng.team.project.modular.filing.pojo.businessAiParseLog.query.BusinessAiParseLogPageRequest;
import cn.stylefeng.team.project.modular.filing.pojo.businessAiParseLog.response.BusinessAiParseLogDetailResponse;
import cn.stylefeng.team.project.modular.filing.pojo.businessAiParseLog.response.BusinessAiParseLogListResponse;
import cn.stylefeng.team.project.modular.filing.pojo.businessAiParseLog.response.BusinessAiParseLogPageResponse;
import cn.stylefeng.team.project.modular.filing.service.BusinessAiParseLogService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class BusinessAiParseLogServiceImpl extends ServiceImpl<BusinessAiParseLogMapper, BusinessAiParseLog> implements BusinessAiParseLogService {

    @Override
    public BusinessAiParseLog add(Long businessId, Long originExcelFileId, Long originPicFileId) {
        BusinessAiParseLog businessAiParseLog = new BusinessAiParseLog();
        businessAiParseLog.setBusinessId(businessId);
        businessAiParseLog.setOriginExcelFileId(originExcelFileId);
        businessAiParseLog.setOriginPicFileId(originPicFileId);
        businessAiParseLog.setParseStatus(ParseStatusEnum.ITEM_10.getCode());
        businessAiParseLog.setBeginTime(new Date());
        this.save(businessAiParseLog);

        return businessAiParseLog;
    }

    @Override
    public void del(BusinessAiParseLogDelRequest businessAiParseLogDelRequest) {
        BusinessAiParseLog businessAiParseLog = this.queryBusinessAiParseLog(businessAiParseLogDelRequest.getId());
        this.removeById(businessAiParseLog.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDelete(BusinessAiParseLogBatchDelRequest businessAiParseLogBatchDelRequest) {
        this.removeByIds(businessAiParseLogBatchDelRequest.getBatchDeleteIdList());
    }

    @Override
    public void edit(BusinessAiParseLogEditRequest businessAiParseLogEditRequest) {
        BusinessAiParseLog businessAiParseLog = this.queryBusinessAiParseLog(businessAiParseLogEditRequest.getId());
        BeanUtil.copyProperties(businessAiParseLogEditRequest, businessAiParseLog);
        this.updateById(businessAiParseLog);
    }

    @Override
    public BusinessAiParseLogDetailResponse detail(BusinessAiParseLogDetailRequest businessAiParseLogDetailRequest) {
        BusinessAiParseLog businessAiParseLog = this.queryBusinessAiParseLog(businessAiParseLogDetailRequest.getId());
        return BeanUtil.copyProperties(businessAiParseLog, BusinessAiParseLogDetailResponse.class);
    }

    @Override
    public List<BusinessAiParseLogListResponse> findList(BusinessAiParseLogListRequest businessAiParseLogListRequest) {
        LambdaQueryWrapper<BusinessAiParseLog> wrapper = new LambdaQueryWrapper<>();


        List<BusinessAiParseLog> list = this.list(wrapper);

        return BeanUtil.copyToList(list, BusinessAiParseLogListResponse.class);
    }

    @Override
    public PageResult<BusinessAiParseLogPageResponse> findPage(BusinessAiParseLogPageRequest businessAiParseLogPageRequest) {
        Page<BusinessAiParseLog> page = PageFactory.defaultPage();

        LambdaQueryWrapper<BusinessAiParseLog> wrapper = new LambdaQueryWrapper<>();

        wrapper.orderByDesc(BaseEntity::getCreateTime);

        if (ObjectUtil.isNotEmpty(businessAiParseLogPageRequest.getBusinessId())) {
            wrapper.eq(BusinessAiParseLog::getBusinessId, businessAiParseLogPageRequest.getBusinessId());
        }

        Page<BusinessAiParseLog> pageResult = this.page(page, wrapper);
        List<BusinessAiParseLog> resultRecords = pageResult.getRecords();
        if (ObjectUtil.isEmpty(resultRecords)) {
            return PageResultFactory.createPageResult(page, new ArrayList<>());
        }

        List<BusinessAiParseLogPageResponse> businessAiParseLogPageResponses = BeanUtil.copyToList(resultRecords, BusinessAiParseLogPageResponse.class);
        return PageResultFactory.createPageResult(pageResult, businessAiParseLogPageResponses);
    }

    @Override
    public BusinessAiParseLogDetailResponse customDetail(BusinessAiParseLogDetailRequest businessAiParseLogDetailRequest) {
        return this.getBaseMapper().customDetail(businessAiParseLogDetailRequest);
    }

    @Override
    public List<BusinessAiParseLogListResponse> customList(BusinessAiParseLogListRequest businessAiParseLogListRequest) {
        return this.getBaseMapper().customList(businessAiParseLogListRequest);
    }

    @Override
    public PageResult<BusinessAiParseLogPageResponse> customPage(BusinessAiParseLogPageRequest businessAiParseLogPageRequest) {
        Page<BusinessAiParseLogPageResponse> page = PageFactory.defaultPage();

        List<BusinessAiParseLogPageResponse> businessAiParseLogPageResponses = this.getBaseMapper().customPage(page, businessAiParseLogPageRequest);
        page.setRecords(businessAiParseLogPageResponses);

        return PageResultFactory.createPageResult(page);
    }

    @Override
    public void updateErrorLog(Throwable throwable, BusinessAiParseLog businessAiParseLog) {
        businessAiParseLog.setParseStatus(ParseStatusEnum.ITEM_30.getCode());
        businessAiParseLog.setEndTime(new Date());
        businessAiParseLog.setErrorMessage(throwable.getMessage());
        this.updateById(businessAiParseLog);
    }

    @Override
    public void updateSuccessLog(BusinessAiParseLog businessAiParseLog, String aiResponseString, ParseToBusinessResult parseBusinessResult) {
        businessAiParseLog.setParseStatus(ParseStatusEnum.ITEM_20.getCode());
        businessAiParseLog.setEndTime(new Date());
        businessAiParseLog.setAiResponseString(aiResponseString);
        businessAiParseLog.setParseBusinessResult(parseBusinessResult);
        this.updateById(businessAiParseLog);
    }

    @Override
    public void updateStopLog(Long aiParseLogId) {
        BusinessAiParseLog businessAiParseLog = this.getById(aiParseLogId);
        businessAiParseLog.setParseStatus(ParseStatusEnum.ITEM_40.getCode());
        businessAiParseLog.setEndTime(new Date());
        businessAiParseLog.setAiResponseString("用户手动停止解析");
        businessAiParseLog.setErrorMessage("用户手动停止解析");
        this.updateById(businessAiParseLog);
    }

    @Override
    public boolean validateAiParseCanContinue(Long aiParseLogId) {
        BusinessAiParseLog businessAiParseLog = this.getById(aiParseLogId);

        if (businessAiParseLog == null) {
            return false;
        }

        Integer parseStatus = businessAiParseLog.getParseStatus();
        if (parseStatus.equals(ParseStatusEnum.ITEM_10.getCode())) {
            return true;
        } else if (parseStatus.equals(ParseStatusEnum.ITEM_20.getCode())) {
            return false;
        } else if (parseStatus.equals(ParseStatusEnum.ITEM_30.getCode())) {
            return false;
        } else if (parseStatus.equals(ParseStatusEnum.ITEM_40.getCode())) {
            return false;
        }

        return false;
    }

    private BusinessAiParseLog queryBusinessAiParseLog(Long id) {
        BusinessAiParseLog businessAiParseLog = this.getById(id);
        if (ObjectUtil.isEmpty(businessAiParseLog)) {
            throw new ServiceException(BusinessAiParseLogExceptionEnum.BUSINESS_AI_PARSE_LOG_NOT_EXISTED);
        }
        return businessAiParseLog;
    }
}