import { MigrationInterface, QueryRunner, Table } from "typeorm";

/**
 * Create the public.otp_verifications table backing the OTP module
 * (OtpVerification entity). One row per phone, holding the latest server-issued
 * code and whether that phone has been verified.
 */
export class CreateOtpVerifications1810000000003
  implements MigrationInterface
{
  name = "CreateOtpVerifications1810000000003";

  public async up(queryRunner: QueryRunner): Promise<void> {
    const exists = await queryRunner.hasTable("otp_verifications");
    if (exists) return;

    await queryRunner.createTable(
      new Table({
        name: "otp_verifications",
        columns: [
          {
            name: "id",
            type: "uuid",
            isPrimary: true,
            default: "gen_random_uuid()",
          },
          { name: "phone", type: "varchar", isUnique: true },
          { name: "otp", type: "varchar" },
          { name: "verified", type: "boolean", default: false },
          { name: "created_at", type: "timestamptz", default: "now()" },
          { name: "updated_at", type: "timestamptz", default: "now()" },
          { name: "deleted_at", type: "timestamptz", isNullable: true },
        ],
      }),
      true,
    );
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.dropTable("otp_verifications", true);
  }
}
