package stack

import (
	"context"

	"github.com/hashicorp/terraform-plugin-framework-timeouts/datasource/timeouts"
	"github.com/hashicorp/terraform-plugin-framework/datasource"
	"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
	"github.com/hashicorp/terraform-plugin-log/tflog"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/client"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/constants"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/stack/flattens"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/stack/models"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/stack/schemas"
)

// Ensure the implementation satisfies the expected interfaces.
var (
	_ datasource.DataSource = &dataSourceStack{}
)

func NewDataSourceStack() datasource.DataSource {
	return &dataSourceStack{}
}

// dataSourceStack is the data source implementation.
type dataSourceStack struct {
	client *client.Client
}

func (d *dataSourceStack) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
	// Read Terraform configuration data into the model
	var state models.InfoStackModel
	resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)
	if resp.Diagnostics.HasError() {
		tflog.Error(ctx, "Read dataSourceStack: Error get state")
		return
	}
	id := uint64(state.StackID.ValueInt64())
	tflog.Info(ctx, "Read dataSourceStack: got state successfully", map[string]any{"stack_id": id})

	// Set timeouts
	readTimeout, diags := state.Timeouts.Read(ctx, constants.Timeout30s)
	resp.Diagnostics.Append(diags...)
	if resp.Diagnostics.HasError() {
		tflog.Error(ctx, "Read dataSourceStack: Error set timeout")
		return
	}
	tflog.Info(ctx, "Read dataSourceStack: set timeouts successfully", map[string]any{
		"stack_id":    id,
		"readTimeout": readTimeout})

	ctx, cancel := context.WithTimeout(ctx, readTimeout)
	defer cancel()

	// Map response body to schema
	resp.Diagnostics.Append(flattens.StackDataSource(ctx, &state, d.client)...)
	if resp.Diagnostics.HasError() {
		tflog.Error(ctx, "Read dataSourceStack: Error flatten data source")
		return
	}

	// Set refreshed state
	resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
	if resp.Diagnostics.HasError() {
		tflog.Error(ctx, "Read dataSourceStack: Error set state")
		return
	}
	tflog.Info(ctx, "End read stack", map[string]any{"stack_id": id})
}

func (d *dataSourceStack) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
	resp.Schema = schema.Schema{
		Attributes: schemas.MakeSchemaDataSourceStack(),
		Blocks: map[string]schema.Block{
			"timeouts": timeouts.Block(ctx),
		},
	}
}

func (d *dataSourceStack) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
	resp.TypeName = req.ProviderTypeName + "_stack"
}

// Configure adds the provider configured client to the data source.
func (d *dataSourceStack) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
	tflog.Info(ctx, "Get Configure dataSourceStack")
	d.client = client.DataSource(ctx, &req, resp)
	tflog.Info(ctx, "Getting Configure dataSourceStack successfully")
}
