// 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 = &SourceDefinitionDataSource{}
var _ datasource.DataSourceWithConfigure = &SourceDefinitionDataSource{}

func NewSourceDefinitionDataSource() datasource.DataSource {
	return &SourceDefinitionDataSource{}
}

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

// SourceDefinitionDataSourceModel describes the data model.
type SourceDefinitionDataSourceModel struct {
	DockerImageTag   types.String `tfsdk:"docker_image_tag"`
	DockerRepository types.String `tfsdk:"docker_repository"`
	DocumentationURL types.String `tfsdk:"documentation_url"`
	ID               types.String `tfsdk:"id"`
	Name             types.String `tfsdk:"name"`
	WorkspaceID      types.String `tfsdk:"workspace_id"`
}

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

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

		Attributes: map[string]schema.Attribute{
			"docker_image_tag": schema.StringAttribute{
				Computed: true,
			},
			"docker_repository": schema.StringAttribute{
				Computed: true,
			},
			"documentation_url": schema.StringAttribute{
				Computed: true,
			},
			"id": schema.StringAttribute{
				Computed: true,
			},
			"name": schema.StringAttribute{
				Computed: true,
			},
			"workspace_id": schema.StringAttribute{
				Required: true,
			},
		},
	}
}

func (r *SourceDefinitionDataSource) 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 *SourceDefinitionDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
	var data *SourceDefinitionDataSourceModel
	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.GetSourceDefinitionRequest{
		WorkspaceID:  workspaceID,
		DefinitionID: definitionID,
	}
	res, err := r.client.SourceDefinitions.GetSourceDefinition(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.DefinitionResponse != nil) {
		resp.Diagnostics.AddError("unexpected response from API. Got an unexpected response body", debugResponse(res.RawResponse))
		return
	}
	data.RefreshFromSharedDefinitionResponse(res.DefinitionResponse)

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