defmodule Towerops.Agents.AgentAssignmentTest do
  use Towerops.DataCase, async: true

  alias Towerops.Agents.AgentAssignment

  describe "changeset/2" do
    test "valid changeset with required fields" do
      agent_token_id = Ecto.UUID.generate()
      device_id = Ecto.UUID.generate()

      attrs = %{
        agent_token_id: agent_token_id,
        device_id: device_id
      }

      changeset = AgentAssignment.changeset(%AgentAssignment{}, attrs)

      assert changeset.valid?
      assert changeset.changes.agent_token_id == agent_token_id
      assert changeset.changes.device_id == device_id
    end

    test "valid changeset with all fields" do
      agent_token_id = Ecto.UUID.generate()
      device_id = Ecto.UUID.generate()

      attrs = %{
        agent_token_id: agent_token_id,
        device_id: device_id,
        enabled: false
      }

      changeset = AgentAssignment.changeset(%AgentAssignment{}, attrs)

      assert changeset.valid?
      assert changeset.changes.agent_token_id == agent_token_id
      assert changeset.changes.device_id == device_id
      assert changeset.changes.enabled == false
    end

    test "enabled defaults to true" do
      agent_token_id = Ecto.UUID.generate()
      device_id = Ecto.UUID.generate()

      attrs = %{
        agent_token_id: agent_token_id,
        device_id: device_id
      }

      changeset = AgentAssignment.changeset(%AgentAssignment{}, attrs)

      assert changeset.valid?
      assert Ecto.Changeset.get_field(changeset, :enabled) == true
    end

    test "requires agent_token_id" do
      device_id = Ecto.UUID.generate()
      attrs = %{device_id: device_id}

      changeset = AgentAssignment.changeset(%AgentAssignment{}, attrs)

      refute changeset.valid?
      assert %{agent_token_id: ["can't be blank"]} = errors_on(changeset)
    end

    test "requires device_id" do
      agent_token_id = Ecto.UUID.generate()
      attrs = %{agent_token_id: agent_token_id}

      changeset = AgentAssignment.changeset(%AgentAssignment{}, attrs)

      refute changeset.valid?
      assert %{device_id: ["can't be blank"]} = errors_on(changeset)
    end

    test "accepts enabled as true" do
      agent_token_id = Ecto.UUID.generate()
      device_id = Ecto.UUID.generate()

      attrs = %{
        agent_token_id: agent_token_id,
        device_id: device_id,
        enabled: true
      }

      changeset = AgentAssignment.changeset(%AgentAssignment{}, attrs)

      assert changeset.valid?
      assert Ecto.Changeset.get_field(changeset, :enabled) == true
    end

    test "accepts enabled as false" do
      agent_token_id = Ecto.UUID.generate()
      device_id = Ecto.UUID.generate()

      attrs = %{
        agent_token_id: agent_token_id,
        device_id: device_id,
        enabled: false
      }

      changeset = AgentAssignment.changeset(%AgentAssignment{}, attrs)

      assert changeset.valid?
      assert changeset.changes.enabled == false
    end

    test "allows updating enabled field" do
      agent_token_id = Ecto.UUID.generate()
      device_id = Ecto.UUID.generate()

      existing_assignment = %AgentAssignment{
        id: Ecto.UUID.generate(),
        agent_token_id: agent_token_id,
        device_id: device_id,
        enabled: true
      }

      attrs = %{enabled: false}
      changeset = AgentAssignment.changeset(existing_assignment, attrs)

      assert changeset.valid?
      assert changeset.changes.enabled == false
    end

    test "allows changing agent_token_id" do
      old_agent_token_id = Ecto.UUID.generate()
      new_agent_token_id = Ecto.UUID.generate()
      device_id = Ecto.UUID.generate()

      existing_assignment = %AgentAssignment{
        id: Ecto.UUID.generate(),
        agent_token_id: old_agent_token_id,
        device_id: device_id,
        enabled: true
      }

      attrs = %{agent_token_id: new_agent_token_id}
      changeset = AgentAssignment.changeset(existing_assignment, attrs)

      assert changeset.valid?
      assert changeset.changes.agent_token_id == new_agent_token_id
    end

    test "casts all supported fields" do
      agent_token_id = Ecto.UUID.generate()
      device_id = Ecto.UUID.generate()

      attrs = %{
        agent_token_id: agent_token_id,
        device_id: device_id,
        enabled: false
      }

      changeset = AgentAssignment.changeset(%AgentAssignment{}, attrs)

      assert changeset.valid?
      assert Map.has_key?(changeset.changes, :agent_token_id)
      assert Map.has_key?(changeset.changes, :device_id)
      assert Map.has_key?(changeset.changes, :enabled)
    end
  end
end
