// job-application-workflow.ts
import { UserBasicInfoTask } from './task.job';
import { JobIntentionTask } from './task.job';
import { UserInterestsTask } from './task.job';
import { FlowDisplayComponent } from '../../lib/flow-display/flow-display.component';
import { FlowExecutor } from '../../lib/flow.executor';

export class JobApplicationWorkflow {
    private executor: any | FlowExecutor;
    private modalCtrl: any;

    constructor(modalCtrl: any) {
        this.modalCtrl = modalCtrl;
        this.executor = new FlowExecutor();
    }

    async startWorkflow() {
        // 创建工作流
        const workflow = {
            title: '求职信息收集',
            desc: '请填写您的求职相关信息',
            taskList: [
                new UserBasicInfoTask(this.modalCtrl),
                new JobIntentionTask(this.modalCtrl),
                new UserInterestsTask(this.modalCtrl)
            ]
        };

        this.executor.setWorkflow(workflow);

        // 打开工作流展示模态框
        const modal = await this.modalCtrl.create({
            component: FlowDisplayComponent, // 假设有工作流展示组件
            componentProps: { executor: this.executor }
        });

        await modal.present();

        try {
            await this.executor.start();
            console.log('工作流完成，收集到的数据:', this.getCollectedData());
            return this.getCollectedData();
        } catch (error) {
            console.error('工作流执行出错:', error);
            throw error;
        }
    }

    getCollectedData() {
        if (!this.executor?.isCompleted) {
            return null;
        }

        // 合并所有任务的数据
        return this.executor?.taskList.reduce((result: any, task: any) => {
            return { ...result, ...task.output };
        }, {});
    }
}