defmodule Towerops.Profiles.DeviceOidTest do
  use Towerops.DataCase, async: true

  alias Towerops.Profiles
  alias Towerops.Profiles.DeviceOid

  describe "changeset/2" do
    setup do
      {:ok, profile} = Profiles.create_profile(%{name: "Test Profile", vendor: "Test"})
      %{profile: profile}
    end

    test "valid changeset with all required fields", %{profile: profile} do
      attrs = %{
        field: "serial_number",
        mib_name: "TEST-MIB::serialNumber.0",
        profile_id: profile.id
      }

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

    test "invalid changeset without field", %{profile: profile} do
      attrs = %{
        mib_name: "TEST-MIB::serialNumber.0",
        profile_id: profile.id
      }

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

    test "invalid changeset without mib_name", %{profile: profile} do
      attrs = %{
        field: "serial_number",
        profile_id: profile.id
      }

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

    test "invalid changeset without profile_id" do
      attrs = %{
        field: "serial_number",
        mib_name: "TEST-MIB::serialNumber.0"
      }

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

    test "valid changeset with various field names", %{profile: profile} do
      fields = [
        "serial_number",
        "firmware_version",
        "hardware_version",
        "model",
        "uptime",
        "location"
      ]

      for field <- fields do
        attrs = %{
          field: field,
          mib_name: "TEST-MIB::#{field}.0",
          profile_id: profile.id
        }

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

    test "valid changeset with various MIB name formats", %{profile: profile} do
      mib_names = [
        "SNMPv2-MIB::sysDescr.0",
        "MIKROTIK-MIB::mtxrSerialNumber.0",
        "CISCO-PRODUCTS-MIB::ciscoProductId",
        "1.3.6.1.2.1.1.1.0",
        ".1.3.6.1.2.1.1.1.0"
      ]

      for mib_name <- mib_names do
        attrs = %{
          field: "test_field",
          mib_name: mib_name,
          profile_id: profile.id
        }

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