require "./spec_helper"

module ContainerMom
  describe DNSManager do
    describe "#configure_domain" do
      it "configures custom domain" do
        with_test_env do
          ENV["CLOUDFLARE_API_TOKEN"] = "test-token"
          manager = DNSManager.instance
          deployment = create_test_deployment

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

          result.should be_true
        end
      end

      it "generates random subdomain when custom domain not provided" do
        with_test_env do
          ENV["CLOUDFLARE_API_TOKEN"] = "test-token"
          manager = DNSManager.instance
          deployment = create_test_deployment
          deployment.spec.domain.custom = nil

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

          result.should be_true
        end
      end

      it "validates domain format" do
        with_test_env do
          ENV["CLOUDFLARE_API_TOKEN"] = "test-token"
          manager = DNSManager.instance
          deployment = create_test_deployment
          deployment.spec.domain.custom = "invalid domain"

          expect_raises(DNSManager::Error, "Invalid domain format") do
            manager.configure_domain(
              deployment.metadata.name,
              deployment.metadata.namespace,
              deployment.spec
            )
          end
        end
      end
    end

    describe "#cleanup_domain" do
      it "removes DNS records" do
        with_test_env do
          ENV["CLOUDFLARE_API_TOKEN"] = "test-token"
          manager = DNSManager.instance
          deployment = create_test_deployment

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

          result.should be_true
        end
      end

      it "handles missing domain gracefully" do
        with_test_env do
          ENV["CLOUDFLARE_API_TOKEN"] = "test-token"
          manager = DNSManager.instance
          deployment = create_test_deployment
          deployment.spec.domain.custom = nil

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

          result.should be_true
        end
      end
    end

    describe "#generate_subdomain" do
      it "generates unique mom-themed subdomains" do
        manager = DNSManager.instance
        subdomain = manager.generate_subdomain

        subdomain.should contain("mom")
        subdomain.should match(/^[a-z0-9-]+$/)
      end

      it "generates different subdomains each time" do
        manager = DNSManager.instance
        subdomain1 = manager.generate_subdomain
        subdomain2 = manager.generate_subdomain

        subdomain1.should_not eq(subdomain2)
      end
    end

    describe "#load_config" do
      it "loads configuration from environment" do
        with_test_env do
          ENV["CLOUDFLARE_API_TOKEN"] = "test-token"
          manager = DNSManager.instance
          # Configuration loaded successfully
        end
      end

      it "raises error when API token not configured" do
        with_test_env do
          expect_raises(DNSManager::Error, "CLOUDFLARE_API_TOKEN not configured") do
            DNSManager.instance
          end
        end
      end
    end
  end
end
