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

  def change do
    create table(:user_credentials, 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

      # WebAuthn credential data
      add :credential_id, :binary, null: false
      add :public_key, :binary, null: false
      add :sign_count, :bigint, default: 0, null: false

      # Metadata for user-friendly display
      add :name, :string, null: false
      add :last_used_at, :utc_datetime

      # Authenticator metadata
      add :aaguid, :binary
      add :transports, {:array, :string}, default: []
      add :backup_eligible, :boolean, default: false
      add :backup_state, :boolean, default: false
      add :attestation_format, :string

      timestamps(type: :utc_datetime)
    end

    create index(:user_credentials, [:user_id])
    create unique_index(:user_credentials, [:credential_id])
  end
end
