require "../spec_helper"

module ContainerMom::Controller
  describe ContainerDeployment do
    describe "#reconcile" do
      it "creates deployments in specified regions" do
        with_test_env do
          ENV["CLUSTER_EU_CENTRAL_TOKEN"] = "test-token"
          ENV["CLUSTER_EU_CENTRAL_CA"] = "test-ca"
          ENV["CLOUDFLARE_API_TOKEN"] = "test-token"

          deployment = create_test_deployment
          controller = ContainerDeployment.new

          result = controller.reconcile(
            deployment.metadata.name,
            deployment.metadata.namespace,
            deployment.spec
          )

          result.should be_true
        end
      end

      it "handles source type: registry" do
        with_test_env do
          ENV["CLUSTER_EU_CENTRAL_TOKEN"] = "test-token"
          ENV["CLUSTER_EU_CENTRAL_CA"] = "test-ca"
          ENV["CLOUDFLARE_API_TOKEN"] = "test-token"

          deployment = create_test_deployment
          deployment.spec.source.type = "registry"
          deployment.spec.source.url = "nginx:latest"
          controller = ContainerDeployment.new

          result = controller.reconcile(
            deployment.metadata.name,
            deployment.metadata.namespace,
            deployment.spec
          )

          result.should be_true
        end
      end

      it "handles source type: git" do
        with_test_env do
          ENV["CLUSTER_EU_CENTRAL_TOKEN"] = "test-token"
          ENV["CLUSTER_EU_CENTRAL_CA"] = "test-ca"
          ENV["CLOUDFLARE_API_TOKEN"] = "test-token"

          deployment = create_test_deployment
          deployment.spec.source.type = "git"
          deployment.spec.source.url = "https://github.com/example/app.git"
          controller = ContainerDeployment.new

          result = controller.reconcile(
            deployment.metadata.name,
            deployment.metadata.namespace,
            deployment.spec
          )

          result.should be_true
        end
      end

      it "validates resource requirements" do
        with_test_env do
          ENV["CLUSTER_EU_CENTRAL_TOKEN"] = "test-token"
          ENV["CLUSTER_EU_CENTRAL_CA"] = "test-ca"
          ENV["CLOUDFLARE_API_TOKEN"] = "test-token"

          deployment = create_test_deployment
          deployment.spec.resources.cpu = "-1"
          controller = ContainerDeployment.new

          expect_raises(ContainerDeployment::Error, "Invalid CPU value") do
            controller.reconcile(
              deployment.metadata.name,
              deployment.metadata.namespace,
              deployment.spec
            )
          end
        end
      end

      it "validates region configuration" do
        with_test_env do
          ENV["CLUSTER_EU_CENTRAL_TOKEN"] = "test-token"
          ENV["CLUSTER_EU_CENTRAL_CA"] = "test-ca"
          ENV["CLOUDFLARE_API_TOKEN"] = "test-token"

          deployment = create_test_deployment
          deployment.spec.regions = ["invalid-region"]
          controller = ContainerDeployment.new

          expect_raises(ContainerDeployment::Error, "Invalid region: invalid-region") do
            controller.reconcile(
              deployment.metadata.name,
              deployment.metadata.namespace,
              deployment.spec
            )
          end
        end
      end

      it "handles billing verification" do
        with_test_env do
          ENV["CLUSTER_EU_CENTRAL_TOKEN"] = "test-token"
          ENV["CLUSTER_EU_CENTRAL_CA"] = "test-ca"
          ENV["CLOUDFLARE_API_TOKEN"] = "test-token"
          ENV["STRIPE_API_KEY"] = "test-key"

          deployment = create_test_deployment
          deployment.spec.resources.cpu = "10" # Exceeds plan limit
          controller = ContainerDeployment.new

          result = controller.reconcile(
            deployment.metadata.name,
            deployment.metadata.namespace,
            deployment.spec
          )

          result.should be_false
        end
      end
    end

    describe "#cleanup" do
      it "removes deployments from all regions" do
        with_test_env do
          ENV["CLUSTER_EU_CENTRAL_TOKEN"] = "test-token"
          ENV["CLUSTER_EU_CENTRAL_CA"] = "test-ca"
          ENV["CLOUDFLARE_API_TOKEN"] = "test-token"

          deployment = create_test_deployment
          controller = ContainerDeployment.new

          result = controller.cleanup(
            deployment.metadata.name,
            deployment.metadata.namespace,
            deployment.spec
          )

          result.should be_true
        end
      end

      it "cleans up DNS records" do
        with_test_env do
          ENV["CLUSTER_EU_CENTRAL_TOKEN"] = "test-token"
          ENV["CLUSTER_EU_CENTRAL_CA"] = "test-ca"
          ENV["CLOUDFLARE_API_TOKEN"] = "test-token"

          deployment = create_test_deployment
          controller = ContainerDeployment.new

          result = controller.cleanup(
            deployment.metadata.name,
            deployment.metadata.namespace,
            deployment.spec
          )

          result.should be_true
        end
      end
    end
  end
end
