"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 IntegrationGhcrPatStatus 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 status;
        try {
            status = await api.get('/repositories/github-app/packages-pat/status');
        }
        catch (error) {
            this.error(`Failed to fetch GHCR PAT status: ${error.message}`, { exit: 1 });
        }
        console.log('');
        if (!status.configured) {
            console.log(`  ${chalk_1.default.bold('Status:')} ${chalk_1.default.yellow('not configured')}`);
            console.log('');
            console.log(chalk_1.default.dim(`  Run \`${chalk_1.default.cyan('flui integration ghcr-pat set')}\` to add one.`));
            console.log('');
            return;
        }
        const statusColor = status.status === 'VALID'
            ? chalk_1.default.green
            : status.status === 'EXPIRING_SOON'
                ? chalk_1.default.yellow
                : chalk_1.default.red;
        console.log(`  ${chalk_1.default.bold('Status:')}    ${statusColor(status.status ?? 'unknown')}`);
        if (status.githubLogin) {
            console.log(`  GitHub user: ${chalk_1.default.bold(status.githubLogin)}`);
        }
        if (status.scopes && status.scopes.length > 0) {
            console.log(`  Scopes:      ${status.scopes.join(', ')}`);
        }
        if (status.expiresAt) {
            const date = new Date(status.expiresAt).toISOString().slice(0, 10);
            const days = status.daysUntilExpiry;
            const suffix = days != null
                ? days < 0
                    ? chalk_1.default.red(` (expired ${-days}d ago)`)
                    : days <= 14
                        ? chalk_1.default.yellow(` (${days}d left)`)
                        : chalk_1.default.dim(` (${days}d left)`)
                : '';
            console.log(`  Expires:     ${date}${suffix}`);
        }
        if (status.lastRotatedAt) {
            console.log(`  Rotated:     ${new Date(status.lastRotatedAt).toISOString().slice(0, 10)}`);
        }
        if (status.lastVerifiedAt) {
            console.log(`  Verified:    ${new Date(status.lastVerifiedAt).toISOString().slice(0, 10)}`);
        }
        console.log('');
    }
}
IntegrationGhcrPatStatus.description = 'Show the current state of the GHCR PAT used by Flui to pull container images from GHCR.';
IntegrationGhcrPatStatus.examples = ['<%= config.bin %> <%= command.id %>'];
exports.default = IntegrationGhcrPatStatus;
