/**
 * Integration Test for Approval Comments Field Migration
 */

const migrateHandler = require('./migrateApprovalCommentsField');
const rollbackHandler = require('./rollbackApprovalCommentsField');

module.exports = {
    rest: {
        method: 'GET',
        fullPath: '/api/workflow/upgrade/test-approval-comments'
    },
    async handler(ctx) {
        const startTime = Date.now();
        const testResults = {
            success: false,
            environment: process.env.NODE_ENV || 'development',
            summary: { totalTests: 0, passed: 0, failed: 0, skipped: 0 },
            tests: [],
            executionTime: null,
            errors: []
        };

        try {
            // Test 1: Environment Check
            testResults.tests.push({ name: '环境检查', status: 'running', startTime: Date.now() });
            
            const isProd = testResults.environment === 'production';
            if (isProd) {
                testResults.tests[0].status = 'skipped';
                testResults.tests[0].message = '生产环境禁止运行集成测试';
                testResults.summary.skipped++;
                testResults.summary.totalTests++;
                ctx.meta.$statusCode = 403;
                return { ...testResults, executionTime: `${((Date.now() - startTime) / 1000).toFixed(2)}s` };
            }

            testResults.tests[0].status = 'passed';
            testResults.tests[0].message = '测试环境验证通过';
            testResults.summary.passed++;
            testResults.summary.totalTests++;

            // Test 2: Migration
            testResults.tests.push({ name: '执行迁移操作', status: 'running', startTime: Date.now() });
            try {
                const migrateResult = await migrateHandler.handler(ctx);
                if (migrateResult.totalForms === 0) {
                    testResults.tests[1].status = 'skipped';
                    testResults.tests[1].message = '没有找到需要迁移的表单';
                    testResults.summary.skipped++;
                } else {
                    testResults.tests[1].status = 'passed';
                    testResults.tests[1].message = `成功处理 ${migrateResult.totalForms} 个表单, ${migrateResult.totalFields || 0} 个字段`;
                    testResults.summary.passed++;
                }
                testResults.summary.totalTests++;
            } catch (error) {
                testResults.tests[1].status = 'failed';
                testResults.tests[1].message = `迁移失败: ${error.message}`;
                testResults.summary.failed++;
                testResults.summary.totalTests++;
            }

            // Test 3: Rollback
            testResults.tests.push({ name: '执行回滚操作', status: 'running', startTime: Date.now() });
            try {
                const rollbackResult = await rollbackHandler.handler(ctx);
                if (rollbackResult.totalForms === 0) {
                    testResults.tests[2].status = 'skipped';
                    testResults.tests[2].message = '没有找到需要回滚的表单';
                    testResults.summary.skipped++;
                } else {
                    testResults.tests[2].status = 'passed';
                    testResults.tests[2].message = `成功回滚 ${rollbackResult.totalForms} 个表单, ${rollbackResult.totalFields || 0} 个字段`;
                    testResults.summary.passed++;
                }
                testResults.summary.totalTests++;
            } catch (error) {
                testResults.tests[2].status = 'failed';
                testResults.tests[2].message = `回滚失败: ${error.message}`;
                testResults.summary.failed++;
                testResults.summary.totalTests++;
            }

            testResults.success = testResults.summary.failed === 0;
            testResults.executionTime = `${((Date.now() - startTime) / 1000).toFixed(2)}s`;
            return testResults;

        } catch (error) {
            testResults.errors.push(error.message);
            testResults.executionTime = `${((Date.now() - startTime) / 1000).toFixed(2)}s`;
            ctx.meta.$statusCode = 500;
            return testResults;
        }
    }
};
