"use strict";
/**
 * Status Command
 * Issue #96: npm install CLI support
 * Issue #125: Use getPidFilePath and load .env for correct settings display
 * Issue #136: Add --issue and --all flags for worktree-specific status
 * Display CommandMate server status
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.statusCommand = statusCommand;
const dotenv_1 = require("dotenv");
const fs_1 = require("fs");
const types_1 = require("../types");
const logger_1 = require("../utils/logger");
const daemon_1 = require("../utils/daemon");
const env_setup_1 = require("../utils/env-setup");
const input_validators_1 = require("../utils/input-validators");
const logger = new logger_1.CLILogger();
/**
 * Show status for a single server (main or issue-specific)
 */
async function showSingleStatus(issueNo) {
    const pidFilePath = (0, env_setup_1.getPidFilePath)(issueNo);
    const envPath = (0, env_setup_1.getEnvPath)(issueNo);
    // Load .env so getStatus() can access correct CM_PORT and CM_BIND values
    (0, dotenv_1.config)({ path: envPath });
    const daemonManager = new daemon_1.DaemonManager(pidFilePath);
    const status = await daemonManager.getStatus();
    const serverLabel = issueNo !== undefined
        ? `Issue #${issueNo}`
        : 'Main Server';
    console.log('');
    console.log(`CommandMate Status - ${serverLabel}`);
    console.log('='.repeat(40));
    if (status === null) {
        console.log('Status:  Stopped (no PID file)');
        return;
    }
    if (!status.running) {
        console.log('Status:  Not running (stale PID file)');
        console.log('');
        const startCmd = issueNo !== undefined
            ? `commandmate start --issue ${issueNo}`
            : 'commandmate start';
        console.log(`Run "${startCmd}" to start the server`);
        return;
    }
    console.log(`Status:  Running (PID: ${status.pid})`);
    if (status.port) {
        console.log(`Port:    ${status.port}`);
    }
    if (status.uptime !== undefined) {
        console.log(`Uptime:  ${logger_1.CLILogger.formatDuration(status.uptime)}`);
    }
    if (status.url) {
        console.log(`URL:     ${status.url}`);
    }
}
/**
 * Show status for all servers (main + all worktrees)
 * Issue #136: --all flag support
 */
async function showAllStatus() {
    // Show main server status
    await showSingleStatus();
    // Check for worktree PID files
    try {
        const pidsDir = (0, env_setup_1.getPidsDir)();
        const files = (0, fs_1.readdirSync)(pidsDir).filter(f => f.endsWith('.pid'));
        for (const file of files) {
            const issueNo = parseInt(file.replace('.pid', ''), 10);
            if (!isNaN(issueNo)) {
                await showSingleStatus(issueNo);
            }
        }
    }
    catch {
        // pids directory may not exist yet
    }
    console.log('');
}
/**
 * Execute status command
 * Issue #125: Use getPidFilePath and load .env for correct settings display
 * Issue #136: Support --issue and --all flags
 */
async function statusCommand(options = {}) {
    try {
        // Issue #136: Handle --all flag
        if (options.all) {
            await showAllStatus();
            process.exit(types_1.ExitCode.SUCCESS);
            return;
        }
        // Issue #136: Validate issue number if provided
        if (options.issue !== undefined) {
            const validation = (0, input_validators_1.validateIssueNoResult)(options.issue);
            if (!validation.valid) {
                logger.error(`Invalid issue number: ${validation.error}`);
                process.exit(types_1.ExitCode.UNEXPECTED_ERROR);
                return;
            }
        }
        // Issue #125: Get PID file path and load .env for correct settings
        // Issue #136: Use issue number for worktree-specific PID file
        const pidFilePath = (0, env_setup_1.getPidFilePath)(options.issue);
        const envPath = (0, env_setup_1.getEnvPath)(options.issue);
        // Load .env so getStatus() can access correct CM_PORT and CM_BIND values
        (0, dotenv_1.config)({ path: envPath });
        const daemonManager = new daemon_1.DaemonManager(pidFilePath);
        const status = await daemonManager.getStatus();
        const serverLabel = options.issue !== undefined
            ? `Issue #${options.issue}`
            : 'Main Server';
        console.log('');
        console.log(`CommandMate Status - ${serverLabel}`);
        console.log('='.repeat(40));
        if (status === null) {
            console.log('Status:  Stopped (no PID file)');
            process.exit(types_1.ExitCode.SUCCESS);
            return;
        }
        if (!status.running) {
            console.log('Status:  Not running (stale PID file)');
            console.log('');
            const startCmd = options.issue !== undefined
                ? `commandmate start --issue ${options.issue}`
                : 'commandmate start';
            console.log(`Run "${startCmd}" to start the server`);
            process.exit(types_1.ExitCode.SUCCESS);
            return;
        }
        console.log(`Status:  Running (PID: ${status.pid})`);
        if (status.port) {
            console.log(`Port:    ${status.port}`);
        }
        if (status.uptime !== undefined) {
            console.log(`Uptime:  ${logger_1.CLILogger.formatDuration(status.uptime)}`);
        }
        if (status.url) {
            console.log(`URL:     ${status.url}`);
        }
        // Issue #332: Show IP restriction status
        if (process.env.CM_ALLOWED_IPS) {
            console.log(`IP ACL:  ${process.env.CM_ALLOWED_IPS}`);
        }
        console.log('');
        process.exit(types_1.ExitCode.SUCCESS);
    }
    catch (error) {
        const message = (0, types_1.getErrorMessage)(error);
        logger.error(`Status check failed: ${message}`);
        process.exit(types_1.ExitCode.UNEXPECTED_ERROR);
    }
}
