'use strict';

/**
 * Contract: payload migrate
 *
 * Konversi file payload backend (RDF / RESTForge consumer) menjadi file payload
 * frontend (UDF / designer). Port dari `rfd migrate` di packages/designer/.
 *
 * Perbedaan dengan `rfd migrate`:
 *  - `apiBaseUrl` di-construct dari SERVER_ADDRESS + SERVER_PORT (db-connection.env)
 *    + --project sehingga sesuai dengan runtime server actual
 *  - Port di-baca dari db-connection.env (SERVER_PORT), bukan hard-coded 3000
 *  - Primary key (constraints.primaryKey=true) di-skip dari fields[] apapun
 *    tipenya (uuid/string/integer), karena PK adalah identifier teknis dan
 *    tidak perlu di-render di UI form
 */

const runner = require('../../lib/migrate/migrate-runner');

module.exports = {
    resource: 'payload',
    verb: 'migrate',
    description: 'Convert backend payload (RDF) file into frontend payload (UDF) for the designer',
    category: 'generation',
    flags: {
        name: {
            type: 'string',
            required: true,
            description: 'Backend payload file name (e.g. visitors.json). Relative to cwd or cwd/payload/.'
        },
        output: {
            type: 'string',
            required: false,
            default: null,
            description: 'Output directory (the file will be written with the same name as --name inside it). If ending with `.json`, treated as an explicit file path. Default: frontend/payload/'
        },
        config: {
            type: 'string',
            required: false,
            default: null,
            description: 'Database config file (.env). Used to read SERVER_ADDRESS and SERVER_PORT. Falls back to `.restforge/defaults.json`.'
        },
        project: {
            type: 'string',
            required: true,
            description: 'Project name (kebab-case code) used as the path segment in apiBaseUrl: http://{host}:{port}/api/{project}'
        },
        'app-name': {
            type: 'string',
            required: false,
            default: null,
            description: 'Application name (default: derived from --project in Title Case, e.g. "visitors-app" -> "Visitors App")'
        },
        'app-code': {
            type: 'string',
            required: false,
            default: null,
            description: 'Application code in kebab-case (default: follows --project)'
        },
        plugin: {
            type: 'string',
            required: false,
            default: null,
            description: 'Designer plugin ID (default: "vanilla-js-basic")'
        },
        port: {
            type: 'number',
            required: false,
            default: null,
            description: 'Frontend application port written to appConfig.port (default: 8000). Independent from the backend port used in apiBaseUrl.'
        },
        overwrite: {
            type: 'boolean',
            required: false,
            default: false,
            description: 'Overwrite the output file if it already exists'
        }
    },
    examples: [
        'npx restforge payload migrate --name=visitors.json --output=..\\sandbox\\frontend\\payload --config=db.env --project=myapp',
        'npx restforge payload migrate --name=visitors.json --project=myapp --overwrite'
    ],
    async handler(args) {
        await runner.run({
            name: args.name,
            output: args.output || null,
            config: args.config || null,
            project: args.project,
            appName: args['app-name'] || null,
            appCode: args['app-code'] || null,
            plugin: args.plugin || null,
            port: typeof args.port === 'number' ? args.port : null,
            overwrite: args.overwrite === true
        });
    }
};
