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

  alias Towerops.Profiles.DeviceProfile

  describe "changeset/2" do
    test "valid changeset with all fields" do
      attrs = %{
        name: "Test Profile",
        vendor: "Test Vendor",
        detection_pattern: "Test.*Device",
        detection_oid: "1.3.6.1.4.1.12345",
        priority: 50,
        enabled: true
      }

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

    test "valid changeset with minimal required fields" do
      attrs = %{
        name: "Minimal Profile",
        vendor: "Test"
      }

      changeset = DeviceProfile.changeset(%DeviceProfile{}, attrs)
      assert changeset.valid?
      assert get_change(changeset, :priority) == nil
      assert get_field(changeset, :priority) == 100
      assert get_field(changeset, :enabled) == true
    end

    test "invalid changeset without name" do
      attrs = %{vendor: "Test"}
      changeset = DeviceProfile.changeset(%DeviceProfile{}, attrs)
      refute changeset.valid?
      assert "can't be blank" in errors_on(changeset).name
    end

    test "invalid changeset without vendor" do
      attrs = %{name: "Test"}
      changeset = DeviceProfile.changeset(%DeviceProfile{}, attrs)
      refute changeset.valid?
      assert "can't be blank" in errors_on(changeset).vendor
    end

    test "invalid changeset with empty name" do
      attrs = %{name: "", vendor: "Test"}
      changeset = DeviceProfile.changeset(%DeviceProfile{}, attrs)
      refute changeset.valid?
    end

    test "invalid changeset with empty vendor" do
      attrs = %{name: "Test", vendor: ""}
      changeset = DeviceProfile.changeset(%DeviceProfile{}, attrs)
      refute changeset.valid?
    end

    test "valid changeset with nil optional fields" do
      attrs = %{
        name: "Test",
        vendor: "Test",
        detection_pattern: nil,
        detection_oid: nil
      }

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

    test "valid changeset with detection_pattern only" do
      attrs = %{
        name: "Test",
        vendor: "Test",
        detection_pattern: "Cisco.*"
      }

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

    test "valid changeset with detection_oid only" do
      attrs = %{
        name: "Test",
        vendor: "Test",
        detection_oid: "1.3.6.1.4.1.9"
      }

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

    test "valid changeset with both detection methods" do
      attrs = %{
        name: "Test",
        vendor: "Test",
        detection_pattern: "Cisco.*",
        detection_oid: "1.3.6.1.4.1.9"
      }

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

    test "valid changeset with custom priority" do
      attrs = %{
        name: "Test",
        vendor: "Test",
        priority: 1
      }

      changeset = DeviceProfile.changeset(%DeviceProfile{}, attrs)
      assert changeset.valid?
      assert get_change(changeset, :priority) == 1
    end

    test "valid changeset with enabled false" do
      attrs = %{
        name: "Test",
        vendor: "Test",
        enabled: false
      }

      changeset = DeviceProfile.changeset(%DeviceProfile{}, attrs)
      assert changeset.valid?
      assert get_change(changeset, :enabled) == false
    end
  end
end
