import type { Kysely } from "kysely";
import { sql } from "kysely";

export async function up(db: Kysely<any>): Promise<void> {
    await sql`DROP TABLE IF EXISTS "admin_console_users" CASCADE`.execute(db);

    await db.schema
        .createTable("console_users")
        .ifNotExists()
        .addColumn("id", "uuid", (col) => col.primaryKey().defaultTo(sql`gen_random_uuid()`))
        .addColumn("first_name", "text", (col) => col.notNull())
        .addColumn("last_name", "text")
        .addColumn("email", "text", (col) => col.unique().notNull())
        .addColumn("username", "text", (col) => col.unique().notNull())
        .addColumn("hashed_password", "text", (col) => col.notNull())
        .addColumn("password_salt", "text", (col) => col.notNull())
        .addColumn("password_iterations", "integer", (col) => col.notNull())
        .addColumn("password_hash_algo", "varchar(20)", (col) => col.notNull())
        .addColumn("login_attempts", "integer", (col) => col.notNull().defaultTo(0))
        .addColumn("login_lock_release_at", "timestamptz")
        .addColumn("is_active", "boolean", (col) => col.notNull().defaultTo(true))
        .addColumn("is_deleted", "boolean", (col) => col.notNull().defaultTo(false))
        .addColumn("created_at", "timestamptz", (col) => col.notNull().defaultTo(sql`now()`))
        .addColumn("updated_at", "timestamptz")
        .execute();
}

export async function down(db: Kysely<any>): Promise<void> {
    await db.schema.dropTable("console_users").ifExists().execute();
}
