'use strict';

/**
 * Contract: payload sync
 *
 * Apply schema drift dari database ke file payload (update kolom, tipe, constraint).
 * File lama di-archive sebelum overwrite (filename.json.archive.NNN).
 *
 * Inline migration v4.0.0: business logic dipindah ke
 * generators/lib/payload/payload-runner.js (dari create-payload.js).
 * Handler instantiate PayloadGenerator dan inject mode sync=true.
 */

const { PayloadGenerator } = require('../../lib/payload/payload-runner');

module.exports = {
    resource: 'payload',
    verb: 'sync',
    description: 'Apply schema drift dari database ke file payload (update kolom, tipe, constraint)',
    category: 'management',
    flags: {
        config: {
            type: 'string',
            required: false,
            default: null,
            description: 'File config database (.env). Fallback ke `.restforge/defaults.json` bila tidak disediakan eksplisit (set via `config set-default`)'
        },
        table: {
            type: 'string',
            required: false,
            default: null,
            description: 'Sync hanya satu tabel spesifik (default: semua)'
        }
    },
    examples: [
        'npx restforge payload sync --config=db.env',
        'npx restforge payload sync --config=db.env --table=users'
    ],
    async handler(args) {
        const generator = new PayloadGenerator();
        await generator.run({
            config: args.config,
            table: args.table || null,
            sync: true
        });
    }
};
