// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

package browser_rendering_test

import (
	"bytes"
	"context"
	"errors"
	"io"
	"net/http"
	"net/http/httptest"
	"testing"

	"github.com/cloudflare/cloudflare-go/v6"
	"github.com/cloudflare/cloudflare-go/v6/browser_rendering"
	"github.com/cloudflare/cloudflare-go/v6/option"
	"github.com/cloudflare/cloudflare-go/v6/shared"
)

func TestPDFNewWithOptionalParams(t *testing.T) {
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(200)
		w.Write([]byte("abc"))
	}))
	defer server.Close()
	baseURL := server.URL
	client := cloudflare.NewClient(
		option.WithBaseURL(baseURL),
		option.WithAPIKey("144c9defac04969c7bfad8efaa8ea194"),
		option.WithAPIEmail("user@example.com"),
	)
	resp, err := client.BrowserRendering.PDF.New(context.TODO(), browser_rendering.PDFNewParams{
		AccountID:     cloudflare.F("account_id"),
		CacheTTL:      cloudflare.F(86400.000000),
		ActionTimeout: cloudflare.F(300000.000000),
		AddScriptTag: cloudflare.F([]browser_rendering.PDFNewParamsAddScriptTag{{
			ID:      cloudflare.F("id"),
			Content: cloudflare.F("content"),
			Type:    cloudflare.F("type"),
			URL:     cloudflare.F("url"),
		}}),
		AddStyleTag: cloudflare.F([]browser_rendering.PDFNewParamsAddStyleTag{{
			Content: cloudflare.F("content"),
			URL:     cloudflare.F("url"),
		}}),
		AllowRequestPattern: cloudflare.F([]string{"string"}),
		AllowResourceTypes:  cloudflare.F([]browser_rendering.PDFNewParamsAllowResourceType{browser_rendering.PDFNewParamsAllowResourceTypeDocument}),
		Authenticate: cloudflare.F(browser_rendering.PDFNewParamsAuthenticate{
			Password: cloudflare.F("x"),
			Username: cloudflare.F("x"),
		}),
		BestAttempt: cloudflare.F(true),
		Cookies: cloudflare.F([]browser_rendering.PDFNewParamsCookie{{
			Name:         cloudflare.F("name"),
			Value:        cloudflare.F("value"),
			Domain:       cloudflare.F("domain"),
			Expires:      cloudflare.F(0.000000),
			HTTPOnly:     cloudflare.F(true),
			PartitionKey: cloudflare.F("partitionKey"),
			Path:         cloudflare.F("path"),
			Priority:     cloudflare.F(browser_rendering.PDFNewParamsCookiesPriorityLow),
			SameParty:    cloudflare.F(true),
			SameSite:     cloudflare.F(browser_rendering.PDFNewParamsCookiesSameSiteStrict),
			Secure:       cloudflare.F(true),
			SourcePort:   cloudflare.F(0.000000),
			SourceScheme: cloudflare.F(browser_rendering.PDFNewParamsCookiesSourceSchemeUnset),
			URL:          cloudflare.F("url"),
		}}),
		EmulateMediaType: cloudflare.F("emulateMediaType"),
		GotoOptions: cloudflare.F(browser_rendering.PDFNewParamsGotoOptions{
			Referer:        cloudflare.F("referer"),
			ReferrerPolicy: cloudflare.F("referrerPolicy"),
			Timeout:        cloudflare.F(60000.000000),
			WaitUntil:      cloudflare.F[browser_rendering.PDFNewParamsGotoOptionsWaitUntilUnion](browser_rendering.PDFNewParamsGotoOptionsWaitUntilString(browser_rendering.PDFNewParamsGotoOptionsWaitUntilStringLoad)),
		}),
		HTML: cloudflare.F("x"),
		PDFOptions: cloudflare.F(browser_rendering.PDFNewParamsPDFOptions{
			DisplayHeaderFooter: cloudflare.F(true),
			FooterTemplate:      cloudflare.F("footerTemplate"),
			Format:              cloudflare.F(browser_rendering.PDFNewParamsPDFOptionsFormatLetter),
			HeaderTemplate:      cloudflare.F("headerTemplate"),
			Height:              cloudflare.F[browser_rendering.PDFNewParamsPDFOptionsHeightUnion](shared.UnionString("string")),
			Landscape:           cloudflare.F(true),
			Margin: cloudflare.F(browser_rendering.PDFNewParamsPDFOptionsMargin{
				Bottom: cloudflare.F[browser_rendering.PDFNewParamsPDFOptionsMarginBottomUnion](shared.UnionString("string")),
				Left:   cloudflare.F[browser_rendering.PDFNewParamsPDFOptionsMarginLeftUnion](shared.UnionString("string")),
				Right:  cloudflare.F[browser_rendering.PDFNewParamsPDFOptionsMarginRightUnion](shared.UnionString("string")),
				Top:    cloudflare.F[browser_rendering.PDFNewParamsPDFOptionsMarginTopUnion](shared.UnionString("string")),
			}),
			OmitBackground:    cloudflare.F(true),
			Outline:           cloudflare.F(true),
			PageRanges:        cloudflare.F("pageRanges"),
			PreferCSSPageSize: cloudflare.F(true),
			PrintBackground:   cloudflare.F(true),
			Scale:             cloudflare.F(0.100000),
			Tagged:            cloudflare.F(true),
			Timeout:           cloudflare.F(0.000000),
			Width:             cloudflare.F[browser_rendering.PDFNewParamsPDFOptionsWidthUnion](shared.UnionString("string")),
		}),
		RejectRequestPattern: cloudflare.F([]string{"string"}),
		RejectResourceTypes:  cloudflare.F([]browser_rendering.PDFNewParamsRejectResourceType{browser_rendering.PDFNewParamsRejectResourceTypeDocument}),
		SetExtraHTTPHeaders: cloudflare.F(map[string]string{
			"foo": "string",
		}),
		SetJavaScriptEnabled: cloudflare.F(true),
		URL:                  cloudflare.F("https://example.com"),
		UserAgent:            cloudflare.F("userAgent"),
		Viewport: cloudflare.F(browser_rendering.PDFNewParamsViewport{
			Height:            cloudflare.F(0.000000),
			Width:             cloudflare.F(0.000000),
			DeviceScaleFactor: cloudflare.F(0.000000),
			HasTouch:          cloudflare.F(true),
			IsLandscape:       cloudflare.F(true),
			IsMobile:          cloudflare.F(true),
		}),
		WaitForSelector: cloudflare.F(browser_rendering.PDFNewParamsWaitForSelector{
			Selector: cloudflare.F("selector"),
			Hidden:   cloudflare.F(browser_rendering.PDFNewParamsWaitForSelectorHiddenTrue),
			Timeout:  cloudflare.F(60000.000000),
			Visible:  cloudflare.F(browser_rendering.PDFNewParamsWaitForSelectorVisibleTrue),
		}),
		WaitForTimeout: cloudflare.F(60000.000000),
	})
	if err != nil {
		var apierr *cloudflare.Error
		if errors.As(err, &apierr) {
			t.Log(string(apierr.DumpRequest(true)))
		}
		t.Fatalf("err should be nil: %s", err.Error())
	}
	defer resp.Body.Close()

	b, err := io.ReadAll(resp.Body)
	if err != nil {
		var apierr *cloudflare.Error
		if errors.As(err, &apierr) {
			t.Log(string(apierr.DumpRequest(true)))
		}
		t.Fatalf("err should be nil: %s", err.Error())
	}
	if !bytes.Equal(b, []byte("abc")) {
		t.Fatalf("return value not %s: %s", "abc", b)
	}
}
