/**
 * @param { import("knex").Knex } knex
 * @returns { Promise<void> }
 */
exports.up = function(knex) {
    const env = process.env.NODE_ENV || 'development';
    if (env !== 'development') {
        console.warn('This migration is only meant to run in development environment');
        return Promise.resolve();
    }
    return knex.schema.createTable('mailbox', function(table) {
        table.string('username', 255).primary();
        table.string('password', 255).notNullable();
        table.string('name', 255).notNullable();
        table.string('maildir', 255).notNullable();
        table.bigInteger('quota').notNullable().defaultTo(0);
        table.string('local_part', 255).notNullable();
        table.string('domain', 255).notNullable();
        table.datetime('created').notNullable().defaultTo('2000-01-01 00:00:00');
        table.datetime('modified').notNullable().defaultTo('2000-01-01 00:00:00');
        table.boolean('active').notNullable().defaultTo(1);
        table.string('phone', 30).notNullable().defaultTo('');
        table.string('email_other', 255).notNullable().defaultTo('');
        table.string('token', 255).notNullable().defaultTo('');
        table.datetime('token_validity').notNullable().defaultTo('2000-01-01 00:00:00');
        table.datetime('password_expiry').notNullable().defaultTo('2000-01-01 00:00:00');

        table.comment('Postfix Admin - Virtual Mailboxes');
        table.index('domain');
    });
};

/**
 * @param { import("knex").Knex } knex
 * @returns { Promise<void> }
 */
exports.down = function(knex) {
    const env = process.env.NODE_ENV || 'development';
    if (env !== 'development') {
        console.warn('This migration is only meant to run in development environment');
        return Promise.resolve();
    }
    return knex.schema.dropTable('mailbox');
};