require "./spec_helper"

module ContainerMom
  describe BillingManager do
    describe "PLANS" do
      it "defines basic plan limits" do
        plan = BillingManager::PLANS["basic"]
        plan[:max_regions].should eq(2)
        plan[:max_cpu].should eq("2")
        plan[:max_memory].should eq("2Gi")
      end

      it "defines pro plan limits" do
        plan = BillingManager::PLANS["pro"]
        plan[:max_regions].should eq(4)
        plan[:max_cpu].should eq("4")
        plan[:max_memory].should eq("4Gi")
      end

      it "defines enterprise plan limits" do
        plan = BillingManager::PLANS["enterprise"]
        plan[:max_regions].should eq(8)
        plan[:max_cpu].should eq("8")
        plan[:max_memory].should eq("8Gi")
      end
    end

    describe "#verify_billing" do
      it "verifies valid plan and resources" do
        deployment = create_test_deployment
        manager = BillingManager.instance

        result = manager.verify_billing(
          deployment.metadata.name,
          deployment.metadata.namespace,
          deployment.spec
        )

        result.should be_true
      end

      it "rejects when regions exceed plan limit" do
        deployment = create_test_deployment
        deployment.spec.regions = ["eu-central", "eu-east", "us-east"] # 3 regions on basic plan
        manager = BillingManager.instance

        result = manager.verify_billing(
          deployment.metadata.name,
          deployment.metadata.namespace,
          deployment.spec
        )

        result.should be_false
      end

      it "rejects when CPU exceeds plan limit" do
        deployment = create_test_deployment
        deployment.spec.resources.cpu = "3" # Exceeds basic plan limit
        manager = BillingManager.instance

        result = manager.verify_billing(
          deployment.metadata.name,
          deployment.metadata.namespace,
          deployment.spec
        )

        result.should be_false
      end

      it "rejects when memory exceeds plan limit" do
        deployment = create_test_deployment
        deployment.spec.resources.memory = "3Gi" # Exceeds basic plan limit
        manager = BillingManager.instance

        result = manager.verify_billing(
          deployment.metadata.name,
          deployment.metadata.namespace,
          deployment.spec
        )

        result.should be_false
      end

      it "handles memory unit conversions" do
        deployment = create_test_deployment
        deployment.spec.resources.memory = "2048Mi" # Equal to 2Gi
        manager = BillingManager.instance

        result = manager.verify_billing(
          deployment.metadata.name,
          deployment.metadata.namespace,
          deployment.spec
        )

        result.should be_true
      end
    end

    describe "#track_usage" do
      it "logs resource usage" do
        deployment = create_test_deployment
        manager = BillingManager.instance

        # Since track_usage currently just logs, we can only verify it doesn't raise
        manager.track_usage(
          deployment.metadata.name,
          deployment.metadata.namespace,
          deployment.spec
        )
      end
    end

    describe "#load_config" do
      it "loads configuration from environment" do
        with_test_env do
          ENV["STRIPE_API_KEY"] = "test_key"
          manager = BillingManager.instance
          # Configuration loaded successfully
        end
      end

      it "uses default values when config not found" do
        with_test_env do
          manager = BillingManager.instance
          # Should use default configuration
        end
      end
    end
  end
end
