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

/**
 * CreateContactSubmissionsTableMigration
 * ----------------------------------
 * Put some description here.
 */
@Injectable()
export default class CreateContactSubmissionsTableMigration 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('contact_submissions')

        table.primaryKey('contact_submission_id').required()

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

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

        table.column('message')
            .type(FieldType.text)

        table.column('sent_at')
            .type(FieldType.timestamp)
            .default(raw('NOW()'))

        await schema.commit(table)
    }

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

        table.dropIfExists()

        await schema.commit(table)
    }
}
