defmodule Berrypod.Orders.EmailSuppression do
  use Ecto.Schema
  import Ecto.Changeset

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

  schema "email_suppressions" do
    field :email, :string
    field :reason, :string

    timestamps(type: :utc_datetime)
  end

  def changeset(suppression, attrs) do
    suppression
    |> cast(attrs, [:email, :reason])
    |> validate_required([:email])
    |> validate_format(:email, ~r/@/)
    |> unique_constraint(:email)
  end
end
