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

  import Towerops.AccountsFixtures
  import Towerops.OrganizationsFixtures

  alias Towerops.Gaiia.Account

  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 =
        Account.changeset(%Account{}, %{
          organization_id: org.id,
          gaiia_id: "acct-123"
        })

      assert changeset.valid?
    end

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

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

    test "invalid without organization_id" do
      changeset =
        Account.changeset(%Account{}, %{
          gaiia_id: "acct-123"
        })

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

    test "accepts all optional fields", %{org: org} do
      changeset =
        Account.changeset(%Account{}, %{
          organization_id: org.id,
          gaiia_id: "acct-123",
          readable_id: "1001",
          name: "John Doe",
          status: "active",
          account_type: "residential",
          address: %{"city" => "Portland"},
          subscription_count: 2,
          mrr: Decimal.new("79.99"),
          raw_data: %{"extra" => "data"}
        })

      assert changeset.valid?
    end
  end
end
