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

package pages_test

import (
	"bytes"
	"context"
	"errors"
	"io"
	"os"
	"testing"

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

func TestProjectDeploymentNewWithOptionalParams(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.Pages.Projects.Deployments.New(
		context.TODO(),
		"this-is-my-project-01",
		pages.ProjectDeploymentNewParams{
			AccountID:                          cloudflare.F("023e105f4ecef8ad9ca31a8372d0c353"),
			Headers:                            cloudflare.F(io.Reader(bytes.NewBuffer([]byte("some file contents")))),
			Redirects:                          cloudflare.F(io.Reader(bytes.NewBuffer([]byte("some file contents")))),
			RoutesJson:                         cloudflare.F(io.Reader(bytes.NewBuffer([]byte("some file contents")))),
			WorkerBundle:                       cloudflare.F(io.Reader(bytes.NewBuffer([]byte("some file contents")))),
			WorkerJS:                           cloudflare.F(io.Reader(bytes.NewBuffer([]byte("some file contents")))),
			Branch:                             cloudflare.F("staging"),
			CommitDirty:                        cloudflare.F(pages.ProjectDeploymentNewParamsCommitDirtyFalse),
			CommitHash:                         cloudflare.F("a1b2c3d4e5f6"),
			CommitMessage:                      cloudflare.F("Update homepage"),
			FunctionsFilepathRoutingConfigJson: cloudflare.F(io.Reader(bytes.NewBuffer([]byte("some file contents")))),
			Manifest:                           cloudflare.F(`{"index.html": "abc123", "style.css": "def456"}`),
			PagesBuildOutputDir:                cloudflare.F("dist"),
			WranglerConfigHash:                 cloudflare.F("wrangler_config_hash"),
		},
	)
	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 TestProjectDeploymentListWithOptionalParams(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.Pages.Projects.Deployments.List(
		context.TODO(),
		"this-is-my-project-01",
		pages.ProjectDeploymentListParams{
			AccountID: cloudflare.F("023e105f4ecef8ad9ca31a8372d0c353"),
			Env:       cloudflare.F(pages.ProjectDeploymentListParamsEnvPreview),
			Page:      cloudflare.F(int64(1)),
			PerPage:   cloudflare.F(int64(10)),
		},
	)
	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 TestProjectDeploymentDelete(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.Pages.Projects.Deployments.Delete(
		context.TODO(),
		"this-is-my-project-01",
		"023e105f4ecef8ad9ca31a8372d0c353",
		pages.ProjectDeploymentDeleteParams{
			AccountID: 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 TestProjectDeploymentGet(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.Pages.Projects.Deployments.Get(
		context.TODO(),
		"this-is-my-project-01",
		"023e105f4ecef8ad9ca31a8372d0c353",
		pages.ProjectDeploymentGetParams{
			AccountID: 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 TestProjectDeploymentRetry(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.Pages.Projects.Deployments.Retry(
		context.TODO(),
		"this-is-my-project-01",
		"023e105f4ecef8ad9ca31a8372d0c353",
		pages.ProjectDeploymentRetryParams{
			AccountID: 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 TestProjectDeploymentRollback(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.Pages.Projects.Deployments.Rollback(
		context.TODO(),
		"this-is-my-project-01",
		"023e105f4ecef8ad9ca31a8372d0c353",
		pages.ProjectDeploymentRollbackParams{
			AccountID: 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())
	}
}
