"use strict";
/**
 * Validate command
 * Validates workflow files
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateCommand = validateCommand;
const path = __importStar(require("path"));
const fs = __importStar(require("fs"));
const chalk_1 = __importDefault(require("chalk"));
const ora_1 = __importDefault(require("ora"));
const workflow_parser_1 = require("../../parser/workflow-parser");
const workflow_engine_1 = require("../../engine/workflow-engine");
async function validateCommand(workflowPath, options) {
    const spinner = (0, ora_1.default)();
    try {
        const absolutePath = path.resolve(process.cwd(), workflowPath);
        // Check if path exists
        if (!fs.existsSync(absolutePath)) {
            console.error(chalk_1.default.red('✗ Error:'), `Path not found: ${workflowPath}`);
            process.exit(1);
        }
        const parser = new workflow_parser_1.WorkflowParser();
        const engine = new workflow_engine_1.WorkflowEngine();
        if (options.directory || fs.statSync(absolutePath).isDirectory()) {
            // Validate directory
            spinner.start('Validating workflows in directory...');
            const parseResults = await parser.parseDirectory(absolutePath);
            spinner.succeed(chalk_1.default.green(`✓ Found ${parseResults.length} workflow(s)`));
            let totalErrors = 0;
            let totalWarnings = 0;
            for (const parseResult of parseResults) {
                const workflow = parseResult.workflow;
                console.log();
                console.log(chalk_1.default.bold(`${workflow.workflow.id}:`));
                const validation = engine.validate(workflow);
                if (validation.valid) {
                    console.log(chalk_1.default.green(`  ✓ Valid`));
                }
                else {
                    console.log(chalk_1.default.red(`  ✗ Invalid`));
                }
                if (validation.errors.length > 0) {
                    console.log(chalk_1.default.red(`  Errors:`));
                    validation.errors.forEach(e => console.log(chalk_1.default.red(`    - ${e}`)));
                    totalErrors += validation.errors.length;
                }
                if (validation.warnings.length > 0) {
                    console.log(chalk_1.default.yellow(`  Warnings:`));
                    validation.warnings.forEach(w => console.log(chalk_1.default.yellow(`    - ${w}`)));
                    totalWarnings += validation.warnings.length;
                }
            }
            console.log();
            console.log(chalk_1.default.bold('Summary:'));
            console.log(`  Workflows: ${parseResults.length}`);
            console.log(chalk_1.default.red(`  Errors: ${totalErrors}`));
            console.log(chalk_1.default.yellow(`  Warnings: ${totalWarnings}`));
            if (totalErrors > 0) {
                process.exit(1);
            }
        }
        else {
            // Validate single file
            spinner.start('Validating workflow...');
            const parseResult = await parser.parseFile(absolutePath);
            const workflow = parseResult.workflow;
            const validation = engine.validate(workflow);
            if (validation.valid) {
                spinner.succeed(chalk_1.default.green('✓ Workflow is valid'));
            }
            else {
                spinner.fail(chalk_1.default.red('✗ Workflow is invalid'));
            }
            console.log();
            console.log(chalk_1.default.bold(`${workflow.workflow.name} (${workflow.workflow.id}):`));
            console.log(chalk_1.default.dim(`  Version: ${workflow.workflow.version}`));
            console.log(chalk_1.default.dim(`  Stack components: ${Object.keys(workflow.stack).length}`));
            if (validation.errors.length > 0) {
                console.log();
                console.log(chalk_1.default.red('Errors:'));
                validation.errors.forEach(e => console.log(chalk_1.default.red(`  - ${e}`)));
            }
            if (validation.warnings.length > 0) {
                console.log();
                console.log(chalk_1.default.yellow('Warnings:'));
                validation.warnings.forEach(w => console.log(chalk_1.default.yellow(`  - ${w}`)));
            }
            if (!validation.valid) {
                process.exit(1);
            }
        }
    }
    catch (error) {
        spinner.fail(chalk_1.default.red('✗ Validation failed'));
        if (error instanceof Error) {
            console.error(chalk_1.default.red('Error:'), error.message);
        }
        else {
            console.error(chalk_1.default.red('Error:'), String(error));
        }
        process.exit(1);
    }
}
//# sourceMappingURL=validate.js.map