#!/usr/bin/env node
"use strict";
/**
 * FAF AUTH - Birth Certificate Authentication
 *
 * Creates immutable record of project origin
 * Every authenticated .faf can be verified
 * Like a blockchain of context evolution
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
const commander_1 = require("../fix-once/commander");
const path = __importStar(require("path"));
const faf_dna_1 = require("../engines/faf-dna");
const colors_1 = require("../fix-once/colors");
const program = new commander_1.Command();
program
    .name('faf auth')
    .description('Authenticate your FAF project with a birth certificate')
    .option('--verify <certificate>', 'Verify an existing certificate')
    .option('--show', 'Show current authentication status')
    .action(async (options) => {
    try {
        const projectPath = process.cwd();
        const dnaManager = new faf_dna_1.FafDNAManager(projectPath);
        // Load existing DNA or error
        const dna = await dnaManager.load();
        if (options.verify) {
            // Verify mode
            if (!dna) {
                console.log(colors_1.colors.error('❌ No FAF DNA found to verify'));
                process.exit(1);
            }
            const isValid = dna.birthCertificate.certificate === options.verify;
            if (isValid) {
                console.log(colors_1.colors.success('✅ Certificate VALID'));
                console.log(colors_1.colors.info(`Project: ${path.basename(projectPath)}`));
                console.log(colors_1.colors.info(`Born: ${dna.birthCertificate.born.toISOString().split('T')[0]}`));
                console.log(colors_1.colors.info(`Birth DNA: ${dna.birthCertificate.birthDNA}%`));
                console.log(colors_1.colors.info(`Authenticated: ${dna.birthCertificate.authenticated ? 'Yes' : 'No'}`));
            }
            else {
                console.log(colors_1.colors.error('❌ Certificate INVALID'));
            }
            process.exit(isValid ? 0 : 1);
        }
        if (options.show) {
            // Show status mode
            if (!dna) {
                console.log(colors_1.colors.warning('🔓 No authentication found'));
                console.log(colors_1.colors.secondary('Run "faf init" first, then "faf auth" to authenticate'));
                process.exit(0);
            }
            console.log();
            console.log(colors_1.colors.primary('🎫 FAF BIRTH CERTIFICATE'));
            console.log(colors_1.colors.secondary('═'.repeat(40)));
            console.log();
            console.log(colors_1.colors.info(`Certificate: ${dna.birthCertificate.certificate}`));
            console.log(colors_1.colors.info(`Born: ${dna.birthCertificate.born.toISOString()}`));
            console.log(colors_1.colors.info(`Birth DNA: ${dna.birthCertificate.birthDNA}%`));
            console.log(colors_1.colors.info(`Source: ${dna.birthCertificate.birthDNASource}`));
            console.log();
            if (dna.birthCertificate.authenticated) {
                console.log(colors_1.colors.success(`✅ AUTHENTICATED`));
                if (dna.birthCertificate.authDate) {
                    console.log(colors_1.colors.secondary(`   Auth Date: ${dna.birthCertificate.authDate.toISOString()}`));
                }
            }
            else {
                console.log(colors_1.colors.warning(`⚠️  NOT AUTHENTICATED`));
                console.log(colors_1.colors.secondary(`   Run "faf auth" to authenticate`));
            }
            console.log();
            console.log(colors_1.colors.secondary('═'.repeat(40)));
            console.log();
            // Show journey
            const journey = dnaManager.getJourney('compact');
            if (journey) {
                console.log(colors_1.colors.info(`Journey: ${journey}`));
            }
            process.exit(0);
        }
        // Authenticate mode
        if (!dna) {
            console.log(colors_1.colors.error('❌ No FAF DNA found to authenticate'));
            console.log(colors_1.colors.secondary('Run "faf init" first to create your project DNA'));
            process.exit(1);
        }
        if (dna.birthCertificate.authenticated) {
            console.log(colors_1.colors.warning('⚠️  Already authenticated'));
            console.log(colors_1.colors.info(`Certificate: ${dna.birthCertificate.certificate}`));
            console.log(colors_1.colors.secondary(`Auth Date: ${dna.birthCertificate.authDate?.toISOString()}`));
            process.exit(0);
        }
        // Perform authentication
        console.log();
        console.log(colors_1.colors.primary('🔏 Authenticating FAF DNA...'));
        console.log();
        const certificate = await dnaManager.authenticate();
        // Display authentication success
        console.log(colors_1.colors.success('✅ AUTHENTICATION COMPLETE'));
        console.log();
        console.log(colors_1.colors.primary('🎫 BIRTH CERTIFICATE ISSUED'));
        console.log(colors_1.colors.secondary('═'.repeat(50)));
        console.log();
        console.log(colors_1.colors.highlight(`Certificate: ${certificate}`));
        console.log();
        console.log(colors_1.colors.info('Project DNA:'));
        console.log(colors_1.colors.secondary(`  Born: ${dna.birthCertificate.born.toISOString()}`));
        console.log(colors_1.colors.secondary(`  Birth DNA: ${dna.birthCertificate.birthDNA}%`));
        console.log(colors_1.colors.secondary(`  Project Hash: ${dna.birthCertificate.projectDNA}`));
        console.log();
        console.log(colors_1.colors.secondary('═'.repeat(50)));
        console.log();
        console.log(colors_1.colors.success('🔒 Your FAF context is now authenticated'));
        console.log(colors_1.colors.secondary('This certificate proves the origin and evolution of your AI context'));
        console.log();
        console.log(colors_1.colors.info('Next steps:'));
        console.log(colors_1.colors.secondary('  • faf auto    - Grow your context'));
        console.log(colors_1.colors.secondary('  • faf approve - Mark a version as approved'));
        console.log(colors_1.colors.secondary('  • faf log     - View your context evolution'));
        console.log();
    }
    catch (error) {
        console.error(colors_1.colors.error(`❌ Error: ${error.message}`));
        process.exit(1);
    }
});
program.parse(process.argv);
//# sourceMappingURL=faf-auth.js.map