import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const SRC_FILE = path.join(__dirname, '../src/features/dev-tools/data/contratos/prafrot-routes.json');
const DOC_FILE = path.join(__dirname, '../docs/contratos/prafrot-routes.json');

const srcData = JSON.parse(fs.readFileSync(SRC_FILE, 'utf8'));
const docData = JSON.parse(fs.readFileSync(DOC_FILE, 'utf8'));

// Merge operations from DOC into SRC where SRC only has GET
docData.rotas.forEach(docRoute => {
    const srcRoute = srcData.rotas.find(r => r.recurso === docRoute.recurso);
    if (srcRoute) {
        docRoute.operations.forEach(docOp => {
            const hasOp = srcRoute.operations.some(op => op.path === docOp.path && op.method === docOp.method);
            if (!hasOp) {
                console.log(`Merging ${docOp.method} ${docOp.path} into ${docRoute.recurso}`);
                srcRoute.operations.push(docOp);
            }
        });
    }
});

fs.writeFileSync(SRC_FILE, JSON.stringify(srcData, null, 2), 'utf8');
console.log('Merge complete!');
