import {Injectable, Migration, Inject, DatabaseService, FieldType} from '@extollo/lib'

/**
 * CreateP5xCredentialsTableMigration
 * ----------------------------------
 * Create a table to store the PVE user credentials.
 */
@Injectable()
export default class CreateP5xCredentialsTableMigration extends Migration {
    @Inject()
    protected readonly db!: DatabaseService

    /**
     * Apply the migration.
     */
    async up(): Promise<void> {
        const schema = this.db.get().schema()
        const table = await schema.table('p5x_credentials')

        table.primaryKey('id')

        table.column('pve_username')
            .type(FieldType.varchar)
            .required()

        table.column('pve_credential')
            .type(FieldType.text)
            .required()

        table.column('is_api_token')
            .type(FieldType.bool)
            .required()

        await schema.commit(table)
    }

    /**
     * Undo the migration.
     */
    async down(): Promise<void> {
        const schema = this.db.get().schema()
        const table = await schema.table('p5x_credentials')

        table.dropIfExists()

        await schema.commit(table)
    }
}
