defmodule Berrypod.Redirects.DeadLink do
  use Ecto.Schema
  import Ecto.Changeset

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

  @url_types ~w(internal external)
  @statuses ~w(broken ignored)

  schema "dead_links" do
    field :url, :string
    field :url_type, :string, default: "external"
    field :status, :string, default: "broken"
    field :http_status, :integer
    field :error, :string
    field :last_checked_at, :utc_datetime

    timestamps()
  end

  def changeset(dead_link, attrs) do
    dead_link
    |> cast(attrs, [:url, :url_type, :status, :http_status, :error, :last_checked_at])
    |> validate_required([:url, :url_type, :last_checked_at])
    |> validate_inclusion(:url_type, @url_types)
    |> validate_inclusion(:status, @statuses)
    |> unique_constraint(:url)
  end
end
