import { color, utils } from '@heroku/heroku-cli-util';
import { Command, flags } from '@heroku-cli/command';
import { Args, ux } from '@oclif/core';
import tsheredoc from 'tsheredoc';
import ConfirmCommand from '../../../lib/confirmCommand.js';
import { essentialPlan } from '../../../lib/pg/util.js';
import { nls } from '../../../nls.js';
const heredoc = tsheredoc.default;
export default class RepairDefault extends Command {
    static args = {
        database: Args.string({ description: `${nls('pg:database:arg:description')} ${nls('pg:database:arg:description:default:suffix')}` }),
    };
    static description = 'repair the permissions of the default credential within database';
    static example = `${color.command('heroku pg:credentials:repair-default postgresql-something-12345')}`;
    static flags = {
        app: flags.app({ required: true }),
        confirm: flags.string({ char: 'c', description: 'set to app name to bypass confirm prompt' }),
        remote: flags.remote(),
    };
    static topic = 'pg';
    async run() {
        const { args, flags } = await this.parse(RepairDefault);
        const { app, confirm } = flags;
        const { database } = args;
        const dbResolver = new utils.pg.DatabaseResolver(this.heroku);
        const { addon: db } = await dbResolver.getAttachment(app, database);
        if (essentialPlan(db))
            throw new Error("You can't perform this operation on Essential-tier databases.");
        await new ConfirmCommand().confirm(app, confirm, heredoc(`
      Destructive Action
      Ownership of all database objects owned by additional credentials will be transferred to the default credential.
      This command will also grant the default credential admin option for all additional credentials.
    `));
        ux.action.start('Resetting permissions and object ownership for default role to factory settings');
        await this.heroku.post(`/postgres/v0/databases/${db.name}/repair-default`, { hostname: utils.pg.host() });
        ux.action.stop();
    }
}
