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

/**
 * CreateP5xNodesTableMigration
 * ----------------------------------
 * Create a table to track the virtual K8s containers under our control.
 */
@Injectable()
export default class CreateP5xNodesTableMigration 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_nodes')

        table.primaryKey('id')

        table.column('pve_id')
            .type(FieldType.int)
            .required()

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

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

        table.column('assigned_subnet')
            .type(FieldType.int)
            .required()

        table.column('is_permanent')
            .type(FieldType.bool)
            .default(false)
            .required()

        table.column('is_master')
            .type(FieldType.bool)
            .default(false)
            .required()

        await schema.commit(table)
    }

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

        table.dropIfExists()

        await schema.commit(table)
    }
}
