defmodule WhoNeedHelp.Trust.AuditEvent do
  use Ecto.Schema
  import Ecto.Changeset

  @primary_key {:id, :binary_id, autogenerate: true}
  @foreign_key_type :binary_id

  schema "audit_events" do
    field :action, :string
    field :target_type, :string
    field :target_id, :binary_id
    field :metadata, :map, default: %{}
    belongs_to :actor, WhoNeedHelp.Accounts.User
    timestamps(type: :utc_datetime, updated_at: false)
  end

  def changeset(event, attrs) do
    event
    |> cast(attrs, [:action, :target_type, :target_id, :metadata, :actor_id])
    |> validate_required([:action, :target_type])
  end
end
