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

/**
 * CreatePhysicalHostsTableMigration
 * ----------------------------------
 * Put some description here.
 */
@Injectable()
export default class CreatePhysicalHostsTableMigration extends Migration {
    @Inject()
    protected readonly db!: DatabaseService

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

        const hostGroups = await schema.table('p5x_host_groups')
        hostGroups.primaryKey('id')
        hostGroups.column('name').type(FieldType.varchar)
        await schema.commit(hostGroups)

        const hostGroupHosts = await schema.table('p5x_host_group_hosts')
        hostGroupHosts.primaryKey('id')
        hostGroupHosts.column('pve_host').type(FieldType.varchar)
        hostGroupHosts.column('host_group_id').type(FieldType.int)
        hostGroupHosts.index('hostgrouphostid').field('host_group_id')
        await schema.commit(hostGroupHosts)
    }

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

        const hostGroups = await schema.table('p5x_host_groups')
        hostGroups.dropIfExists()
        await schema.commit(hostGroups)

        const hostGroupHosts = await schema.table('p5x_host_group_hosts')
        hostGroupHosts.dropIfExists()
        await schema.commit(hostGroupHosts)
    }
}
