"use strict";
/**
 * List Command Handler
 *
 * Lists all saved profiles from both legacy and unified config.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleList = void 0;
const ui_1 = require("../../utils/ui");
const account_context_1 = require("../account-context");
const shared_resource_policy_1 = require("../shared-resource-policy");
const errors_1 = require("../../errors");
const exit_codes_1 = require("../../errors/exit-codes");
const types_1 = require("./types");
/**
 * Handle the list command
 */
async function handleList(ctx, args) {
    await (0, ui_1.initUI)();
    const parsed = (0, types_1.parseArgs)(args);
    const { verbose, json } = parsed;
    (0, types_1.rejectUnsupportedAuthOptions)(parsed, {
        usage: 'ccs auth list [--verbose] [--json]',
    });
    try {
        // Get profiles from both legacy (profiles.json) and unified config (config.yaml)
        const legacyProfiles = ctx.registry.getAllProfiles();
        const unifiedAccounts = ctx.registry.getAllAccountsUnified();
        // Merge profiles: unified config takes precedence
        const profiles = { ...legacyProfiles };
        for (const [name, account] of Object.entries(unifiedAccounts)) {
            profiles[name] = {
                type: 'account',
                created: account.created,
                last_used: account.last_used,
                context_mode: account.context_mode,
                context_group: account.context_group,
                continuity_mode: account.continuity_mode,
                shared_resource_mode: account.shared_resource_mode,
                bare: account.bare,
            };
        }
        const defaultProfile = ctx.registry.getDefaultUnified() ?? ctx.registry.getDefaultProfile();
        const profileNames = Object.keys(profiles);
        // JSON output mode
        if (json) {
            const output = {
                version: ctx.version,
                profiles: profileNames.map((name) => {
                    const profile = profiles[name];
                    const contextPolicy = (0, account_context_1.resolveAccountContextPolicy)(profile);
                    const resourcePolicy = (0, shared_resource_policy_1.resolveSharedResourcePolicy)(profile);
                    const isDefault = name === defaultProfile;
                    const instancePath = ctx.instanceMgr.getInstancePath(name);
                    return {
                        name: name,
                        type: profile.type || 'account',
                        is_default: isDefault,
                        created: profile.created,
                        last_used: profile.last_used || null,
                        context_mode: contextPolicy.mode,
                        context_group: contextPolicy.group || null,
                        continuity_mode: contextPolicy.mode === 'shared' ? contextPolicy.continuityMode : null,
                        shared_resource_mode: resourcePolicy.mode,
                        shared_resource_inferred: resourcePolicy.inferred,
                        ...(resourcePolicy.profileLocal ? { bare: true } : {}),
                        instance_path: instancePath,
                    };
                }),
            };
            console.log(JSON.stringify(output, null, 2));
            return;
        }
        // Human-readable output
        if (profileNames.length === 0) {
            console.log((0, ui_1.warn)('No account profiles found'));
            console.log('');
            console.log('To create your first profile:');
            console.log(`  ${(0, ui_1.color)('ccs auth create <profile>', 'command')}`);
            console.log('');
            console.log('Example:');
            console.log(`  ${(0, ui_1.color)('ccs auth create work', 'command')}`);
            console.log('');
            return;
        }
        console.log((0, ui_1.header)('Saved Account Profiles'));
        console.log('');
        // Sort by last_used (descending), then alphabetically
        const sorted = profileNames.sort((a, b) => {
            const aProfile = profiles[a];
            const bProfile = profiles[b];
            // Default first
            if (a === defaultProfile)
                return -1;
            if (b === defaultProfile)
                return 1;
            // Then by last_used
            if (aProfile.last_used && bProfile.last_used) {
                return new Date(bProfile.last_used).getTime() - new Date(aProfile.last_used).getTime();
            }
            if (aProfile.last_used)
                return -1;
            if (bProfile.last_used)
                return 1;
            // Then alphabetically
            return a.localeCompare(b);
        });
        // Build table rows
        const rows = sorted.map((name) => {
            const profile = profiles[name];
            const isDefault = name === defaultProfile;
            const contextPolicy = (0, account_context_1.resolveAccountContextPolicy)(profile);
            const resourcePolicy = (0, shared_resource_policy_1.resolveSharedResourcePolicy)(profile);
            // Status column
            const status = isDefault ? (0, ui_1.color)('[OK] default', 'success') : (0, ui_1.color)('[OK]', 'success');
            // Last used column
            let lastUsed = '-';
            if (profile.last_used) {
                lastUsed = (0, types_1.formatRelativeTime)(new Date(profile.last_used));
            }
            const row = [(0, ui_1.color)(name, isDefault ? 'primary' : 'info'), profile.type || 'account', status];
            if (verbose) {
                row.push(lastUsed);
                row.push((0, account_context_1.formatAccountContextPolicy)(contextPolicy));
                row.push(resourcePolicy.mode);
            }
            return row;
        });
        // Headers
        const headers = verbose
            ? ['Profile', 'Type', 'Status', 'Last Used', 'Context', 'Resources']
            : ['Profile', 'Type', 'Status'];
        // Print table
        console.log((0, ui_1.table)(rows, {
            head: headers,
            colWidths: verbose ? [15, 12, 15, 12, 34, 16] : [15, 12, 15],
        }));
        console.log('');
        console.log((0, ui_1.dim)(`Total: ${profileNames.length} profile(s)`));
        console.log('');
    }
    catch (error) {
        (0, errors_1.exitWithError)(`Failed to list profiles: ${error.message}`, exit_codes_1.ExitCode.GENERAL_ERROR);
    }
}
exports.handleList = handleList;
//# sourceMappingURL=list-command.js.map