package com.fs.audit.event;

import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 审核事件监听器
 * 处理审核相关的事件通知
 */
@Slf4j
@Component
public class AuditEventListener {

    /**
     * 处理审核创建事件
     */
    @Async
    @EventListener
    public void handleAuditCreated(AuditCreatedEvent event) {
        log.info("收到审核创建事件，审核ID: {}, 业务ID: {}",
                event.getAudit().getId(),
                event.getAudit().getBusinessId());

        // 获取第一步审核人列表
        List<Long> auditUserIds = JSON.parseArray(
                event.getFirstFlow().getAuditUserIds(), Long.class);

        // TODO: 发送通知给审核人（邮件、短信、站内信等）
        log.info("通知审核人: {}, 步骤: {}",
                auditUserIds,
                event.getFirstFlow().getStepDescription());
    }

    /**
     * 处理审核步骤变更事件
     */
    @Async
    @EventListener
    public void handleAuditStepChanged(AuditStepChangedEvent event) {
        log.info("收到步骤变更事件，审核ID: {}, 新步骤: {}",
                event.getAudit().getId(),
                event.getNewFlow().getStepNumber());

        // 获取新步骤审核人列表
        List<Long> auditUserIds = JSON.parseArray(
                event.getNewFlow().getAuditUserIds(), Long.class);

        // TODO: 发送通知给新的审核人
        log.info("通知新审核人: {}, 步骤: {}",
                auditUserIds,
                event.getNewFlow().getStepDescription());
    }

    /**
     * 处理审核完成事件
     */
    @Async
    @EventListener
    public void handleAuditCompleted(AuditCompletedEvent event) {
        String status = event.isApproved() ? "通过" : "驳回";
        log.info("收到审核完成事件，审核ID: {}, 业务ID: {}, 结果: {}",
                event.getAudit().getId(),
                event.getAudit().getBusinessId(),
                status);

        // TODO: 发送通知给发起人
        log.info("通知发起人: {}, 审核结果: {}",
                event.getAudit().getInitiatorId(),
                status);

        // TODO: 回调业务系统更新状态
    }
}

