'use strict';

/**
 * Contract: catalog field-validation
 *
 * Output JSON catalog tipe field validation, constraint, dan format preset
 * (introspection untuk AI agent / MCP server / documentation auto-generation).
 *
 * Inline migration v4.0.0: business logic asli dari
 * field-validation-catalog.js di-inline langsung di handler. Filter
 * flags legacy (--section, --type, --scope, --applicable-to, --name) TIDAK
 * di-expose di contract Fase 11, jadi inline minimal output full catalog
 * dengan summary count + pretty toggle.
 */

const { FIELD_VALIDATION_CATALOG } = require('../../lib/templates/field-validation-catalog');

module.exports = {
    resource: 'catalog',
    verb: 'field-validation',
    description: 'Output JSON catalog tipe field validation, constraint, dan format preset yang didukung',
    category: 'introspection',
    flags: {
        pretty: {
            type: 'boolean',
            required: false,
            default: true,
            description: 'Pretty-print output JSON'
        }
    },
    examples: [
        'npx restforge catalog field-validation',
        'npx restforge catalog field-validation --pretty=false'
    ],
    async handler(args) {
        const pretty = args.pretty !== false;

        const output = {
            schemaVersion: FIELD_VALIDATION_CATALOG.schemaVersion,
            source: FIELD_VALIDATION_CATALOG.source,
            summary: {
                totalTypes: FIELD_VALIDATION_CATALOG.types.length,
                totalConstraints: FIELD_VALIDATION_CATALOG.constraints.length,
                totalFormatPresets: FIELD_VALIDATION_CATALOG.formatPresets.length
            },
            types: FIELD_VALIDATION_CATALOG.types,
            constraints: FIELD_VALIDATION_CATALOG.constraints,
            formatPresets: FIELD_VALIDATION_CATALOG.formatPresets,
            auditColumns: FIELD_VALIDATION_CATALOG.auditColumns,
            messageOverridePattern: FIELD_VALIDATION_CATALOG.messageOverridePattern
        };

        const json = pretty
            ? JSON.stringify(output, null, 2)
            : JSON.stringify(output);
        console.log(json);
    }
};
