// Copyright (c) 2025 Aiven, Helsinki, Finland. https://aiven.io/
// Code generated by user config generator. DO NOT EDIT.

package organization

import (
	"context"
	"fmt"
	"path/filepath"

	"github.com/hashicorp/terraform-plugin-framework/diag"
	"github.com/hashicorp/terraform-plugin-framework/types"

	"github.com/aiven/terraform-provider-aiven/internal/plugin/util"
)

const aivenName = "aiven_organization"

type tfModel struct {
	ID         types.String `tfsdk:"id"`
	CreateTime types.String `tfsdk:"create_time"`
	Name       types.String `tfsdk:"name"`
	TenantID   types.String `tfsdk:"tenant_id"`
	UpdateTime types.String `tfsdk:"update_time"`
}

func (tf *tfModel) SetID(vID string) {
	tf.ID = types.StringValue(filepath.Join(vID))
}

type apiModel struct {
	CreateTime *string `json:"create_time,omitempty"`
	ID         *string `json:"organization_id,omitempty"`
	Name       *string `json:"account_name,omitempty"`
	TenantID   *string `json:"tenant_id,omitempty"`
	UpdateTime *string `json:"update_time,omitempty"`
}

// composeID the ID attribute fields, i.e.:
// terraform import aiven_organization.foo ID
func composeID() []string {
	return []string{"id"}
}

// expandData turns TF object into Request
func expandData[R any](ctx context.Context, plan, state *tfModel, req *R, modifiers ...util.MapModifier[apiModel]) diag.Diagnostics {
	api := new(apiModel)
	if !plan.Name.IsNull() || state != nil && !state.Name.IsNull() {
		vName := plan.Name.ValueString()
		api.Name = &vName
	}
	err := util.Unmarshal(api, req, modifiers...)
	if err != nil {
		var diags diag.Diagnostics
		diags.AddError("Unmarshal error", fmt.Sprintf("Failed to unmarshal dtoModel to Request: %s", err.Error()))
		return diags
	}
	return nil
}

// flattenData turns Response into TF object
func flattenData[R any](ctx context.Context, state *tfModel, rsp *R, modifiers ...util.MapModifier[R]) diag.Diagnostics {
	api := new(apiModel)
	err := util.Unmarshal(rsp, api, modifiers...)
	if err != nil {
		var diags diag.Diagnostics
		diags.AddError("Unmarshal error", fmt.Sprintf("Failed to unmarshal Response to dtoModel: %s", err.Error()))
		return diags
	}
	if api.CreateTime != nil && (*api.CreateTime != "" || !state.CreateTime.IsNull()) {
		state.CreateTime = types.StringPointerValue(api.CreateTime)
	}
	if api.ID != nil && (*api.ID != "" || !state.ID.IsNull()) {
		state.ID = types.StringPointerValue(api.ID)
	}
	if api.Name != nil && (*api.Name != "" || !state.Name.IsNull()) {
		state.Name = types.StringPointerValue(api.Name)
	}
	if api.TenantID != nil && (*api.TenantID != "" || !state.TenantID.IsNull()) {
		state.TenantID = types.StringPointerValue(api.TenantID)
	}
	if api.UpdateTime != nil && (*api.UpdateTime != "" || !state.UpdateTime.IsNull()) {
		state.UpdateTime = types.StringPointerValue(api.UpdateTime)
	}
	return nil
}
