defmodule Microwaveprop.RoverPlanning.PathTest do
  use Microwaveprop.DataCase, async: true

  alias Microwaveprop.RoverPlanning.Path

  describe "changeset/2" do
    test "valid attrs produce a valid changeset" do
      attrs = %{
        mission_id: Ecto.UUID.generate(),
        rover_location_id: Ecto.UUID.generate(),
        station_id: Ecto.UUID.generate(),
        band_mhz: 10_000,
        status: :pending
      }

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

    test "missing required fields makes changeset invalid" do
      changeset = Path.changeset(%Path{}, %{})
      refute changeset.valid?
      assert "can't be blank" in errors_on(changeset).mission_id
      assert "can't be blank" in errors_on(changeset).rover_location_id
      assert "can't be blank" in errors_on(changeset).station_id
      assert "can't be blank" in errors_on(changeset).band_mhz
    end

    test "rejects invalid status" do
      attrs = %{
        mission_id: Ecto.UUID.generate(),
        rover_location_id: Ecto.UUID.generate(),
        station_id: Ecto.UUID.generate(),
        band_mhz: 10_000,
        status: :invalid_status
      }

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

    test "rejects zero or negative band_mhz" do
      attrs = %{
        mission_id: Ecto.UUID.generate(),
        rover_location_id: Ecto.UUID.generate(),
        station_id: Ecto.UUID.generate(),
        band_mhz: 0,
        status: :pending
      }

      changeset = Path.changeset(%Path{}, attrs)
      refute changeset.valid?
      assert "must be greater than 0" in errors_on(changeset).band_mhz
    end

    test "accepts complete and failed statuses" do
      for status <- [:complete, :failed] do
        attrs = %{
          mission_id: Ecto.UUID.generate(),
          rover_location_id: Ecto.UUID.generate(),
          station_id: Ecto.UUID.generate(),
          band_mhz: 10_000,
          status: status
        }

        changeset = Path.changeset(%Path{}, attrs)
        assert changeset.valid?, "status :#{status} should be valid"
      end
    end

    test "accepts optional result and error fields" do
      attrs = %{
        mission_id: Ecto.UUID.generate(),
        rover_location_id: Ecto.UUID.generate(),
        station_id: Ecto.UUID.generate(),
        band_mhz: 10_000,
        status: :failed,
        result: nil,
        error: "timeout"
      }

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