'use strict';

/**
 * Contract: config get-default
 *
 * Menampilkan file config default yang aktif untuk working directory saat ini.
 * Membaca dari {cwd}/.restforge/defaults.json.
 *
 * Inline migration v4.0.0: business logic asli dari config-get-default.js
 * di-inline langsung di handler. Adapter process.argv override dihapus karena
 * handler langsung pakai args.pretty. Exit code semantics:
 *   - Default tidak di-set ATAU file tidak ditemukan -> throw (exit 1 via cli-entry.js)
 *   - Default ada dan file ditemukan -> return normal (exit 0)
 */

const fs = require('node:fs');
const {
    getDefaultConfig,
    getDefaultsFilePath,
    resolveConfigFilePath
} = require('../../lib/utils/config-resolver');

module.exports = {
    resource: 'config',
    verb: 'get-default',
    description: 'Menampilkan file config default yang aktif untuk working directory saat ini',
    category: 'introspection',
    flags: {
        pretty: {
            type: 'boolean',
            required: false,
            default: true,
            description: 'Pretty-print output JSON'
        }
    },
    examples: [
        'npx restforge config get-default',
        'npx restforge config get-default --pretty=false'
    ],
    async handler(args) {
        const pretty = args.pretty !== false;
        const cwd = process.cwd();
        const def = getDefaultConfig(cwd);
        const defaultsPath = getDefaultsFilePath(cwd);

        if (!def) {
            if (pretty) {
                console.log('');
                console.log('No default config is set for this working directory.');
                console.log('');
                console.log(`  Expected at: ${defaultsPath}`);
                console.log('');
                console.log('Set a default with:');
                console.log('  npx restforge config set-default --config=<file>');
                console.log('');
            } else {
                console.log(JSON.stringify({
                    isSet: false,
                    defaultsFile: defaultsPath
                }));
            }
            throw new Error('No default config is set');
        }

        const resolvedPath = resolveConfigFilePath(def.config, cwd);
        const fileExists = fs.existsSync(resolvedPath);

        if (pretty) {
            console.log('');
            console.log('Default Config');
            console.log('');
            console.log(`  Config name : ${def.config}`);
            console.log(`  Resolved to : ${resolvedPath}`);
            console.log(`  File status : ${fileExists ? 'found' : 'NOT FOUND'}`);
            if (def.setAt) {
                console.log(`  Set at      : ${def.setAt}`);
            }
            console.log(`  Stored at   : ${defaultsPath}`);
            console.log('');

            if (!fileExists) {
                console.log('Warning: The default config file no longer exists at the resolved path.');
                console.log('Re-set with: npx restforge config set-default --config=<file>');
                console.log('');
            }
        } else {
            console.log(JSON.stringify({
                isSet: true,
                config: def.config,
                resolvedPath,
                fileExists,
                setAt: def.setAt,
                defaultsFile: defaultsPath
            }));
        }

        if (!fileExists) {
            throw new Error('Default config file no longer exists at the resolved path');
        }
    }
};
