/**
 * GitHub Profile Manager
 *
 * Manages multiple GitHub repository profiles for multi-repo projects
 * Handles CRUD operations and profile selection for increments
 *
 * @module cli/helpers/github/profile-manager
 */
import * as fs from 'fs';
import * as path from 'path';
import chalk from 'chalk';
import { select } from '@inquirer/prompts';
/**
 * GitHub Profile Manager
 *
 * Handles all profile-related operations
 */
export class GitHubProfileManager {
    constructor(projectPath) {
        this.configPath = path.join(projectPath, '.specweave', 'config.json');
        this.loadConfig();
    }
    /**
     * Load config from disk
     */
    loadConfig() {
        try {
            if (fs.existsSync(this.configPath)) {
                const content = fs.readFileSync(this.configPath, 'utf-8');
                this.config = JSON.parse(content);
            }
            else {
                this.config = {};
            }
            // Ensure sync structure exists
            if (!this.config.sync) {
                this.config.sync = {
                    enabled: false,
                    defaultProfile: '',
                    profiles: {}
                };
            }
            if (!this.config.sync.profiles) {
                this.config.sync.profiles = {};
            }
        }
        catch (error) {
            console.error(chalk.red('Failed to load config:'), error);
            this.config = {
                sync: {
                    enabled: false,
                    defaultProfile: '',
                    profiles: {}
                }
            };
        }
    }
    /**
     * Save config to disk
     */
    saveConfig() {
        try {
            const dir = path.dirname(this.configPath);
            if (!fs.existsSync(dir)) {
                fs.mkdirSync(dir, { recursive: true });
            }
            fs.writeFileSync(this.configPath, JSON.stringify(this.config, null, 2) + '\n');
        }
        catch (error) {
            console.error(chalk.red('Failed to save config:'), error);
            throw error;
        }
    }
    /**
     * Get all GitHub profiles
     *
     * @returns Array of GitHub profiles
     */
    getAllProfiles() {
        if (!this.config.sync?.profiles) {
            return [];
        }
        return Object.entries(this.config.sync.profiles)
            .filter(([_, profile]) => profile.provider === 'github')
            .map(([id, profile]) => ({ ...profile, id }));
    }
    /**
     * Get a specific profile by ID
     *
     * @param id - Profile ID
     * @returns Profile or null
     */
    getProfile(id) {
        const profile = this.config.sync?.profiles?.[id];
        if (!profile || profile.provider !== 'github') {
            return null;
        }
        return { ...profile, id };
    }
    /**
     * Get the default profile
     *
     * @returns Default profile or null
     */
    getDefaultProfile() {
        const defaultId = this.config.sync?.defaultProfile;
        if (!defaultId) {
            // If no default profile set, return the first GitHub profile
            const profiles = this.getAllProfiles();
            return profiles.length > 0 ? profiles[0] : null;
        }
        return this.getProfile(defaultId);
    }
    /**
     * Add a new profile
     *
     * @param profile - Profile to add
     * @returns Success status
     */
    addProfile(profile) {
        try {
            if (!this.config.sync) {
                this.config.sync = {
                    enabled: true,
                    defaultProfile: profile.id,
                    profiles: {}
                };
            }
            // Check if ID already exists
            if (this.config.sync.profiles[profile.id]) {
                console.error(chalk.red(`Profile with ID '${profile.id}' already exists`));
                return false;
            }
            // Add the profile
            this.config.sync.profiles[profile.id] = {
                ...profile,
                provider: 'github',
                timeRange: profile.timeRange || {
                    default: '1M',
                    max: '6M'
                },
                rateLimits: profile.rateLimits || {
                    maxItemsPerSync: 500,
                    warnThreshold: 100
                }
            };
            // Set as default if it's the first profile
            if (!this.config.sync.defaultProfile) {
                this.config.sync.defaultProfile = profile.id;
                this.config.sync.enabled = true;
            }
            this.saveConfig();
            return true;
        }
        catch (error) {
            console.error(chalk.red('Failed to add profile:'), error);
            return false;
        }
    }
    /**
     * Update an existing profile
     *
     * @param id - Profile ID
     * @param updates - Partial profile updates
     * @returns Success status
     */
    updateProfile(id, updates) {
        try {
            const profile = this.config.sync?.profiles?.[id];
            if (!profile) {
                console.error(chalk.red(`Profile '${id}' not found`));
                return false;
            }
            // Update the profile
            this.config.sync.profiles[id] = {
                ...profile,
                ...updates,
                provider: 'github' // Ensure provider stays as github
            };
            this.saveConfig();
            return true;
        }
        catch (error) {
            console.error(chalk.red('Failed to update profile:'), error);
            return false;
        }
    }
    /**
     * Delete a profile
     *
     * @param id - Profile ID
     * @returns Success status
     */
    deleteProfile(id) {
        try {
            if (!this.config.sync?.profiles?.[id]) {
                console.error(chalk.red(`Profile '${id}' not found`));
                return false;
            }
            // Delete the profile
            delete this.config.sync.profiles[id];
            // Update default profile if necessary
            if (this.config.sync.defaultProfile === id) {
                const remainingProfiles = this.getAllProfiles();
                this.config.sync.defaultProfile = remainingProfiles.length > 0
                    ? remainingProfiles[0].id
                    : '';
                // Disable sync if no profiles left
                if (remainingProfiles.length === 0) {
                    this.config.sync.enabled = false;
                }
            }
            this.saveConfig();
            return true;
        }
        catch (error) {
            console.error(chalk.red('Failed to delete profile:'), error);
            return false;
        }
    }
    /**
     * Set default profile
     *
     * @param id - Profile ID
     * @returns Success status
     */
    setDefaultProfile(id) {
        try {
            if (!this.config.sync?.profiles?.[id]) {
                console.error(chalk.red(`Profile '${id}' not found`));
                return false;
            }
            this.config.sync.defaultProfile = id;
            this.config.sync.enabled = true;
            this.saveConfig();
            return true;
        }
        catch (error) {
            console.error(chalk.red('Failed to set default profile:'), error);
            return false;
        }
    }
    /**
     * Interactive profile selection
     *
     * @param message - Prompt message
     * @returns Selected profile or null
     */
    async selectProfile(message = 'Select GitHub repository:') {
        const profiles = this.getAllProfiles();
        if (profiles.length === 0) {
            console.log(chalk.yellow('No GitHub profiles configured'));
            return null;
        }
        if (profiles.length === 1) {
            // Auto-select if only one profile
            return profiles[0];
        }
        const selectedId = await select({
            message,
            choices: profiles.map(p => ({
                name: `${p.displayName} (${p.config.owner}/${p.config.repo})`,
                value: p.id
            }))
        });
        return this.getProfile(selectedId);
    }
    /**
     * Check if any profiles exist
     *
     * @returns True if profiles exist
     */
    hasProfiles() {
        return this.getAllProfiles().length > 0;
    }
    /**
     * Get profile count
     *
     * @returns Number of profiles
     */
    getProfileCount() {
        return this.getAllProfiles().length;
    }
    /**
     * List all profiles (formatted for display)
     */
    listProfiles() {
        const profiles = this.getAllProfiles();
        const defaultId = this.config.sync?.defaultProfile;
        if (profiles.length === 0) {
            console.log(chalk.yellow('No GitHub profiles configured'));
            return;
        }
        console.log(chalk.cyan('\nGitHub Repository Profiles:\n'));
        profiles.forEach(profile => {
            const isDefault = profile.id === defaultId;
            const marker = isDefault ? chalk.green('✓') : ' ';
            const label = isDefault ? chalk.green('(default)') : '';
            console.log(`${marker} ${chalk.white(profile.id)}: ${profile.displayName} ${label}`);
            console.log(`  ${chalk.gray(`${profile.config.owner}/${profile.config.repo}`)}`);
            if (profile.config.monorepoProjects && profile.config.monorepoProjects.length > 0) {
                console.log(`  ${chalk.gray('Projects:')} ${profile.config.monorepoProjects.join(', ')}`);
            }
        });
        console.log('');
    }
}
//# sourceMappingURL=profile-manager.js.map