"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 api_client_1 = require("../../lib/api-client");
const config_storage_1 = require("../../lib/config-storage");
class IntegrationInstallations extends core_1.Command {
    async run() {
        const configStorage = new config_storage_1.ConfigStorage();
        const apiUrl = configStorage.getApiUrlOrThrow();
        const apiKey = configStorage.getApiKey();
        if (!apiKey) {
            this.error('Not logged in. Run `flui auth login` first.', { exit: 1 });
        }
        const api = new api_client_1.ApiClient({ baseUrl: apiUrl, apiKey });
        let rows;
        try {
            rows = await api.get('/repositories/github-app/installations');
        }
        catch (error) {
            this.error(`Failed to list installations: ${error.message}`, { exit: 1 });
        }
        if (!rows || rows.length === 0) {
            console.log(chalk_1.default.dim('\n  No GitHub App installations tracked.\n'));
            return;
        }
        console.log('');
        console.log(`  ${chalk_1.default.bold('INSTALLATION_ID')}  ${chalk_1.default.bold('ACCOUNT')}  ${chalk_1.default.bold('TYPE')}  ${chalk_1.default.bold('STATUS')}  ${chalk_1.default.bold('CREATED')}`);
        console.log(`  ${'─'.repeat(80)}`);
        for (const r of rows) {
            const created = new Date(r.createdAt).toISOString().slice(0, 10);
            const status = r.suspendedAt
                ? chalk_1.default.yellow('suspended')
                : chalk_1.default.green('active');
            console.log(`  ${String(r.installationId).padEnd(15)}  ${r.accountLogin.padEnd(20)}  ${r.accountType.padEnd(12)}  ${status}  ${created}`);
        }
        console.log('');
    }
}
IntegrationInstallations.description = 'List all GitHub App installations tracked by Flui. Requires admin privileges. Use the `installationId` shown here with `flui integration remove-installation` to delete a stale or wrong record.';
IntegrationInstallations.examples = ['<%= config.bin %> <%= command.id %>'];
exports.default = IntegrationInstallations;
