// Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.

package provider

import (
	"context"
	"fmt"
	"github.com/airbytehq/terraform-provider-airbyte/internal/sdk"
	"github.com/airbytehq/terraform-provider-airbyte/internal/sdk/models/operations"
	"github.com/hashicorp/terraform-plugin-framework/datasource"
	"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
	"github.com/hashicorp/terraform-plugin-framework/types"
	"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
)

// Ensure provider defined types fully satisfy framework interfaces.
var _ datasource.DataSource = &DeclarativeSourceDefinitionDataSource{}
var _ datasource.DataSourceWithConfigure = &DeclarativeSourceDefinitionDataSource{}

func NewDeclarativeSourceDefinitionDataSource() datasource.DataSource {
	return &DeclarativeSourceDefinitionDataSource{}
}

// DeclarativeSourceDefinitionDataSource is the data source implementation.
type DeclarativeSourceDefinitionDataSource struct {
	client *sdk.SDK
}

// DeclarativeSourceDefinitionDataSourceModel describes the data model.
type DeclarativeSourceDefinitionDataSourceModel struct {
	ID          types.String `tfsdk:"id"`
	Manifest    types.String `tfsdk:"manifest"`
	Name        types.String `tfsdk:"name"`
	Version     types.Int64  `tfsdk:"version"`
	WorkspaceID types.String `tfsdk:"workspace_id"`
}

// Metadata returns the data source type name.
func (r *DeclarativeSourceDefinitionDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
	resp.TypeName = req.ProviderTypeName + "_declarative_source_definition"
}

// Schema defines the schema for the data source.
func (r *DeclarativeSourceDefinitionDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
	resp.Schema = schema.Schema{
		MarkdownDescription: "DeclarativeSourceDefinition DataSource",

		Attributes: map[string]schema.Attribute{
			"id": schema.StringAttribute{
				Computed: true,
			},
			"manifest": schema.StringAttribute{
				Computed:    true,
				Description: `Low code CDK manifest JSON object. Parsed as JSON.`,
			},
			"name": schema.StringAttribute{
				Computed: true,
			},
			"version": schema.Int64Attribute{
				Computed: true,
			},
			"workspace_id": schema.StringAttribute{
				Required: true,
			},
		},
	}
}

func (r *DeclarativeSourceDefinitionDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
	// Prevent panic if the provider has not been configured.
	if req.ProviderData == nil {
		return
	}

	client, ok := req.ProviderData.(*sdk.SDK)

	if !ok {
		resp.Diagnostics.AddError(
			"Unexpected DataSource Configure Type",
			fmt.Sprintf("Expected *sdk.SDK, got: %T. Please report this issue to the provider developers.", req.ProviderData),
		)

		return
	}

	r.client = client
}

func (r *DeclarativeSourceDefinitionDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
	var data *DeclarativeSourceDefinitionDataSourceModel
	var item types.Object

	resp.Diagnostics.Append(req.Config.Get(ctx, &item)...)
	if resp.Diagnostics.HasError() {
		return
	}

	resp.Diagnostics.Append(item.As(ctx, &data, basetypes.ObjectAsOptions{
		UnhandledNullAsEmpty:    true,
		UnhandledUnknownAsEmpty: true,
	})...)

	if resp.Diagnostics.HasError() {
		return
	}

	var workspaceID string
	workspaceID = data.WorkspaceID.ValueString()

	var definitionID string
	definitionID = data.ID.ValueString()

	request := operations.GetDeclarativeSourceDefinitionRequest{
		WorkspaceID:  workspaceID,
		DefinitionID: definitionID,
	}
	res, err := r.client.DeclarativeSourceDefinitions.GetDeclarativeSourceDefinition(ctx, request)
	if err != nil {
		resp.Diagnostics.AddError("failure to invoke API", err.Error())
		if res != nil && res.RawResponse != nil {
			resp.Diagnostics.AddError("unexpected http request/response", debugResponse(res.RawResponse))
		}
		return
	}
	if res == nil {
		resp.Diagnostics.AddError("unexpected response from API", fmt.Sprintf("%v", res))
		return
	}
	if res.StatusCode == 404 {
		resp.State.RemoveResource(ctx)
		return
	}
	if res.StatusCode != 200 {
		resp.Diagnostics.AddError(fmt.Sprintf("unexpected response from API. Got an unexpected response code %v", res.StatusCode), debugResponse(res.RawResponse))
		return
	}
	if !(res.DeclarativeSourceDefinitionResponse != nil) {
		resp.Diagnostics.AddError("unexpected response from API. Got an unexpected response body", debugResponse(res.RawResponse))
		return
	}
	data.RefreshFromSharedDeclarativeSourceDefinitionResponse(res.DeclarativeSourceDefinitionResponse)

	// Save updated data into Terraform state
	resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
