package org.qinan.safetyeval.adapter.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.qinan.safetyeval.client.api.QualFilingApi;
import org.qinan.safetyeval.client.co.QualFilingCO;
import org.qinan.safetyeval.client.co.QualFilingComplianceCheckResultCO;
import org.qinan.safetyeval.client.co.QualFilingDetailCO;
import org.qinan.safetyeval.client.dto.PageResponse;
import org.qinan.safetyeval.client.dto.SingleResponse;
import org.qinan.safetyeval.client.dto.QualFilingAddCmd;
import org.qinan.safetyeval.client.dto.QualFilingAttachmentUploadCmd;
import org.qinan.safetyeval.client.dto.QualFilingModifyCmd;
import org.qinan.safetyeval.client.dto.QualFilingPageQuery;
import org.qinan.safetyeval.client.dto.QualFilingRejectCmd;
import org.qinan.safetyeval.domain.constant.QualFilingApplyTypeEnum;
import org.qinan.safetyeval.domain.constant.QualFilingStatusEnum;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 资质备案申请适配层（Controller）
 */
@Api(tags = "资质备案申请管理")
@RestController
@RequestMapping("/qual-filing")
public class QualFilingController {

    @Resource
    private QualFilingApi qualFilingApi;

    @ApiOperation("新增资质备案申请")
    @PostMapping("/save")
    public SingleResponse<QualFilingCO> add(@Validated @RequestBody QualFilingAddCmd cmd) {
        return qualFilingApi.add(cmd);
    }

    @ApiOperation("创建备案草稿")
    @PostMapping("/draft")
    public SingleResponse<QualFilingCO> draft(
            @RequestParam(defaultValue = "1") Integer applyTypeCode) {
        return qualFilingApi.draft(applyTypeCode);
    }

    @ApiOperation("暂存备案信息")
    @PostMapping("/save-draft")
    public SingleResponse<QualFilingCO> saveDraft(@Validated @RequestBody QualFilingModifyCmd cmd) {
        return qualFilingApi.saveDraft(cmd);
    }

    @ApiOperation("提交备案审核")
    @PostMapping("/submit")
    public SingleResponse<QualFilingCO> submit(@RequestParam Long id) {
        return qualFilingApi.submit(id);
    }

    @ApiOperation("审核通过")
    @PostMapping("/approve")
    public SingleResponse<QualFilingCO> approve(@RequestParam Long id) {
        return qualFilingApi.approve(id);
    }

    @ApiOperation("审核打回")
    @PostMapping("/reject")
    public SingleResponse<QualFilingCO> reject(@Validated @RequestBody QualFilingRejectCmd cmd) {
        return qualFilingApi.reject(cmd);
    }

    @ApiOperation("备案详情（5步聚合）")
    @GetMapping("/detail")
    public SingleResponse<QualFilingDetailCO> detail(@RequestParam Long id) {
        return qualFilingApi.detail(id);
    }

    @ApiOperation("上传主表附件")
    @PostMapping("/upload-attachment")
    public SingleResponse<QualFilingCO> uploadAttachment(@Validated @RequestBody QualFilingAttachmentUploadCmd cmd) {
        return qualFilingApi.uploadAttachment(cmd);
    }

    @ApiOperation("已备案资质填报-创建草稿")
    @PostMapping("/filed/draft")
    public SingleResponse<QualFilingCO> filedDraft() {
        return qualFilingApi.filedDraft();
    }

    @ApiOperation("已备案资质管理-分页")
    @GetMapping("/filed/page")
    public PageResponse<QualFilingCO> filedPage(@Validated QualFilingPageQuery query) {
        query.setApplyTypeCode(QualFilingApplyTypeEnum.FILED_MANAGE.getCode());
        applyDefaultStatusCodes(query, Arrays.asList(
                QualFilingStatusEnum.FILED.getCode(),
                QualFilingStatusEnum.REVIEWING.getCode()));
        return qualFilingApi.page(query);
    }

    @ApiOperation("备案变更管理-分页")
    @GetMapping("/change/page")
    public PageResponse<QualFilingCO> changePage(@Validated QualFilingPageQuery query) {
        query.setScene("change");
        applyDefaultStatusCodes(query, Arrays.asList(
                QualFilingStatusEnum.FILED.getCode(),
                QualFilingStatusEnum.CHANGE_REVIEWING.getCode()));
        return qualFilingApi.page(query);
    }

    private void applyDefaultStatusCodes(QualFilingPageQuery query, List<Integer> defaults) {
        if (query.getFilingStatusCode() == null
                && (query.getFilingStatusCodes() == null || query.getFilingStatusCodes().isEmpty())) {
            query.setFilingStatusCodes(defaults);
        }
    }

    @ApiOperation("查询资质备案申请详情")
    @GetMapping("/get")
    public SingleResponse<QualFilingCO> get(@RequestParam Long id) {
        return qualFilingApi.get(id);
    }

    @ApiOperation("修改资质备案申请")
    @PostMapping("/modify")
    public SingleResponse<QualFilingCO> modify(@Validated @RequestBody QualFilingModifyCmd cmd) {
        return qualFilingApi.modify(cmd);
    }

    @ApiOperation("删除资质备案申请")
    @PostMapping("/delete")
    public SingleResponse<Void> delete(@RequestParam Long id) {
        return qualFilingApi.delete(id);
    }

    @ApiOperation("分页查询资质备案申请")
    @GetMapping("/page")
    public PageResponse<QualFilingCO> page(@Validated QualFilingPageQuery query) {
        return qualFilingApi.page(query);
    }

    @ApiOperation("合规检查")
    @GetMapping("/compliance-check")
    public SingleResponse<List<QualFilingComplianceCheckResultCO>> complianceCheck(@RequestParam Long id) {
        return qualFilingApi.complianceCheck(id);
    }
}
