/**
 * Install command for Night Watch CLI
 * Adds crontab entries for automated PRD execution
 */
import { execSync } from 'child_process';
import * as path from 'path';
import * as fs from 'fs';
import { LOG_DIR, MANAGER_LOG_NAME, dim, generateMarker, getEntries, getProjectEntries, getProjectName, header, loadConfig, readCrontab, registerProject, success, error as uiError, warn, writeCrontab, } from '@night-watch/core';
/**
 * Safely quote a value for POSIX shell commands.
 */
function shellQuote(value) {
    return `'${value.replace(/'/g, "'\"'\"'")}'`;
}
/**
 * Get the path to the night-watch binary
 */
function getNightWatchBinPath() {
    // Try to find night-watch in npm global bin
    try {
        const npmBin = execSync('npm bin -g', { encoding: 'utf-8' }).trim();
        const binPath = path.join(npmBin, 'night-watch');
        if (fs.existsSync(binPath)) {
            return binPath;
        }
    }
    catch {
        // Ignore error, fall back to which
    }
    // Try which command
    try {
        return execSync('which night-watch', { encoding: 'utf-8' }).trim();
    }
    catch {
        // Fall back to assuming it's in PATH
        return 'night-watch';
    }
}
/**
 * Get the directory containing the node binary.
 * Cron runs with a minimal PATH that typically doesn't include nvm/fnm/volta paths,
 * so we need to explicitly add the node bin directory to each cron entry.
 */
function getNodeBinDir() {
    // process.execPath is the node binary that ran this install command — stable even
    // when invoked via yarn (which sets a temp /tmp/yarn-* dir in PATH that gets wiped
    // on WSL restart). Falls back to `which node` for edge cases.
    if (process.execPath && process.execPath !== 'node') {
        return path.dirname(process.execPath);
    }
    try {
        const nodePath = execSync('which node', { encoding: 'utf-8' }).trim();
        return path.dirname(nodePath);
    }
    catch {
        return '';
    }
}
/**
 * Build PATH export for cron entries using relevant binary directories.
 */
export function buildCronPathPrefix(nodeBinDir, nightWatchBin) {
    const nightWatchBinDir = nightWatchBin.includes('/') || nightWatchBin.includes('\\') ? path.dirname(nightWatchBin) : '';
    const pathParts = Array.from(new Set([nodeBinDir, nightWatchBinDir].filter((part) => part.length > 0)));
    if (pathParts.length === 0) {
        return '';
    }
    return `export PATH="${pathParts.join(':')}:$PATH" && `;
}
/**
 * Apply minute offset to a cron schedule expression.
 * Replaces the minute field (first field) with the offset value.
 * Only applies if the minute field is a single number (e.g. "0").
 * Complex expressions (e.g. star-slash, comma-separated) are left unchanged.
 */
export function applyScheduleOffset(schedule, offset) {
    if (offset === 0)
        return schedule;
    const parts = schedule.split(/\s+/);
    if (parts.length < 5)
        return schedule;
    // Only replace if minute field is a plain number
    if (/^\d+$/.test(parts[0])) {
        parts[0] = String(offset);
        return parts.join(' ');
    }
    return schedule;
}
/**
 * Core install logic, reusable from dashboard.
 * Returns result without printing to console.
 */
export function performInstall(projectDir, config, options) {
    try {
        const executorSchedule = options?.schedule || config.cronSchedule;
        const reviewerSchedule = options?.reviewerSchedule || config.reviewerSchedule;
        const nightWatchBin = getNightWatchBinPath();
        const projectName = getProjectName(projectDir);
        const marker = generateMarker(projectName);
        const logDir = path.join(projectDir, LOG_DIR);
        if (!fs.existsSync(logDir)) {
            fs.mkdirSync(logDir, { recursive: true });
        }
        const executorLog = path.join(logDir, 'executor.log');
        const reviewerLog = path.join(logDir, 'reviewer.log');
        // Check if already installed (unless force)
        if (!options?.force) {
            const existingEntries = Array.from(new Set([...getEntries(marker), ...getProjectEntries(projectDir)]));
            if (existingEntries.length > 0) {
                registerProject(projectDir);
                return {
                    success: false,
                    entries: existingEntries,
                    error: 'Already installed. Uninstall first or use force.',
                };
            }
        }
        const entries = [];
        const nodeBinDir = getNodeBinDir();
        const pathPrefix = buildCronPathPrefix(nodeBinDir, nightWatchBin);
        const cliBinPrefix = `export NW_CLI_BIN=${shellQuote(nightWatchBin)} && `;
        const cronTriggerPrefix = 'export NW_CRON_TRIGGER=1 && ';
        let providerEnvPrefix = '';
        if (config.providerEnv && Object.keys(config.providerEnv).length > 0) {
            const exports = Object.entries(config.providerEnv)
                .map(([key, value]) => `export ${key}=${shellQuote(value)}`)
                .join(' && ');
            providerEnvPrefix = exports + ' && ';
        }
        const installExecutor = config.executorEnabled !== false;
        if (installExecutor) {
            const executorEntry = `${executorSchedule} ${pathPrefix}${providerEnvPrefix}${cliBinPrefix}${cronTriggerPrefix}cd ${shellQuote(projectDir)} && ${shellQuote(nightWatchBin)} run >> ${shellQuote(executorLog)} 2>&1  ${marker}`;
            entries.push(executorEntry);
        }
        const installReviewer = options?.noReviewer === true ? false : config.reviewerEnabled;
        if (installReviewer) {
            const reviewerEntry = `${reviewerSchedule} ${pathPrefix}${providerEnvPrefix}${cliBinPrefix}${cronTriggerPrefix}cd ${shellQuote(projectDir)} && ${shellQuote(nightWatchBin)} review >> ${shellQuote(reviewerLog)} 2>&1  ${marker}`;
            entries.push(reviewerEntry);
        }
        // Planner entry (uses roadmap scanner schedule; noSlicer flag kept for backward compatibility)
        const installSlicer = options?.noSlicer === true ? false : config.roadmapScanner.enabled;
        if (installSlicer) {
            const slicerSchedule = config.roadmapScanner.slicerSchedule;
            const slicerLog = path.join(logDir, 'slicer.log');
            const slicerEntry = `${slicerSchedule} ${pathPrefix}${providerEnvPrefix}${cliBinPrefix}${cronTriggerPrefix}cd ${shellQuote(projectDir)} && ${shellQuote(nightWatchBin)} planner >> ${shellQuote(slicerLog)} 2>&1  ${marker}`;
            entries.push(slicerEntry);
        }
        // QA entry (if enabled and noQa not set)
        const disableQa = options?.noQa === true || options?.qa === false;
        const installQa = disableQa ? false : config.qa.enabled;
        if (installQa) {
            const qaSchedule = config.qa.schedule;
            const qaLog = path.join(logDir, 'qa.log');
            const qaEntry = `${qaSchedule} ${pathPrefix}${providerEnvPrefix}${cliBinPrefix}${cronTriggerPrefix}cd ${shellQuote(projectDir)} && ${shellQuote(nightWatchBin)} qa >> ${shellQuote(qaLog)} 2>&1  ${marker}`;
            entries.push(qaEntry);
        }
        // Audit entry (if enabled and noAudit not set)
        const disableAudit = options?.noAudit === true || options?.audit === false;
        const installAudit = disableAudit ? false : config.audit.enabled;
        if (installAudit) {
            const auditSchedule = config.audit.schedule;
            const auditLog = path.join(logDir, 'audit.log');
            const auditEntry = `${auditSchedule} ${pathPrefix}${providerEnvPrefix}${cliBinPrefix}${cronTriggerPrefix}cd ${shellQuote(projectDir)} && ${shellQuote(nightWatchBin)} audit >> ${shellQuote(auditLog)} 2>&1  ${marker}`;
            entries.push(auditEntry);
        }
        // Optimizer entry (if enabled and noOptimizer not set)
        const disableOptimizer = options?.noOptimizer === true || options?.optimizer === false;
        const installOptimizer = disableOptimizer ? false : config.optimizer.enabled;
        if (installOptimizer) {
            const optimizerSchedule = config.optimizer.schedule;
            const optimizerLog = path.join(logDir, 'optimizer.log');
            const optimizerEntry = `${optimizerSchedule} ${pathPrefix}${providerEnvPrefix}${cliBinPrefix}${cronTriggerPrefix}cd ${shellQuote(projectDir)} && ${shellQuote(nightWatchBin)} optimize >> ${shellQuote(optimizerLog)} 2>&1  ${marker}`;
            entries.push(optimizerEntry);
        }
        // UX entry (if enabled and noUx not set)
        const disableUx = options?.noUx === true || options?.ux === false;
        const installUx = disableUx ? false : config.ux.enabled;
        if (installUx) {
            const uxSchedule = config.ux.schedule;
            const uxLog = path.join(logDir, 'ux.log');
            const uxEntry = `${uxSchedule} ${pathPrefix}${providerEnvPrefix}${cliBinPrefix}${cronTriggerPrefix}cd ${shellQuote(projectDir)} && ${shellQuote(nightWatchBin)} ux >> ${shellQuote(uxLog)} 2>&1  ${marker}`;
            entries.push(uxEntry);
        }
        // Analytics entry (if enabled and noAnalytics not set)
        const disableAnalytics = options?.noAnalytics === true || options?.analytics === false;
        const installAnalytics = disableAnalytics ? false : config.analytics.enabled;
        if (installAnalytics) {
            const analyticsSchedule = config.analytics.schedule;
            const analyticsLog = path.join(logDir, 'analytics.log');
            const analyticsEntry = `${analyticsSchedule} ${pathPrefix}${providerEnvPrefix}${cliBinPrefix}${cronTriggerPrefix}cd ${shellQuote(projectDir)} && ${shellQuote(nightWatchBin)} analytics >> ${shellQuote(analyticsLog)} 2>&1  ${marker}`;
            entries.push(analyticsEntry);
        }
        // PR Resolver entry (if enabled and noPrResolver not set)
        const disablePrResolver = options?.noPrResolver === true || options?.prResolver === false;
        const installPrResolver = disablePrResolver ? false : config.prResolver.enabled;
        if (installPrResolver) {
            const prResolverSchedule = config.prResolver.schedule;
            const prResolverLog = path.join(logDir, 'pr-resolver.log');
            const prResolverEntry = `${prResolverSchedule} ${pathPrefix}${providerEnvPrefix}${cliBinPrefix}${cronTriggerPrefix}cd ${shellQuote(projectDir)} && ${shellQuote(nightWatchBin)} resolve >> ${shellQuote(prResolverLog)} 2>&1  ${marker}`;
            entries.push(prResolverEntry);
        }
        // Merger entry (if enabled and noMerger not set)
        const disableMerger = options?.noMerger === true || options?.merger === false;
        const installMerger = disableMerger ? false : (config.merger?.enabled ?? false);
        if (installMerger) {
            const mergerSchedule = config.merger.schedule;
            const mergerLog = path.join(logDir, 'merger.log');
            const mergerEntry = `${mergerSchedule} ${pathPrefix}${providerEnvPrefix}${cliBinPrefix}${cronTriggerPrefix}cd ${shellQuote(projectDir)} && ${shellQuote(nightWatchBin)} merge >> ${shellQuote(mergerLog)} 2>&1  ${marker}`;
            entries.push(mergerEntry);
        }
        // Manager entry (if enabled and noManager not set)
        const disableManager = options?.noManager === true || options?.manager === false;
        const installManager = disableManager ? false : (config.manager?.enabled ?? false);
        if (installManager) {
            const managerSchedule = config.manager.schedule;
            const managerLog = path.join(logDir, `${MANAGER_LOG_NAME}.log`);
            const managerEntry = `${managerSchedule} ${pathPrefix}${providerEnvPrefix}${cliBinPrefix}${cronTriggerPrefix}cd ${shellQuote(projectDir)} && ${shellQuote(nightWatchBin)} manager >> ${shellQuote(managerLog)} 2>&1  ${marker}`;
            entries.push(managerEntry);
        }
        const existingEntries = new Set(Array.from(new Set([...getEntries(marker), ...getProjectEntries(projectDir)])));
        const currentCrontab = readCrontab();
        const baseCrontab = options?.force
            ? currentCrontab.filter((line) => !existingEntries.has(line) && !line.includes(marker))
            : currentCrontab;
        const newCrontab = [...baseCrontab, ...entries];
        writeCrontab(newCrontab);
        registerProject(projectDir);
        return { success: true, entries };
    }
    catch (err) {
        return {
            success: false,
            entries: [],
            error: err instanceof Error ? err.message : String(err),
        };
    }
}
/**
 * Install crontab entries for night-watch
 */
export function installCommand(program) {
    program
        .command('install')
        .description('Add crontab entries for automated execution')
        .option('-s, --schedule <cron>', 'Cron schedule for PRD executor')
        .option('--reviewer-schedule <cron>', 'Cron schedule for reviewer')
        .option('--no-reviewer', 'Skip installing reviewer cron')
        .option('--no-slicer', 'Skip installing slicer cron')
        .option('--no-qa', 'Skip installing QA cron')
        .option('--no-audit', 'Skip installing audit cron')
        .option('--no-optimizer', 'Skip installing optimizer cron')
        .option('--no-ux', 'Skip installing UX cron')
        .option('--no-analytics', 'Skip installing analytics cron')
        .option('--no-pr-resolver', 'Skip installing PR resolver cron')
        .option('--no-merger', 'Skip installing merger cron')
        .option('--no-manager', 'Skip installing manager cron')
        .option('-f, --force', 'Replace existing cron entries for this project')
        .action(async (options) => {
        try {
            // Get project directory
            const projectDir = process.cwd();
            // Load configuration
            const config = loadConfig(projectDir);
            // Get schedules from options or config.
            const executorSchedule = options.schedule || config.cronSchedule;
            const reviewerSchedule = options.reviewerSchedule || config.reviewerSchedule;
            // Get paths
            const nightWatchBin = getNightWatchBinPath();
            const projectName = getProjectName(projectDir);
            const marker = generateMarker(projectName);
            const logDir = path.join(projectDir, LOG_DIR);
            // Ensure log directory exists
            if (!fs.existsSync(logDir)) {
                fs.mkdirSync(logDir, { recursive: true });
            }
            const executorLog = path.join(logDir, 'executor.log');
            const reviewerLog = path.join(logDir, 'reviewer.log');
            // Check if already installed
            const existingEntries = Array.from(new Set([...getEntries(marker), ...getProjectEntries(projectDir)]));
            if (existingEntries.length > 0 && !options.force) {
                warn(`Night Watch is already installed for ${projectName}.`);
                console.log();
                dim('Existing crontab entries:');
                existingEntries.forEach((entry) => dim(`  ${entry}`));
                console.log();
                dim("Run 'night-watch install --force' to replace them.");
                return;
            }
            // Create crontab entries
            const entries = [];
            // Detect node bin directory for cron PATH
            const nodeBinDir = getNodeBinDir();
            const pathPrefix = buildCronPathPrefix(nodeBinDir, nightWatchBin);
            const cliBinPrefix = `export NW_CLI_BIN=${shellQuote(nightWatchBin)} && `;
            const cronTriggerPrefix = 'export NW_CRON_TRIGGER=1 && ';
            // Build providerEnv export prefix for cron entries
            let providerEnvPrefix = '';
            if (config.providerEnv && Object.keys(config.providerEnv).length > 0) {
                const exports = Object.entries(config.providerEnv)
                    .map(([key, value]) => `export ${key}=${shellQuote(value)}`)
                    .join(' && ');
                providerEnvPrefix = exports + ' && ';
            }
            // Executor entry (if enabled)
            const installExecutor = config.executorEnabled !== false;
            if (installExecutor) {
                const executorEntry = `${executorSchedule} ${pathPrefix}${providerEnvPrefix}${cliBinPrefix}${cronTriggerPrefix}cd ${shellQuote(projectDir)} && ${shellQuote(nightWatchBin)} run >> ${shellQuote(executorLog)} 2>&1  ${marker}`;
                entries.push(executorEntry);
            }
            // Determine if reviewer should be installed
            // Priority: --no-reviewer flag > config.reviewerEnabled
            const installReviewer = options.noReviewer === true ? false : config.reviewerEnabled;
            // Reviewer entry (if enabled)
            if (installReviewer) {
                const reviewerEntry = `${reviewerSchedule} ${pathPrefix}${providerEnvPrefix}${cliBinPrefix}${cronTriggerPrefix}cd ${shellQuote(projectDir)} && ${shellQuote(nightWatchBin)} review >> ${shellQuote(reviewerLog)} 2>&1  ${marker}`;
                entries.push(reviewerEntry);
            }
            // Determine if planner should be installed
            // Priority: --no-slicer flag > config.roadmapScanner.enabled
            const installSlicer = options.noSlicer === true ? false : config.roadmapScanner.enabled;
            // Planner entry (if roadmap scanner enabled)
            let slicerLog;
            if (installSlicer) {
                slicerLog = path.join(logDir, 'slicer.log');
                const slicerSchedule = config.roadmapScanner.slicerSchedule;
                const slicerEntry = `${slicerSchedule} ${pathPrefix}${providerEnvPrefix}${cliBinPrefix}${cronTriggerPrefix}cd ${shellQuote(projectDir)} && ${shellQuote(nightWatchBin)} planner >> ${shellQuote(slicerLog)} 2>&1  ${marker}`;
                entries.push(slicerEntry);
            }
            // Determine if QA should be installed
            const disableQa = options.noQa === true || options.qa === false;
            const installQa = disableQa ? false : config.qa.enabled;
            // QA entry (if enabled)
            let qaLog;
            if (installQa) {
                qaLog = path.join(logDir, 'qa.log');
                const qaSchedule = config.qa.schedule;
                const qaEntry = `${qaSchedule} ${pathPrefix}${providerEnvPrefix}${cliBinPrefix}${cronTriggerPrefix}cd ${shellQuote(projectDir)} && ${shellQuote(nightWatchBin)} qa >> ${shellQuote(qaLog)} 2>&1  ${marker}`;
                entries.push(qaEntry);
            }
            // Determine if audit should be installed
            const disableAudit = options.noAudit === true || options.audit === false;
            const installAudit = disableAudit ? false : config.audit.enabled;
            // Audit entry (if enabled)
            let auditLog;
            if (installAudit) {
                auditLog = path.join(logDir, 'audit.log');
                const auditSchedule = config.audit.schedule;
                const auditEntry = `${auditSchedule} ${pathPrefix}${providerEnvPrefix}${cliBinPrefix}${cronTriggerPrefix}cd ${shellQuote(projectDir)} && ${shellQuote(nightWatchBin)} audit >> ${shellQuote(auditLog)} 2>&1  ${marker}`;
                entries.push(auditEntry);
            }
            // Determine if optimizer should be installed
            const disableOptimizer = options.noOptimizer === true || options.optimizer === false;
            const installOptimizer = disableOptimizer ? false : config.optimizer.enabled;
            // Optimizer entry (if enabled)
            let optimizerLog;
            if (installOptimizer) {
                optimizerLog = path.join(logDir, 'optimizer.log');
                const optimizerSchedule = config.optimizer.schedule;
                const optimizerEntry = `${optimizerSchedule} ${pathPrefix}${providerEnvPrefix}${cliBinPrefix}${cronTriggerPrefix}cd ${shellQuote(projectDir)} && ${shellQuote(nightWatchBin)} optimize >> ${shellQuote(optimizerLog)} 2>&1  ${marker}`;
                entries.push(optimizerEntry);
            }
            // Determine if UX should be installed
            const disableUx = options.noUx === true || options.ux === false;
            const installUx = disableUx ? false : config.ux.enabled;
            // UX entry (if enabled)
            let uxLog;
            if (installUx) {
                uxLog = path.join(logDir, 'ux.log');
                const uxSchedule = config.ux.schedule;
                const uxEntry = `${uxSchedule} ${pathPrefix}${providerEnvPrefix}${cliBinPrefix}${cronTriggerPrefix}cd ${shellQuote(projectDir)} && ${shellQuote(nightWatchBin)} ux >> ${shellQuote(uxLog)} 2>&1  ${marker}`;
                entries.push(uxEntry);
            }
            // Determine if analytics should be installed
            const disableAnalytics = options.noAnalytics === true || options.analytics === false;
            const installAnalytics = disableAnalytics ? false : config.analytics.enabled;
            // Analytics entry (if enabled)
            let analyticsLog;
            if (installAnalytics) {
                analyticsLog = path.join(logDir, 'analytics.log');
                const analyticsSchedule = config.analytics.schedule;
                const analyticsEntry = `${analyticsSchedule} ${pathPrefix}${providerEnvPrefix}${cliBinPrefix}${cronTriggerPrefix}cd ${shellQuote(projectDir)} && ${shellQuote(nightWatchBin)} analytics >> ${shellQuote(analyticsLog)} 2>&1  ${marker}`;
                entries.push(analyticsEntry);
            }
            // Determine if PR resolver should be installed
            const disablePrResolver = options.noPrResolver === true ||
                options.prResolver === false;
            const installPrResolver = disablePrResolver ? false : config.prResolver.enabled;
            // PR Resolver entry (if enabled)
            let prResolverLog;
            if (installPrResolver) {
                prResolverLog = path.join(logDir, 'pr-resolver.log');
                const prResolverSchedule = config.prResolver.schedule;
                const prResolverEntry = `${prResolverSchedule} ${pathPrefix}${providerEnvPrefix}${cliBinPrefix}${cronTriggerPrefix}cd ${shellQuote(projectDir)} && ${shellQuote(nightWatchBin)} resolve >> ${shellQuote(prResolverLog)} 2>&1  ${marker}`;
                entries.push(prResolverEntry);
            }
            // Determine if merger should be installed
            const disableMerger = options.noMerger === true || options.merger === false;
            const installMerger = disableMerger ? false : (config.merger?.enabled ?? false);
            // Merger entry (if enabled)
            let mergerLog;
            if (installMerger) {
                mergerLog = path.join(logDir, 'merger.log');
                const mergerSchedule = config.merger.schedule;
                const mergerEntry = `${mergerSchedule} ${pathPrefix}${providerEnvPrefix}${cliBinPrefix}${cronTriggerPrefix}cd ${shellQuote(projectDir)} && ${shellQuote(nightWatchBin)} merge >> ${shellQuote(mergerLog)} 2>&1  ${marker}`;
                entries.push(mergerEntry);
            }
            // Determine if manager should be installed
            const disableManager = options.noManager === true || options.manager === false;
            const installManager = disableManager ? false : (config.manager?.enabled ?? false);
            // Manager entry (if enabled)
            let managerLog;
            if (installManager) {
                managerLog = path.join(logDir, `${MANAGER_LOG_NAME}.log`);
                const managerSchedule = config.manager.schedule;
                const managerEntry = `${managerSchedule} ${pathPrefix}${providerEnvPrefix}${cliBinPrefix}${cronTriggerPrefix}cd ${shellQuote(projectDir)} && ${shellQuote(nightWatchBin)} manager >> ${shellQuote(managerLog)} 2>&1  ${marker}`;
                entries.push(managerEntry);
            }
            // Add all entries
            const existingEntrySet = new Set(existingEntries);
            const currentCrontab = readCrontab();
            const baseCrontab = options.force
                ? currentCrontab.filter((line) => !existingEntrySet.has(line) && !line.includes(marker))
                : currentCrontab;
            const newCrontab = [...baseCrontab, ...entries];
            writeCrontab(newCrontab);
            // Success message
            success(`Night Watch installed successfully for ${projectName}!`);
            console.log();
            header('Crontab Entries Added');
            entries.forEach((entry) => dim(`  ${entry}`));
            console.log();
            header('Log Files');
            if (installExecutor) {
                dim(`  Executor: ${executorLog}`);
            }
            if (installReviewer) {
                dim(`  Reviewer: ${reviewerLog}`);
            }
            if (installSlicer && slicerLog) {
                dim(`  Planner: ${slicerLog}`);
            }
            if (installQa && qaLog) {
                dim(`  QA: ${qaLog}`);
            }
            if (installAudit && auditLog) {
                dim(`  Audit: ${auditLog}`);
            }
            if (installUx && uxLog) {
                dim(`  UX: ${uxLog}`);
            }
            if (installAnalytics && analyticsLog) {
                dim(`  Analytics: ${analyticsLog}`);
            }
            if (installPrResolver && prResolverLog) {
                dim(`  PR Resolver: ${prResolverLog}`);
            }
            if (installMerger && mergerLog) {
                dim(`  Merger: ${mergerLog}`);
            }
            if (installManager && managerLog) {
                dim(`  Manager: ${managerLog}`);
            }
            console.log();
            dim('To uninstall, run: night-watch uninstall');
            dim('To check status, run: night-watch status');
        }
        catch (err) {
            uiError(`Error installing Night Watch: ${err instanceof Error ? err.message : String(err)}`);
            process.exit(1);
        }
    });
}
//# sourceMappingURL=install.js.map