defmodule SimpleshopTheme.Orders.OrderItem do
  use Ecto.Schema
  import Ecto.Changeset

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

  schema "order_items" do
    field :variant_id, :string
    field :product_name, :string
    field :variant_title, :string
    field :quantity, :integer
    field :unit_price, :integer

    belongs_to :order, SimpleshopTheme.Orders.Order

    timestamps(type: :utc_datetime)
  end

  def changeset(item, attrs) do
    item
    |> cast(attrs, [:variant_id, :product_name, :variant_title, :quantity, :unit_price, :order_id])
    |> validate_required([:variant_id, :product_name, :quantity, :unit_price])
    |> validate_number(:quantity, greater_than: 0)
    |> validate_number(:unit_price, greater_than_or_equal_to: 0)
  end
end
