package org.qinan.safetyeval.app.executor;

import org.qinan.safetyeval.app.orchestrator.QualFilingOrchestrator;
import org.qinan.safetyeval.client.api.QualFilingMaterialApi;
import org.qinan.safetyeval.client.dto.PageResponse;
import org.qinan.safetyeval.client.dto.SingleResponse;
import org.qinan.safetyeval.client.co.QualFilingMaterialCO;
import org.qinan.safetyeval.client.dto.QualFilingMaterialAddCmd;
import org.qinan.safetyeval.client.dto.QualFilingMaterialModifyCmd;
import org.qinan.safetyeval.client.dto.QualFilingMaterialPageQuery;
import org.qinan.safetyeval.domain.entity.QualFilingMaterialEntity;
import org.qinan.safetyeval.domain.query.QualFilingMaterialQuery;
import org.qinan.safetyeval.domain.query.PageResult;
import org.qinan.safetyeval.domain.service.QualFilingMaterialDomainService;
import org.qinan.safetyeval.infrastructure.adapter.auth.AuthUserContextAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 备案申请材料执行器（App层）
 *
 * @author safety-eval
 */
@Service
public class QualFilingMaterialExecutor implements QualFilingMaterialApi {

    @Lazy
    @Autowired(required = false)
    private QualFilingMaterialDomainService qualFilingMaterialDomainService;

    @Override
    public SingleResponse<QualFilingMaterialCO> add(QualFilingMaterialAddCmd cmd) {
        QualFilingMaterialEntity entity = new QualFilingMaterialEntity();
        entity.setMaterialContent(cmd.getMaterialContent());
        entity.setMaterialFormat(cmd.getMaterialFormat());
        entity.setAttachmentUrl(cmd.getAttachmentUrl());
        entity.setMaterialRemark(cmd.getMaterialRemark());

        QualFilingMaterialEntity result = qualFilingMaterialDomainService.add(entity);
        return SingleResponse.success(toCO(result));
    }

    @Override
    public SingleResponse<QualFilingMaterialCO> get(Long id) {
        QualFilingMaterialEntity entity = qualFilingMaterialDomainService.get(id);
        return SingleResponse.success(toCO(entity));
    }

    @Override
    public SingleResponse<QualFilingMaterialCO> modify(QualFilingMaterialModifyCmd cmd) {
        QualFilingMaterialEntity entity = new QualFilingMaterialEntity();
        entity.setId(cmd.getId());
        entity.setMaterialContent(cmd.getMaterialContent());
        entity.setMaterialFormat(cmd.getMaterialFormat());
        entity.setAttachmentUrl(cmd.getAttachmentUrl());
        entity.setMaterialRemark(cmd.getMaterialRemark());

        QualFilingMaterialEntity result = qualFilingMaterialDomainService.modify(entity);
        return SingleResponse.success(toCO(result));
    }

    @Override
    public SingleResponse<Void> delete(Long id) {
        qualFilingMaterialDomainService.delete(id);
        return SingleResponse.success();
    }

    @Override
    public PageResponse<QualFilingMaterialCO> page(QualFilingMaterialPageQuery query) {
        QualFilingMaterialQuery domainQuery = new QualFilingMaterialQuery();
        domainQuery.setPageNum(query.getCurrent());
        domainQuery.setPageSize(query.getSize());
        domainQuery.setTenantId(AuthUserContextAdapter.getCurrentTenantId());
        domainQuery.setFilingId(query.getFilingId());
        domainQuery.setMaterialContent(query.getMaterialContent());

        PageResult<QualFilingMaterialEntity> pageResult = qualFilingMaterialDomainService.page(domainQuery);

        return PageResponse.of(
                pageResult.getRecords().stream()
                        .map(this::toCO)
                        .collect(java.util.stream.Collectors.toList()),
                pageResult.getTotal());
    }

    @Resource
    private QualFilingOrchestrator qualFilingOrchestrator;

    @Override
    public SingleResponse<Void> initTemplate(Long filingId) {
        qualFilingOrchestrator.initMaterialTemplate(filingId);
        return SingleResponse.success();
    }

    @Override
    public SingleResponse<QualFilingMaterialCO> upload(org.qinan.safetyeval.client.dto.QualFilingAttachmentUploadCmd cmd) {
        return SingleResponse.success(qualFilingOrchestrator.uploadMaterial(cmd.getId(), cmd.getAttachmentUrl()));
    }

    private QualFilingMaterialCO toCO(QualFilingMaterialEntity entity) {
        if (entity == null) {
            return null;
        }
        QualFilingMaterialCO co = new QualFilingMaterialCO();
        co.setId(entity.getId());
        co.setFilingId(entity.getFilingId());
        co.setMaterialContent(entity.getMaterialContent());
        co.setMaterialFormat(entity.getMaterialFormat());
        co.setSortOrder(entity.getSortOrder());
        co.setRequiredFlag(entity.getRequiredFlag());
        co.setUploadStatusCode(entity.getUploadStatusCode());
        co.setUploadStatusName(entity.getUploadStatusName());
        co.setAttachmentDesc(entity.getAttachmentDesc());
        co.setAttachmentUrl(entity.getAttachmentUrl());
        co.setMaterialRemark(entity.getMaterialRemark());
        co.setTenantId(entity.getTenantId());
        return co;
    }
}
