defmodule Microwaveprop.Commercial.SampleTest do
  use Microwaveprop.DataCase, async: true

  alias Microwaveprop.Commercial.Link
  alias Microwaveprop.Commercial.Sample

  defp create_link do
    %Link{}
    |> Link.changeset(%{
      label: "test-link",
      host: "10.0.0.1",
      community: "public",
      radio_type: "af11x"
    })
    |> Repo.insert!()
  end

  @sample_attrs %{
    sampled_at: ~U[2026-03-30 12:00:00.000000Z],
    rx_power_0: -55.0,
    tx_power: 20.0,
    link_state: 1,
    link_uptime: 86_400,
    cur_tx_mod_rate: 6,
    rx_capacity: 450,
    tx_capacity: 450
  }

  describe "changeset/2" do
    test "valid attributes with link_id" do
      link = create_link()
      attrs = Map.put(@sample_attrs, :link_id, link.id)
      changeset = Sample.changeset(%Sample{}, attrs)
      assert changeset.valid?
    end

    test "requires link_id and sampled_at" do
      changeset = Sample.changeset(%Sample{}, %{})
      assert %{link_id: ["can't be blank"], sampled_at: ["can't be blank"]} = errors_on(changeset)
    end

    test "persists to the database" do
      link = create_link()
      attrs = Map.put(@sample_attrs, :link_id, link.id)

      assert {:ok, sample} = Repo.insert(Sample.changeset(%Sample{}, attrs))
      assert sample.rx_power_0 == -55.0
      assert sample.link_id == link.id
    end

    test "all signal fields are optional" do
      link = create_link()
      attrs = %{link_id: link.id, sampled_at: ~U[2026-03-30 12:00:00.000000Z]}
      changeset = Sample.changeset(%Sample{}, attrs)
      assert changeset.valid?
    end
  end
end
