/**
 * CI/CD Monitor Service
 *
 * High-level orchestration service that coordinates the workflow monitor,
 * state manager, and notifier for end-to-end failure detection and notification.
 */
import { WorkflowMonitor } from './workflow-monitor.js';
import { StateManager } from './state-manager.js';
import { Notifier } from './notifier.js';
/**
 * MonitorService - Orchestrates CI/CD monitoring components
 *
 * Coordinates WorkflowMonitor, StateManager, and Notifier for
 * automatic failure detection and notification.
 */
export class MonitorService {
    /**
     * Create monitor service
     */
    constructor(config) {
        this.startTime = null;
        this.config = {
            ...config,
            rootDir: config.rootDir ?? process.cwd(),
            autoNotify: config.autoNotify ?? true,
            pushStrategy: config.pushStrategy ?? 'direct',
            git: config.git ?? { branchPrefix: 'sw/', targetBranch: 'main', deleteOnMerge: true },
            autoFix: config.autoFix ?? { enabled: true, maxRetries: 1, allowedBranches: ['develop', 'main'] },
            release: config.release ?? {},
        };
        this.stateManager = new StateManager(this.config.rootDir);
        this.notifier = new Notifier(this.config.notifier);
        this.monitor = new WorkflowMonitor(this.config.monitor, this.stateManager);
    }
    /**
     * Start service
     */
    async start() {
        if (this.monitor.isRunning()) {
            throw new Error('Service already running');
        }
        console.log('Starting CI/CD Monitor Service...');
        this.startTime = Date.now();
        this.monitor.start();
        console.log('Service started');
        console.log(`  Repository: ${this.config.monitor.owner}/${this.config.monitor.repo}`);
        console.log(`  Poll interval: ${this.config.monitor.pollInterval}ms`);
        console.log(`  Notifications: ${this.config.notifier.channels.join(', ')}`);
    }
    /**
     * Stop service
     */
    async stop() {
        if (!this.monitor.isRunning()) {
            throw new Error('Service not running');
        }
        console.log('Stopping CI/CD Monitor Service...');
        this.monitor.stop();
        this.startTime = null;
        console.log('Service stopped');
    }
    /**
     * Get service status
     */
    async getStatus() {
        const state = await this.stateManager.loadState();
        const unprocessed = await this.stateManager.getUnprocessedFailures();
        return {
            running: this.monitor.isRunning(),
            uptime: this.startTime ? Date.now() - this.startTime : 0,
            totalFailures: state.totalFailures,
            unprocessedFailures: unprocessed.length,
            lastPoll: state.lastPoll
        };
    }
    /**
     * Check for new failures and send notifications
     */
    async checkForNewFailures() {
        const unprocessed = await this.stateManager.getUnprocessedFailures();
        for (const failure of unprocessed) {
            // Send notification
            await this.notifier.notifyFailureDetected(failure);
            // Mark as processed (notification sent)
            await this.stateManager.markProcessed(failure.runId);
        }
    }
    /**
     * Manually trigger notification for a failure
     *
     * @param runId - Workflow run ID
     */
    async notifyFailure(runId) {
        const state = await this.stateManager.loadState();
        const failure = state.failures[runId];
        if (!failure) {
            throw new Error(`Failure not found: ${runId}`);
        }
        await this.notifier.notifyFailureDetected(failure);
    }
    /**
     * Get all unprocessed failures
     */
    async getUnprocessedFailures() {
        return this.stateManager.getUnprocessedFailures();
    }
    /**
     * Mark failure as processed
     */
    async markProcessed(runId) {
        await this.stateManager.markProcessed(runId);
    }
}
//# sourceMappingURL=monitor-service.js.map