defmodule Towerops.Gaiia.NetworkSiteTest do
  use Towerops.DataCase, async: true

  import Towerops.AccountsFixtures
  import Towerops.OrganizationsFixtures

  alias Towerops.Gaiia.NetworkSite

  setup do
    user = user_fixture()
    org = organization_fixture(user.id)
    %{org: org}
  end

  describe "changeset/2" do
    test "valid with required fields", %{org: org} do
      changeset =
        NetworkSite.changeset(%NetworkSite{}, %{
          organization_id: org.id,
          gaiia_id: "site-123"
        })

      assert changeset.valid?
    end

    test "invalid without gaiia_id", %{org: org} do
      changeset =
        NetworkSite.changeset(%NetworkSite{}, %{
          organization_id: org.id
        })

      refute changeset.valid?
      assert "can't be blank" in errors_on(changeset).gaiia_id
    end

    test "accepts ip_blocks array", %{org: org} do
      changeset =
        NetworkSite.changeset(%NetworkSite{}, %{
          organization_id: org.id,
          gaiia_id: "site-123",
          ip_blocks: ["10.0.0.0/24", "192.168.1.0/24"]
        })

      assert changeset.valid?
    end

    test "accepts all optional fields", %{org: org} do
      changeset =
        NetworkSite.changeset(%NetworkSite{}, %{
          organization_id: org.id,
          gaiia_id: "site-123",
          name: "Tower 3",
          address: %{"city" => "Portland"},
          ip_blocks: ["10.0.0.0/24"],
          account_count: 147,
          total_mrr: Decimal.new("17640.00"),
          raw_data: %{"extra" => "data"}
        })

      assert changeset.valid?
    end
  end
end
