defmodule Wish.Wishlist.Item do
  use Ecto.Schema
  import Ecto.Changeset

  schema "items" do
    field :description, :string
    field :title, :string
    field :url, :string
    field :received, :boolean, default: false
    field :desire, :integer
    field :image_url, :string
    field :price, :integer
    field :visible, :boolean, default: true

    timestamps(type: :utc_datetime)
  end

  @doc false
  def changeset(item, attrs) do
    item
    |> cast(attrs, [:title, :description, :url, :price, :received, :desire, :image_url, :visible])
    |> validate_required([:url])
  end

  def toggle_received_changeset(item) do
    new_state = !item.received
    change(item, received: new_state)
  end
end
