"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@oclif/core");
const chalk_1 = __importDefault(require("chalk"));
const config_storage_1 = require("../../lib/config-storage");
const preferences_resolver_1 = require("../../config/preferences-resolver");
const preferences_echo_1 = require("../../config/preferences-echo");
class ConfigList extends core_1.Command {
    async run() {
        const { flags } = await this.parse(ConfigList);
        const showAll = !flags.tokens && !flags.preferences;
        try {
            const storage = new config_storage_1.ConfigStorage();
            if (flags.tokens || showAll) {
                this.printTokens(storage, flags.verbose);
            }
            if (flags.preferences || showAll) {
                this.printPreferences(storage);
            }
            console.log(chalk_1.default.gray(`Storage: ${storage.getConfigPath()}`));
            console.log(chalk_1.default.gray('Encryption: AES-256-GCM (tokens only)\n'));
        }
        catch (error) {
            console.log(chalk_1.default.red('\nFailed to list configuration'));
            console.log(chalk_1.default.gray(`Error: ${error instanceof Error ? error.message : String(error)}\n`));
            this.exit(1);
        }
    }
    printTokens(storage, verbose) {
        const providers = storage.listProviders();
        if (providers.length === 0) {
            console.log(chalk_1.default.yellow('\nNo provider tokens configured'));
            console.log(chalk_1.default.gray('Configure one: flui config set <provider> <token>'));
            return;
        }
        console.log(chalk_1.default.green(`\nProvider tokens (${providers.length}):\n`));
        for (const provider of providers) {
            const meta = storage.getTokenMetadata(provider);
            if (!meta) {
                console.log(`  ${chalk_1.default.red('✗')} ${provider} ${chalk_1.default.gray('(no metadata)')}`);
                continue;
            }
            const updatedAgo = this.getTimeAgo(new Date(), new Date(meta.updatedAt));
            console.log(`  ${chalk_1.default.green('✓')} ${chalk_1.default.bold(provider)}`);
            if (verbose) {
                console.log(chalk_1.default.gray(`    Created: ${this.formatDate(new Date(meta.createdAt))}`));
                console.log(chalk_1.default.gray(`    Updated: ${this.formatDate(new Date(meta.updatedAt))} (${updatedAgo})`));
            }
            else {
                console.log(chalk_1.default.gray(`    Configured ${updatedAgo}`));
            }
        }
    }
    printPreferences(storage) {
        const resolver = new preferences_resolver_1.PreferencesResolver(storage);
        (0, preferences_echo_1.echoPreferences)(resolver.resolveAll(), resolver);
    }
    formatDate(date) {
        return date.toLocaleString('en-US', {
            year: 'numeric',
            month: 'short',
            day: 'numeric',
            hour: '2-digit',
            minute: '2-digit',
        });
    }
    getTimeAgo(now, past) {
        const diffMs = now.getTime() - past.getTime();
        const minutes = Math.floor(diffMs / 60_000);
        const hours = Math.floor(minutes / 60);
        const days = Math.floor(hours / 24);
        if (minutes < 1)
            return 'just now';
        if (minutes < 60)
            return `${minutes} minute${minutes > 1 ? 's' : ''} ago`;
        if (hours < 24)
            return `${hours} hour${hours > 1 ? 's' : ''} ago`;
        if (days < 30)
            return `${days} day${days > 1 ? 's' : ''} ago`;
        const months = Math.floor(days / 30);
        return `${months} month${months > 1 ? 's' : ''} ago`;
    }
}
ConfigList.description = 'List configured provider tokens and resolved preferences (with their source).';
ConfigList.examples = [
    '<%= config.bin %> <%= command.id %>',
    '<%= config.bin %> <%= command.id %> --tokens',
    '<%= config.bin %> <%= command.id %> --preferences',
    '<%= config.bin %> <%= command.id %> --verbose',
];
ConfigList.flags = {
    verbose: core_1.Flags.boolean({
        char: 'v',
        description: 'Show detailed information including timestamps',
        default: false,
    }),
    tokens: core_1.Flags.boolean({
        description: 'Show only provider tokens',
        default: false,
    }),
    preferences: core_1.Flags.boolean({
        description: 'Show only resolved preferences',
        default: false,
    }),
};
exports.default = ConfigList;
