defmodule SimpleshopTheme.Settings.Setting do
  use Ecto.Schema
  import Ecto.Changeset

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

  schema "settings" do
    field :key, :string
    field :value, :string
    field :value_type, :string, default: "string"

    timestamps(type: :utc_datetime)
  end

  @doc false
  def changeset(setting, attrs) do
    setting
    |> cast(attrs, [:key, :value, :value_type])
    |> validate_required([:key, :value, :value_type])
    |> validate_inclusion(:value_type, ~w(string json integer boolean))
    |> unique_constraint(:key)
  end
end
