/**
 * SpecWeave Configuration Manager
 *
 * Handles reading, writing, and updating .specweave/config.json
 * with Zod validation.
 */
import fs from 'fs/promises';
import path from 'path';
import { SpecWeaveConfigSchema } from './types.js';
/**
 * ConfigManager - Manages SpecWeave configuration persistence
 *
 * **Features**:
 * - Zod schema validation on read/write
 * - Atomic updates (read-modify-write)
 * - Automatic timestamp updates
 * - Graceful error handling
 *
 * **Usage**:
 * ```typescript
 * const config = await ConfigManager.load();
 * config.research = { vision: insights };
 * await ConfigManager.save(config);
 * ```
 */
export class ConfigManager {
    /**
     * Load configuration from disk
     *
     * @param configPath - Path to config file (defaults to .specweave/config.json)
     * @returns Parsed and validated config
     * @throws Error if config is invalid or cannot be read
     */
    static async load(configPath = ConfigManager.CONFIG_PATH) {
        try {
            const content = await fs.readFile(configPath, 'utf-8');
            return SpecWeaveConfigSchema.parse(JSON.parse(content));
        }
        catch (error) {
            if (error.code === 'ENOENT') {
                throw new Error(`Config file not found: ${configPath}`);
            }
            throw error;
        }
    }
    /**
     * Save configuration to disk
     *
     * @param config - Config object to save
     * @param configPath - Path to config file (defaults to .specweave/config.json)
     */
    static async save(config, configPath = ConfigManager.CONFIG_PATH) {
        // Validate before saving
        const validated = SpecWeaveConfigSchema.parse(config);
        // Update timestamp
        validated.lastUpdated = new Date().toISOString();
        // Ensure .specweave directory exists
        const dir = path.dirname(configPath);
        await fs.mkdir(dir, { recursive: true });
        // Write with pretty formatting
        await fs.writeFile(configPath, JSON.stringify(validated, null, 2), 'utf-8');
    }
    /**
     * Update research insights in config
     *
     * Atomic read-modify-write operation.
     *
     * @param updates - Partial research config to merge
     * @param configPath - Path to config file
     */
    static async updateResearch(updates, configPath = ConfigManager.CONFIG_PATH) {
        try {
            const config = await ConfigManager.load(configPath);
            // Merge research updates
            config.research = {
                ...config.research,
                ...updates
            };
            await ConfigManager.save(config, configPath);
        }
        catch (error) {
            const errorMessage = error.message || '';
            if (error.code === 'ENOENT' || errorMessage.includes('Config file not found')) {
                throw new Error(`Cannot update research: config file not found. Run 'specweave init' first.`);
            }
            throw error;
        }
    }
    /**
     * Check if config file exists
     *
     * @param configPath - Path to config file
     * @returns True if config file exists
     */
    static async exists(configPath = ConfigManager.CONFIG_PATH) {
        try {
            await fs.access(configPath);
            return true;
        }
        catch {
            return false;
        }
    }
    /**
     * Initialize a new config file
     *
     * @param projectName - Name of the project
     * @param configPath - Path to config file
     * @returns Initialized config
     */
    static async initialize(projectName, configPath = ConfigManager.CONFIG_PATH) {
        const config = {
            version: '1.0.0',
            projectName,
            livingDocs: {
                copyBasedSync: { enabled: true },
                threeLayerSync: true
            }
        };
        await ConfigManager.save(config, configPath);
        return config;
    }
}
/**
 * Default config file path
 */
ConfigManager.CONFIG_PATH = '.specweave/config.json';
//# sourceMappingURL=ConfigManager.js.map