package cn.axzo.msg.center.message.controller;

import cn.axzo.basics.common.util.AssertUtil;
import cn.axzo.msg.center.message.domain.param.MessageTemplateSaveOrUpdateParam;
import cn.axzo.msg.center.message.domain.param.RelationTemplateMapInitParam;
import cn.axzo.msg.center.message.domain.vo.RelationTemplateMapInitRequest;
import cn.axzo.msg.center.message.service.MessageTemplateNewService;
import cn.axzo.msg.center.message.service.MessageTemplateNewSyncService;
import cn.axzo.msg.center.message.service.RelationTemplateMapService;
import cn.axzo.msg.center.service.template.client.MessageTemplateClient;
import cn.axzo.msg.center.service.template.request.MessageTemplateCreateRequest;
import cn.axzo.msg.center.service.template.request.MessageTemplatePageRequest;
import cn.axzo.msg.center.service.template.request.MessageTemplateSyncDto;
import cn.axzo.msg.center.service.template.request.MessageTemplateSyncQueryRequest;
import cn.axzo.msg.center.service.template.request.MessageTemplateUpdateRequest;
import cn.axzo.msg.center.service.template.request.MessageTemplateUpdateStatusRequest;
import cn.axzo.msg.center.service.template.response.MessageDetailStyle;
import cn.axzo.msg.center.service.template.response.MessageTemplateDetailResponse;
import cn.axzo.msg.center.service.template.response.MessageTemplatePageResponse;
import cn.azxo.framework.common.model.CommonResponse;
import cn.azxo.framework.common.model.Page;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Collection;
import java.util.List;

/**
 * 消息模板管理
 *
 * @author cold_blade
 * @date 2023/10/5
 * @version 1.0
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class MessageTemplateController implements MessageTemplateClient {

    private final MessageTemplateNewService messageTemplateNewService;
    private final RelationTemplateMapService relationTemplateMapService;
    private final MessageTemplateNewSyncService messageTemplateNewSyncService;

    @Override
    public CommonResponse<String> save(MessageTemplateCreateRequest request) {
        return CommonResponse.success(
                messageTemplateNewService.createTemplate(MessageTemplateSaveOrUpdateParam.from(request)));
    }

    @Override
    public CommonResponse<Void> update(MessageTemplateUpdateRequest request) {
        messageTemplateNewService.updateTemplate(MessageTemplateSaveOrUpdateParam.from(request));
        return CommonResponse.success();
    }

    @Override
    public CommonResponse<MessageTemplateDetailResponse> detail(String templateCode) {
        return CommonResponse.success(messageTemplateNewService.detail(templateCode));
    }

    @Override
    public CommonResponse<Page<MessageTemplatePageResponse>> page(MessageTemplatePageRequest request) {
        return CommonResponse.success(messageTemplateNewService.page(request));
    }

    @Override
    public CommonResponse<List<MessageTemplatePageResponse>> listByCodes(Collection<String> templateCodes) {
        return CommonResponse.success(messageTemplateNewService.listByCodes(templateCodes));
    }

    @Override
    public CommonResponse<Void> updateStatus(MessageTemplateUpdateStatusRequest request) {
        messageTemplateNewService.updateStatus(request.getOperatorId(), request.getTemplateCode(), request.getStatus());
        return CommonResponse.success();
    }

    @Override
    public CommonResponse<MessageTemplateSyncDto> getSyncMessageTemplate(
            MessageTemplateSyncQueryRequest request) {
        MessageTemplateDetailResponse detail = messageTemplateNewService.querySyncMessageTemplate(request.getTemplateCode());
        AssertUtil.notNull(detail, "消息模板不存在");
        MessageTemplateSyncDto result = BeanUtil.copyProperties(detail, MessageTemplateSyncDto.class);
        result.setTemplateCode(request.getTemplateCode());
        result.setEnv(request.getEnv());
        return CommonResponse.success(result);
    }

    @Override
    public CommonResponse<List<MessageDetailStyle>> listTemplateDetailStyles() {
        List<MessageDetailStyle> styles = messageTemplateNewService.listTemplateDetailStyles();
        return CommonResponse.success(styles);
    }

    @Override
    public CommonResponse<Void> syncTemplate(MessageTemplateSyncDto param) {
        log.info("syncTemplate param= [{}]", JSONUtil.toJsonStr(param));
        messageTemplateNewSyncService.syncTemplate(param);
        return CommonResponse.success();
    }

    @PostMapping(value = "/message/template/relation/init", produces = {MediaType.APPLICATION_JSON_VALUE})
    public CommonResponse<Void> initRelationTemplateMap(@RequestBody @Valid RelationTemplateMapInitRequest request) {
        RelationTemplateMapInitParam param = request.toInitParam();
        relationTemplateMapService.init(param);
        return CommonResponse.success();
    }
}
