defmodule Towerops.Topology.DeviceLinkEvidenceTest do
  use Towerops.DataCase

  alias Towerops.Topology.DeviceLinkEvidence

  describe "changeset/2" do
    test "valid changeset with required fields" do
      attrs = %{
        device_link_id: Ecto.UUID.generate(),
        evidence_type: "lldp_neighbor",
        observed_at: DateTime.utc_now()
      }

      changeset = DeviceLinkEvidence.changeset(%DeviceLinkEvidence{}, attrs)
      assert changeset.valid?
    end

    test "invalid without device_link_id" do
      attrs = %{
        evidence_type: "lldp_neighbor",
        observed_at: DateTime.utc_now()
      }

      changeset = DeviceLinkEvidence.changeset(%DeviceLinkEvidence{}, attrs)
      refute changeset.valid?
      assert "can't be blank" in errors_on(changeset).device_link_id
    end

    test "invalid without evidence_type" do
      attrs = %{
        device_link_id: Ecto.UUID.generate(),
        observed_at: DateTime.utc_now()
      }

      changeset = DeviceLinkEvidence.changeset(%DeviceLinkEvidence{}, attrs)
      refute changeset.valid?
      assert "can't be blank" in errors_on(changeset).evidence_type
    end

    test "invalid without observed_at" do
      attrs = %{
        device_link_id: Ecto.UUID.generate(),
        evidence_type: "lldp_neighbor"
      }

      changeset = DeviceLinkEvidence.changeset(%DeviceLinkEvidence{}, attrs)
      refute changeset.valid?
      assert "can't be blank" in errors_on(changeset).observed_at
    end

    test "validates evidence_type inclusion rejects invalid type" do
      attrs = %{
        device_link_id: Ecto.UUID.generate(),
        evidence_type: "invalid",
        observed_at: DateTime.utc_now()
      }

      changeset = DeviceLinkEvidence.changeset(%DeviceLinkEvidence{}, attrs)
      refute changeset.valid?
      assert "is invalid" in errors_on(changeset).evidence_type
    end

    test "validates all valid evidence_types" do
      valid_types = ~w(lldp_neighbor cdp_neighbor mac_on_interface arp_entry wireless_registration subnet_match)

      for evidence_type <- valid_types do
        attrs = %{
          device_link_id: Ecto.UUID.generate(),
          evidence_type: evidence_type,
          observed_at: DateTime.utc_now()
        }

        changeset = DeviceLinkEvidence.changeset(%DeviceLinkEvidence{}, attrs)
        assert changeset.valid?, "expected #{evidence_type} to be a valid evidence_type"
      end
    end

    test "valid changeset with optional evidence_data" do
      attrs = %{
        device_link_id: Ecto.UUID.generate(),
        evidence_type: "arp_entry",
        evidence_data: %{"ip" => "10.0.0.1", "mac" => "AA:BB:CC:DD:EE:FF"},
        observed_at: DateTime.utc_now()
      }

      changeset = DeviceLinkEvidence.changeset(%DeviceLinkEvidence{}, attrs)
      assert changeset.valid?
    end
  end
end
