'use strict';

/**
 * Contract: catalog query-declarative
 *
 * Output JSON catalog query declarative properties, endpoint resolution rules,
 * dan file reference convention (introspection untuk AI agent / MCP server /
 * documentation auto-generation).
 *
 * Inline migration v4.0.0: business logic asli dari
 * query-declarative-catalog.js di-inline langsung di handler. Filter
 * flags legacy (--section, --name, --endpoint, --supports-file-reference,
 * --required) TIDAK di-expose di contract Fase 11, jadi inline minimal output
 * full catalog dengan summary count + pretty toggle.
 */

const { QUERY_DECLARATIVE_CATALOG } = require('../../lib/templates/query-declarative-catalog');

module.exports = {
    resource: 'catalog',
    verb: 'query-declarative',
    description: 'Output JSON catalog query declarative properties, endpoint resolution, dan file reference convention',
    category: 'introspection',
    flags: {
        pretty: {
            type: 'boolean',
            required: false,
            default: true,
            description: 'Pretty-print output JSON'
        }
    },
    examples: [
        'npx restforge catalog query-declarative',
        'npx restforge catalog query-declarative --pretty=false'
    ],
    async handler(args) {
        const pretty = args.pretty !== false;

        const output = {
            schemaVersion: QUERY_DECLARATIVE_CATALOG.schemaVersion,
            source: QUERY_DECLARATIVE_CATALOG.source,
            summary: {
                totalQueryProperties: QUERY_DECLARATIVE_CATALOG.queryProperties.length,
                totalEndpoints: QUERY_DECLARATIVE_CATALOG.endpointResolution.length,
                totalDatabasePlaceholders: QUERY_DECLARATIVE_CATALOG.fileReferenceConvention.placeholderByDatabase.length,
                totalFileReferenceCapableProperties: QUERY_DECLARATIVE_CATALOG.fileReferenceConvention.applicableProperties.length
            },
            queryProperties: QUERY_DECLARATIVE_CATALOG.queryProperties,
            endpointResolution: QUERY_DECLARATIVE_CATALOG.endpointResolution,
            fileReferenceConvention: QUERY_DECLARATIVE_CATALOG.fileReferenceConvention,
            documentationUrl: QUERY_DECLARATIVE_CATALOG.documentationUrl
        };

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