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

package api_shield_discovery_operation

import (
	"context"

	"github.com/cloudflare/terraform-provider-cloudflare/internal/customfield"
	"github.com/hashicorp/terraform-plugin-framework-timetypes/timetypes"
	"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
	"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
	"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
	"github.com/hashicorp/terraform-plugin-framework/datasource"
	"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
	"github.com/hashicorp/terraform-plugin-framework/schema/validator"
	"github.com/hashicorp/terraform-plugin-framework/types"
)

var _ datasource.DataSourceWithConfigValidators = (*APIShieldDiscoveryOperationsDataSource)(nil)

func ListDataSourceSchema(ctx context.Context) schema.Schema {
	return schema.Schema{
		Attributes: map[string]schema.Attribute{
			"zone_id": schema.StringAttribute{
				Description: "Identifier.",
				Required:    true,
			},
			"diff": schema.BoolAttribute{
				Description: "When `true`, only return API Discovery results that are not saved into API Shield Endpoint Management",
				Optional:    true,
			},
			"direction": schema.StringAttribute{
				Description: "Direction to order results.\nAvailable values: \"asc\", \"desc\".",
				Optional:    true,
				Validators: []validator.String{
					stringvalidator.OneOfCaseInsensitive("asc", "desc"),
				},
			},
			"endpoint": schema.StringAttribute{
				Description: "Filter results to only include endpoints containing this pattern.",
				Optional:    true,
			},
			"order": schema.StringAttribute{
				Description: "Field to order by\nAvailable values: \"host\", \"method\", \"endpoint\", \"traffic_stats.requests\", \"traffic_stats.last_updated\".",
				Optional:    true,
				Validators: []validator.String{
					stringvalidator.OneOfCaseInsensitive(
						"host",
						"method",
						"endpoint",
						"traffic_stats.requests",
						"traffic_stats.last_updated",
					),
				},
			},
			"origin": schema.StringAttribute{
				Description: "Filter results to only include discovery results sourced from a particular discovery engine\n  * `ML` - Discovered operations that were sourced using ML API Discovery\n  * `SessionIdentifier` - Discovered operations that were sourced using Session Identifier API Discovery\nAvailable values: \"ML\", \"SessionIdentifier\", \"LabelDiscovery\".",
				Optional:    true,
				Validators: []validator.String{
					stringvalidator.OneOfCaseInsensitive(
						"ML",
						"SessionIdentifier",
						"LabelDiscovery",
					),
				},
			},
			"state": schema.StringAttribute{
				Description: "Filter results to only include discovery results in a particular state. States are as follows\n  * `review` - Discovered operations that are not saved into API Shield Endpoint Management\n  * `saved` - Discovered operations that are already saved into API Shield Endpoint Management\n  * `ignored` - Discovered operations that have been marked as ignored\nAvailable values: \"review\", \"saved\", \"ignored\".",
				Optional:    true,
				Validators: []validator.String{
					stringvalidator.OneOfCaseInsensitive(
						"review",
						"saved",
						"ignored",
					),
				},
			},
			"host": schema.ListAttribute{
				Description: "Filter results to only include the specified hosts.",
				Optional:    true,
				ElementType: types.StringType,
			},
			"method": schema.ListAttribute{
				Description: "Filter results to only include the specified HTTP methods.",
				Optional:    true,
				ElementType: types.StringType,
			},
			"max_items": schema.Int64Attribute{
				Description: "Max items to fetch, default: 1000",
				Optional:    true,
				Validators: []validator.Int64{
					int64validator.AtLeast(0),
				},
			},
			"result": schema.ListNestedAttribute{
				Description: "The items returned by the data source",
				Computed:    true,
				CustomType:  customfield.NewNestedObjectListType[APIShieldDiscoveryOperationsResultDataSourceModel](ctx),
				NestedObject: schema.NestedAttributeObject{
					Attributes: map[string]schema.Attribute{
						"id": schema.StringAttribute{
							Description: "UUID.",
							Computed:    true,
						},
						"endpoint": schema.StringAttribute{
							Description: "The endpoint which can contain path parameter templates in curly braces, each will be replaced from left to right with {varN}, starting with {var1}, during insertion. This will further be Cloudflare-normalized upon insertion. See: https://developers.cloudflare.com/rules/normalization/how-it-works/.",
							Computed:    true,
						},
						"host": schema.StringAttribute{
							Description: "RFC3986-compliant host.",
							Computed:    true,
						},
						"last_updated": schema.StringAttribute{
							Computed:   true,
							CustomType: timetypes.RFC3339Type{},
						},
						"method": schema.StringAttribute{
							Description: "The HTTP method used to access the endpoint.\nAvailable values: \"GET\", \"POST\", \"HEAD\", \"OPTIONS\", \"PUT\", \"DELETE\", \"CONNECT\", \"PATCH\", \"TRACE\".",
							Computed:    true,
							Validators: []validator.String{
								stringvalidator.OneOfCaseInsensitive(
									"GET",
									"POST",
									"HEAD",
									"OPTIONS",
									"PUT",
									"DELETE",
									"CONNECT",
									"PATCH",
									"TRACE",
								),
							},
						},
						"origin": schema.ListAttribute{
							Description: "API discovery engine(s) that discovered this operation",
							Computed:    true,
							Validators: []validator.List{
								listvalidator.ValueStringsAre(
									stringvalidator.OneOfCaseInsensitive(
										"ML",
										"SessionIdentifier",
										"LabelDiscovery",
									),
								),
							},
							CustomType:  customfield.NewListType[types.String](ctx),
							ElementType: types.StringType,
						},
						"state": schema.StringAttribute{
							Description: "State of operation in API Discovery\n  * `review` - Operation is not saved into API Shield Endpoint Management\n  * `saved` - Operation is saved into API Shield Endpoint Management\n  * `ignored` - Operation is marked as ignored\nAvailable values: \"review\", \"saved\", \"ignored\".",
							Computed:    true,
							Validators: []validator.String{
								stringvalidator.OneOfCaseInsensitive(
									"review",
									"saved",
									"ignored",
								),
							},
						},
						"features": schema.SingleNestedAttribute{
							Computed:   true,
							CustomType: customfield.NewNestedObjectType[APIShieldDiscoveryOperationsFeaturesDataSourceModel](ctx),
							Attributes: map[string]schema.Attribute{
								"traffic_stats": schema.SingleNestedAttribute{
									Computed:   true,
									CustomType: customfield.NewNestedObjectType[APIShieldDiscoveryOperationsFeaturesTrafficStatsDataSourceModel](ctx),
									Attributes: map[string]schema.Attribute{
										"last_updated": schema.StringAttribute{
											Computed:   true,
											CustomType: timetypes.RFC3339Type{},
										},
										"period_seconds": schema.Int64Attribute{
											Description: "The period in seconds these statistics were computed over",
											Computed:    true,
										},
										"requests": schema.Float64Attribute{
											Description: "The average number of requests seen during this period",
											Computed:    true,
										},
									},
								},
							},
						},
					},
				},
			},
		},
	}
}

func (d *APIShieldDiscoveryOperationsDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
	resp.Schema = ListDataSourceSchema(ctx)
}

func (d *APIShieldDiscoveryOperationsDataSource) ConfigValidators(_ context.Context) []datasource.ConfigValidator {
	return []datasource.ConfigValidator{}
}
