/*
Code generated by github-terraform-generator; DO NOT EDIT.
Any modifications will be overwritten
*/

package tests

import (
	"context"
	"fmt"
	structs "github.com/Lenstra/terraform-plugin-generator/tests/structs"
	diag "github.com/hashicorp/terraform-plugin-framework/diag"
	types "github.com/hashicorp/terraform-plugin-framework/types"
)

type Setter interface {
	Set(context.Context, interface{}) diag.Diagnostics
}

func Set[Model *structs.Coffee | *structs.Config | *structs.Ingredient](ctx context.Context, setter Setter, obj Model) diag.Diagnostics {
	var diags diag.Diagnostics
	var converted interface{}
	switch o := any(obj).(type) {
	case *structs.Coffee:
		converted, diags = EncodeCoffee(o)
	case *structs.Config:
		converted, diags = EncodeConfig(o)
	case *structs.Ingredient:
		converted, diags = EncodeIngredient(o)
	default:
		diags.AddError("unsupported object type", fmt.Sprintf("%T is not supported in %s.Set(). Please report this issue to the provider developers.", obj, "tests"))
		return diags
	}
	diags.Append(setter.Set(ctx, converted)...)
	return diags
}

func EncodeCoffee(coffee *structs.Coffee) (*Coffee, diag.Diagnostics) {
	if coffee == nil {
		return nil, nil
	}

	var diags diag.Diagnostics
	res := Coffee{}
	res.ID = types.Int64Value(int64(coffee.ID))
	res.Name = types.StringValue(coffee.Name)
	res.Teaser = types.StringValue(coffee.Teaser)
	res.Description = types.StringValue(coffee.Description)
	res.Image = types.StringValue(coffee.Image)
	if coffee.Ingredients != nil {
		res.Ingredients = make([]*Ingredient, len(coffee.Ingredients))
		for i, attr := range coffee.Ingredients {
			{
				data, d := EncodeIngredient(&attr)
				diags.Append(d...)
				if diags.HasError() {
					return nil, diags
				}
				if data != nil {
					res.Ingredients[i] = data
				}
			}
		}
	}
	{
		data, d := encodeCustomer(coffee.Customer)
		diags.Append(d...)
		if diags.HasError() {
			return nil, diags
		}
		if data != nil {
			res.Customer = data
		}
	}
	return &res, diags
}

func EncodeConfig(config *structs.Config) (*Config, diag.Diagnostics) {
	if config == nil {
		return nil, nil
	}

	var diags diag.Diagnostics
	res := Config{}
	res.Host = types.StringValue(config.Host)
	res.Bool = types.BoolValue(config.PromotedBool.Bool)
	res.Int = types.Int64Value(int64(config.PromotedInt.Int))
	res.String = types.StringValue(config.PromotedString.String)
	return &res, diags
}

func EncodeIngredient(ingredient *structs.Ingredient) (*Ingredient, diag.Diagnostics) {
	if ingredient == nil {
		return nil, nil
	}

	var diags diag.Diagnostics
	res := Ingredient{}
	res.ID = types.Int64Value(int64(ingredient.ID))
	res.Float32 = types.Float64Value(float64(ingredient.Float32))
	res.Float64 = types.Float64Value(float64(ingredient.Float64))
	return &res, diags
}

func encodeCustomer(customer *structs.Customer) (*Customer, diag.Diagnostics) {
	if customer == nil {
		return nil, nil
	}

	var diags diag.Diagnostics
	res := Customer{}
	res.ID = types.Int64Value(customer.ID)
	res.Name = types.StringValue(customer.Name)
	return &res, diags
}
