defmodule Towerops.Repo.Migrations.CreateUserRecoveryCodes do
  use Ecto.Migration

  def change do
    create table(:user_recovery_codes, primary_key: false) do
      add :id, :binary_id, primary_key: true
      add :user_id, references(:users, type: :binary_id, on_delete: :delete_all), null: false

      add :code_hash, :string, null: false
      add :used_at, :utc_datetime
      add :created_at, :utc_datetime, null: false

      timestamps(type: :utc_datetime, updated_at: false)
    end

    create index(:user_recovery_codes, [:user_id])
    create index(:user_recovery_codes, [:user_id, :used_at])
    create unique_index(:user_recovery_codes, [:code_hash])
  end
end
