/**
 * Scale Assessment Scanner
 *
 * Orchestrates load testing, bottleneck detection, and capacity estimation.
 * This is the entry point for M8 scale assessment capabilities.
 *
 * @module scanners/scale
 */
import { logger } from "../../logger.js";
import { detectLoadTool, discoverProfiles, runK6Test, } from "./load-profiler.js";
import { detectBottlenecks, calculateBottleneckScore, } from "./bottleneck-detector.js";
import { estimateCapacity, calculateCapacityScore, } from "./capacity-estimator.js";
export * from "./types.js";
export { detectLoadTool, discoverProfiles, runK6Test, generateSampleProfile, loadProfile, isK6Available, } from "./load-profiler.js";
export { detectBottlenecks, calculateBottleneckScore, } from "./bottleneck-detector.js";
export { estimateCapacity, calculateCapacityScore, } from "./capacity-estimator.js";
/**
 * Run full scale assessment
 *
 * 1. Detect load testing tool
 * 2. Discover and run load profiles
 * 3. Detect bottlenecks
 * 4. Estimate capacity
 * 5. Calculate scores
 */
export async function runScaleAssessment(projectPath, baseUrl, options = {}) {
    const startTime = Date.now();
    const { timeout = 600000, profileName, skipLoadTest = false, targetConcurrent = 100 } = options;
    logger.info("scale.assessment_started", { projectPath, baseUrl });
    let loadTestResult;
    // Step 1: Run load tests if not skipped
    if (!skipLoadTest) {
        // Check for load testing tools
        const tool = await detectLoadTool();
        if (!tool) {
            logger.warn("scale.no_load_tool", {
                message: "No load testing tool found. Install k6 or Artillery.",
            });
        }
        else {
            // Discover profiles
            const profiles = await discoverProfiles(projectPath);
            if (profiles.length === 0) {
                logger.info("scale.no_profiles_found", {
                    message: "No load profiles found. Generate one with scale_profile_generate.",
                });
            }
            else {
                // Find matching profile or use first
                const selectedProfile = profileName
                    ? profiles.find((p) => p.profile.name === profileName)
                    : profiles[0];
                if (selectedProfile && tool === "k6") {
                    loadTestResult = await runK6Test(selectedProfile.profile, baseUrl, {
                        timeout,
                    });
                }
            }
        }
    }
    // Step 2: Detect bottlenecks
    const bottlenecks = await detectBottlenecks(projectPath, loadTestResult);
    const bottleneckScore = calculateBottleneckScore(bottlenecks);
    // Step 3: Estimate capacity
    const capacity = await estimateCapacity(loadTestResult, bottlenecks);
    const capacityScore = calculateCapacityScore(capacity, targetConcurrent);
    // Step 4: Calculate overall score
    const loadTestScore = loadTestResult?.score ?? 50; // Neutral if no test
    const overallScore = Math.round((loadTestScore * 0.4) + (bottleneckScore * 0.35) + (capacityScore * 0.25));
    const result = {
        success: overallScore >= 70,
        loadTest: loadTestResult,
        bottlenecks,
        capacity,
        score: {
            loadTestScore,
            bottleneckScore,
            capacityScore,
            overallScore,
        },
        duration: Date.now() - startTime,
    };
    logger.info("scale.assessment_completed", {
        success: result.success,
        overallScore,
        loadTestScore,
        bottleneckScore,
        capacityScore,
        duration: result.duration,
    });
    return result;
}
/**
 * Quick bottleneck scan (no load testing)
 */
export async function quickBottleneckScan(projectPath) {
    const bottlenecks = await detectBottlenecks(projectPath);
    const score = calculateBottleneckScore(bottlenecks);
    return { bottlenecks, score };
}
//# sourceMappingURL=index.js.map