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

package firewall_test

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

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

func TestLockdownNewWithOptionalParams(t *testing.T) {
	t.Skip("TODO: investigate broken test")
	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.Firewall.Lockdowns.New(context.TODO(), firewall.LockdownNewParams{
		ZoneID: cloudflare.F("023e105f4ecef8ad9ca31a8372d0c353"),
		Configurations: cloudflare.F(firewall.ConfigurationParam{firewall.LockdownIPConfigurationParam{
			Target: cloudflare.F(firewall.LockdownIPConfigurationTargetIP),
			Value:  cloudflare.F("198.51.100.4"),
		}}),
		URLs:        cloudflare.F([]firewall.OverrideURLParam{"shop.example.com/*"}),
		Description: cloudflare.F("Prevent multiple login failures to mitigate brute force attacks"),
		Paused:      cloudflare.F(false),
		Priority:    cloudflare.F(5.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())
	}
}

func TestLockdownUpdate(t *testing.T) {
	t.Skip("TODO: investigate broken test")
	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.Firewall.Lockdowns.Update(
		context.TODO(),
		"372e67954025e0ba6aaa6d586b9e0b59",
		firewall.LockdownUpdateParams{
			ZoneID: cloudflare.F("023e105f4ecef8ad9ca31a8372d0c353"),
			Configurations: cloudflare.F(firewall.ConfigurationParam{firewall.LockdownIPConfigurationParam{
				Target: cloudflare.F(firewall.LockdownIPConfigurationTargetIP),
				Value:  cloudflare.F("198.51.100.4"),
			}}),
			URLs: cloudflare.F([]firewall.OverrideURLParam{"shop.example.com/*"}),
		},
	)
	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 TestLockdownListWithOptionalParams(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.Firewall.Lockdowns.List(context.TODO(), firewall.LockdownListParams{
		ZoneID:            cloudflare.F("023e105f4ecef8ad9ca31a8372d0c353"),
		CreatedOn:         cloudflare.F(time.Now()),
		Description:       cloudflare.F("endpoints"),
		DescriptionSearch: cloudflare.F("endpoints"),
		IP:                cloudflare.F("1.2.3.4"),
		IPRangeSearch:     cloudflare.F("1.2.3.0/16"),
		IPSearch:          cloudflare.F("1.2.3.4"),
		ModifiedOn:        cloudflare.F(time.Now()),
		Page:              cloudflare.F(1.000000),
		PerPage:           cloudflare.F(1.000000),
		Priority:          cloudflare.F(5.000000),
		URISearch:         cloudflare.F("/some/path"),
	})
	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 TestLockdownDelete(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.Firewall.Lockdowns.Delete(
		context.TODO(),
		"372e67954025e0ba6aaa6d586b9e0b59",
		firewall.LockdownDeleteParams{
			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 TestLockdownGet(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.Firewall.Lockdowns.Get(
		context.TODO(),
		"372e67954025e0ba6aaa6d586b9e0b59",
		firewall.LockdownGetParams{
			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())
	}
}
