package flattens

import (
	"context"

	"github.com/hashicorp/terraform-plugin-framework/diag"
	"github.com/hashicorp/terraform-plugin-framework/types"
	"github.com/hashicorp/terraform-plugin-log/tflog"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/client"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/bservice/models"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/bservice/utilities"
)

// BServicesDeletedListDataSource flattens data source for a list of basic services.
// Return error in case data source is not found on the platform.
// Flatten errors are added to tflog.
func BServicesDeletedListDataSource(ctx context.Context, state *models.ListBasicServicesDelModel, c *client.Client) diag.Diagnostics {
	tflog.Info(ctx, "Start flattens.BServicesDeletedListDataSource")

	diags := diag.Diagnostics{}

	// Fetch the list of basic services from the API
	recordList, err := utilities.BServiceDeletedListDataSourceCheckPresence(ctx, *state, c)
	if err != nil {
		diags.AddError("Cannot get list of basic services", err.Error())
		return diags
	}

	tflog.Info(ctx, "flattens.BServicesDeletedListDataSource: before flatten", map[string]any{"record_list": recordList})

	// Flatten the fetched data into the state
	var itemList []models.ItemBasicServiceDelModel
	for _, record := range recordList.Data {
		item := models.ItemBasicServiceDelModel{
			AccountID:     types.Int64Value(int64(record.AccountID)),
			AccountName:   types.StringValue(record.AccountName),
			BaseDomain:    types.StringValue(record.BaseDomain),
			CreatedBy:     types.StringValue(record.CreatedBy),
			CreatedTime:   types.Int64Value(int64(record.CreatedTime)),
			DeletedBy:     types.StringValue(record.DeletedBy),
			DeletedTime:   types.Int64Value(int64(record.DeletedTime)),
			GID:           types.Int64Value(int64(record.GID)),
			GUID:          types.Int64Value(int64(record.GUID)),
			ID:            types.Int64Value(int64(record.ID)),
			Name:          types.StringValue(record.Name),
			ParentSrvID:   types.Int64Value(int64(record.ParentSrvID)),
			ParentSrvType: types.StringValue(record.ParentSrvType),
			RGID:          types.Int64Value(int64(record.RGID)),
			RGName:        types.StringValue(record.RGName),
			SSHUser:       types.StringValue(record.SSHUser),
			Status:        types.StringValue(record.Status),
			TechStatus:    types.StringValue(record.TechStatus),
			UpdatedBy:     types.StringValue(record.UpdatedBy),
			UpdatedTime:   types.Int64Value(int64(record.UpdatedTime)),
			UserManaged:   types.BoolValue(record.UserManaged),
		}

		// Handle groups as a list of strings
		groupList := make([]types.Int64, len(record.Groups))
		for i, group := range record.Groups {
			groupList[i] = types.Int64Value(int64(group))
		}

		itemList = append(itemList, item)
	}

	// Update state with the flattened data
	state.Data = itemList
	state.EntryCount = types.Int64Value(int64(len(itemList)))

	tflog.Info(ctx, "flattens.BServicesDeletedListDataSource: after flatten", map[string]any{"entry_count": state.EntryCount.ValueInt64()})
	tflog.Info(ctx, "End flattens.BServicesDeletedListDataSource")

	return nil
}
