package com.qqchen.deploy.backend.workflow.delegate;

import com.qqchen.deploy.backend.workflow.constants.WorkFlowConstants;
import com.qqchen.deploy.backend.workflow.dto.definition.node.localVariables.ApprovalNodeLocalVariables;
import com.qqchen.deploy.backend.workflow.dto.definition.node.panelVariables.ApprovalNodePanelVariables;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.TaskService;
import org.flowable.engine.delegate.BpmnError;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.task.api.Task;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * 审批节点的委派者实现
 */
@Slf4j
@Component
public class ApprovalNodeDelegate extends BaseNodeDelegate<ApprovalNodePanelVariables, ApprovalNodeLocalVariables> {

    @Resource
    private TaskService taskService;

    @Override
    protected Class<ApprovalNodePanelVariables> getPanelVariablesClass() {
        return ApprovalNodePanelVariables.class;
    }

    @Override
    protected Class<ApprovalNodeLocalVariables> getLocalVariablesClass() {
        return ApprovalNodeLocalVariables.class;
    }

    @Override
    protected void executeInternal(DelegateExecution execution, ApprovalNodePanelVariables panelVariables, ApprovalNodeLocalVariables localVariables) {
        if (panelVariables == null) {
            throw new BpmnError(WorkFlowConstants.WORKFLOW_EXEC_ERROR, "Panel variables are required but not provided");
        }

        try {
            // 创建用户任务
            Task task = taskService.createTaskQuery()
                .processInstanceId(execution.getProcessInstanceId())
                .taskDefinitionKey(execution.getCurrentActivityId())
                .singleResult();

            if (task == null) {
                throw new BpmnError(WorkFlowConstants.WORKFLOW_EXEC_ERROR, "Failed to create approval task");
            }

            // 设置任务属性
            task.setName(panelVariables.getName());
            task.setDescription(panelVariables.getDescription());

            // 设置审批人
            if (panelVariables.getAssignee() != null) {
                task.setAssignee(panelVariables.getAssignee());
            }

            // 设置候选组
            if (panelVariables.getCandidateGroup() != null) {
                taskService.addCandidateGroup(task.getId(), panelVariables.getCandidateGroup());
            }

            // 设置超时时间
            if (panelVariables.getTimeoutHours() != null) {
                Date dueDate = java.sql.Timestamp.valueOf(LocalDateTime.now().plusHours(panelVariables.getTimeoutHours()));
                task.setDueDate(dueDate);
            }

            // 更新任务
            taskService.saveTask(task);

            log.info("Created approval task: {}, assignee: {}, candidateGroup: {}", 
                task.getId(), panelVariables.getAssignee(), panelVariables.getCandidateGroup());

        } catch (Exception e) {
            log.error("Failed to create approval task", e);
            throw new BpmnError(WorkFlowConstants.WORKFLOW_EXEC_ERROR, "Failed to create approval task: " + e.getMessage());
        }
    }
} 