defmodule Towerops.DeviceProfiles.OSDefinition do
  @moduledoc """
  Schema for OS-specific metadata definitions.

  Defines how to extract device version, serial number, hardware model,
  and other OS-specific information.
  """
  use Ecto.Schema

  import Ecto.Changeset

  alias Towerops.DeviceProfiles.DeviceProfile

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

  schema "profile_os_definitions" do
    field :field, :string
    field :oid, :string
    field :regex, :string
    field :template, :string
    field :value, :string

    belongs_to :device_profile, DeviceProfile

    timestamps(type: :utc_datetime)
  end

  @doc false
  def changeset(definition, attrs) do
    definition
    |> cast(attrs, [:field, :oid, :regex, :template, :value, :device_profile_id])
    |> validate_required([:field, :device_profile_id])
    |> foreign_key_constraint(:device_profile_id)
    |> unique_constraint([:device_profile_id, :field])
  end
end
