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

  import Towerops.AccountsFixtures
  import Towerops.OrganizationsFixtures

  alias Towerops.Gaiia.BillingSubscription

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

      assert changeset.valid?
    end

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

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

    test "accepts all optional fields", %{org: org} do
      changeset =
        BillingSubscription.changeset(%BillingSubscription{}, %{
          organization_id: org.id,
          gaiia_id: "sub-123",
          account_gaiia_id: "acct-456",
          status: "active",
          product_name: "100Mbps Unlimited",
          mrr_amount: Decimal.new("79.99"),
          currency: "USD",
          speed_download: 100,
          speed_upload: 25,
          raw_data: %{"extra" => "data"}
        })

      assert changeset.valid?
    end
  end
end
