defmodule Berrypod.Newsletter.Campaign do
  use Ecto.Schema
  import Ecto.Changeset

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

  schema "newsletter_campaigns" do
    field :subject, :string
    field :body, :string
    field :status, :string, default: "draft"
    field :scheduled_at, :utc_datetime
    field :sent_at, :utc_datetime
    field :sent_count, :integer, default: 0
    field :failed_count, :integer, default: 0

    timestamps(type: :utc_datetime)
  end

  def changeset(campaign, attrs) do
    campaign
    |> cast(attrs, [:subject, :body, :status, :scheduled_at, :sent_at, :sent_count, :failed_count])
    |> validate_required([:subject, :body])
    |> validate_length(:subject, max: 200)
    |> validate_inclusion(:status, ~w(draft scheduled sending sent cancelled))
  end
end
