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

package cache_test

import (
	"context"
	"errors"
	"os"
	"testing"

	"github.com/cloudflare/cloudflare-go/v6"
	"github.com/cloudflare/cloudflare-go/v6/cache"
	"github.com/cloudflare/cloudflare-go/v6/internal/testutil"
	"github.com/cloudflare/cloudflare-go/v6/option"
)

func TestVariantDelete(t *testing.T) {
	baseURL := "http://localhost:4010"
	if envURL, ok := os.LookupEnv("TEST_API_BASE_URL"); ok {
		baseURL = envURL
	}
	if !testutil.CheckTestServer(t, baseURL) {
		return
	}
	client := cloudflare.NewClient(
		option.WithBaseURL(baseURL),
		option.WithAPIKey("144c9defac04969c7bfad8efaa8ea194"),
		option.WithAPIEmail("user@example.com"),
	)
	_, err := client.Cache.Variants.Delete(context.TODO(), cache.VariantDeleteParams{
		ZoneID: cloudflare.F("023e105f4ecef8ad9ca31a8372d0c353"),
	})
	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())
	}
}

func TestVariantEditWithOptionalParams(t *testing.T) {
	t.Skip("TODO: investigate HTTP 422 errors on test suite")
	baseURL := "http://localhost:4010"
	if envURL, ok := os.LookupEnv("TEST_API_BASE_URL"); ok {
		baseURL = envURL
	}
	if !testutil.CheckTestServer(t, baseURL) {
		return
	}
	client := cloudflare.NewClient(
		option.WithBaseURL(baseURL),
		option.WithAPIKey("144c9defac04969c7bfad8efaa8ea194"),
		option.WithAPIEmail("user@example.com"),
	)
	_, err := client.Cache.Variants.Edit(context.TODO(), cache.VariantEditParams{
		ZoneID: cloudflare.F("023e105f4ecef8ad9ca31a8372d0c353"),
		Value: cloudflare.F(cache.VariantEditParamsValue{
			AVIF: cloudflare.F([]string{"image/webp", "image/jpeg"}),
			BMP:  cloudflare.F([]string{"image/webp", "image/jpeg"}),
			GIF:  cloudflare.F([]string{"image/webp", "image/jpeg"}),
			JP2:  cloudflare.F([]string{"image/webp", "image/avif"}),
			JPEG: cloudflare.F([]string{"image/webp", "image/avif"}),
			JPG:  cloudflare.F([]string{"image/webp", "image/avif"}),
			JPG2: cloudflare.F([]string{"image/webp", "image/avif"}),
			PNG:  cloudflare.F([]string{"image/webp", "image/avif"}),
			TIF:  cloudflare.F([]string{"image/webp", "image/avif"}),
			TIFF: cloudflare.F([]string{"image/webp", "image/avif"}),
			WebP: cloudflare.F([]string{"image/jpeg", "image/avif"}),
		}),
	})
	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())
	}
}

func TestVariantGet(t *testing.T) {
	t.Skip("TODO: investigate HTTP 422 errors on test suite")
	baseURL := "http://localhost:4010"
	if envURL, ok := os.LookupEnv("TEST_API_BASE_URL"); ok {
		baseURL = envURL
	}
	if !testutil.CheckTestServer(t, baseURL) {
		return
	}
	client := cloudflare.NewClient(
		option.WithBaseURL(baseURL),
		option.WithAPIKey("144c9defac04969c7bfad8efaa8ea194"),
		option.WithAPIEmail("user@example.com"),
	)
	_, err := client.Cache.Variants.Get(context.TODO(), cache.VariantGetParams{
		ZoneID: cloudflare.F("023e105f4ecef8ad9ca31a8372d0c353"),
	})
	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())
	}
}
