// Code generated by apbr generate. DO NOT EDIT.
// versions:
// 	apbr generate v1.2

//go:build !codeanalysis

package resource_model

import (
	"github.com/apibrew/apibrew/pkg/abs"
	"github.com/apibrew/apibrew/pkg/formats/unstructured"
	"github.com/apibrew/apibrew/pkg/model"
	"github.com/apibrew/apibrew/pkg/types"
	"google.golang.org/protobuf/types/known/structpb"
)

import "github.com/google/uuid"
import "time"

type PermissionMapper struct {
}

func NewPermissionMapper() *PermissionMapper {
	return &PermissionMapper{}
}

var PermissionMapperInstance = NewPermissionMapper()

func (m *PermissionMapper) New() *Permission {
	return &Permission{}
}

func (m *PermissionMapper) ResourceIdentity() abs.ResourceIdentity {
	return abs.ResourceIdentity{
		Namespace: "system",
		Name:      "Permission",
	}
}

func (m *PermissionMapper) ToRecord(permission *Permission) *model.Record {
	var rec = &model.Record{}
	rec.Properties = m.ToProperties(permission)
	return rec
}

func (m *PermissionMapper) FromRecord(record *model.Record) *Permission {
	return m.FromProperties(record.Properties)
}

func (m *PermissionMapper) ToProperties(permission *Permission) map[string]*structpb.Value {
	var properties = make(map[string]*structpb.Value)

	var_Id := permission.Id

	if var_Id != nil {
		var var_Id_mapped *structpb.Value

		var var_Id_err error
		var_Id_mapped, var_Id_err = types.ByResourcePropertyType(model.ResourceProperty_UUID).Pack(*var_Id)
		if var_Id_err != nil {
			panic(var_Id_err)
		}
		properties["id"] = var_Id_mapped
	}

	var_Version := permission.Version

	var var_Version_mapped *structpb.Value

	var var_Version_err error
	var_Version_mapped, var_Version_err = types.ByResourcePropertyType(model.ResourceProperty_INT32).Pack(var_Version)
	if var_Version_err != nil {
		panic(var_Version_err)
	}
	properties["version"] = var_Version_mapped

	var_AuditData := permission.AuditData

	if var_AuditData != nil {
		var var_AuditData_mapped *structpb.Value

		var_AuditData_mapped = structpb.NewStructValue(&structpb.Struct{Fields: PermissionAuditDataMapperInstance.ToProperties(var_AuditData)})
		properties["auditData"] = var_AuditData_mapped
	}

	var_Namespace := permission.Namespace

	if var_Namespace != nil {
		var var_Namespace_mapped *structpb.Value

		var var_Namespace_err error
		var_Namespace_mapped, var_Namespace_err = types.ByResourcePropertyType(model.ResourceProperty_STRING).Pack(*var_Namespace)
		if var_Namespace_err != nil {
			panic(var_Namespace_err)
		}
		properties["namespace"] = var_Namespace_mapped
	}

	var_Resource := permission.Resource

	if var_Resource != nil {
		var var_Resource_mapped *structpb.Value

		var var_Resource_err error
		var_Resource_mapped, var_Resource_err = types.ByResourcePropertyType(model.ResourceProperty_STRING).Pack(*var_Resource)
		if var_Resource_err != nil {
			panic(var_Resource_err)
		}
		properties["resource"] = var_Resource_mapped
	}

	var_RecordSelector := permission.RecordSelector

	if var_RecordSelector != nil {
		var var_RecordSelector_mapped *structpb.Value

		var_RecordSelector_mapped = structpb.NewStructValue(&structpb.Struct{Fields: BooleanExpressionMapperInstance.ToProperties(var_RecordSelector)})
		properties["recordSelector"] = var_RecordSelector_mapped
	}

	var_Operation := permission.Operation

	var var_Operation_mapped *structpb.Value

	var var_Operation_err error
	var_Operation_mapped, var_Operation_err = types.ByResourcePropertyType(model.ResourceProperty_ENUM).Pack(string(var_Operation))
	if var_Operation_err != nil {
		panic(var_Operation_err)
	}
	properties["operation"] = var_Operation_mapped

	var_Before := permission.Before

	if var_Before != nil {
		var var_Before_mapped *structpb.Value

		var var_Before_err error
		var_Before_mapped, var_Before_err = types.ByResourcePropertyType(model.ResourceProperty_TIMESTAMP).Pack(*var_Before)
		if var_Before_err != nil {
			panic(var_Before_err)
		}
		properties["before"] = var_Before_mapped
	}

	var_After := permission.After

	if var_After != nil {
		var var_After_mapped *structpb.Value

		var var_After_err error
		var_After_mapped, var_After_err = types.ByResourcePropertyType(model.ResourceProperty_TIMESTAMP).Pack(*var_After)
		if var_After_err != nil {
			panic(var_After_err)
		}
		properties["after"] = var_After_mapped
	}

	var_User := permission.User

	if var_User != nil {
		var var_User_mapped *structpb.Value

		var_User_mapped = structpb.NewStructValue(&structpb.Struct{Fields: UserMapperInstance.ToProperties(var_User)})
		properties["user"] = var_User_mapped
	}

	var_Role := permission.Role

	if var_Role != nil {
		var var_Role_mapped *structpb.Value

		var_Role_mapped = structpb.NewStructValue(&structpb.Struct{Fields: RoleMapperInstance.ToProperties(var_Role)})
		properties["role"] = var_Role_mapped
	}

	var_Permit := permission.Permit

	var var_Permit_mapped *structpb.Value

	var var_Permit_err error
	var_Permit_mapped, var_Permit_err = types.ByResourcePropertyType(model.ResourceProperty_ENUM).Pack(string(var_Permit))
	if var_Permit_err != nil {
		panic(var_Permit_err)
	}
	properties["permit"] = var_Permit_mapped

	var_LocalFlags := permission.LocalFlags

	if var_LocalFlags != nil {
		var var_LocalFlags_mapped *structpb.Value

		var var_LocalFlags_err error
		var_LocalFlags_mapped, var_LocalFlags_err = types.ByResourcePropertyType(model.ResourceProperty_OBJECT).Pack(var_LocalFlags)
		if var_LocalFlags_err != nil {
			panic(var_LocalFlags_err)
		}
		properties["localFlags"] = var_LocalFlags_mapped
	}
	return properties
}

func (m *PermissionMapper) FromProperties(properties map[string]*structpb.Value) *Permission {
	var s = m.New()
	if properties["id"] != nil && properties["id"].AsInterface() != nil {

		var_Id := properties["id"]
		val, err := types.ByResourcePropertyType(model.ResourceProperty_UUID).UnPack(var_Id)

		if err != nil {
			panic(err)
		}

		var_Id_mapped := new(uuid.UUID)
		*var_Id_mapped = val.(uuid.UUID)

		s.Id = var_Id_mapped
	}
	if properties["version"] != nil && properties["version"].AsInterface() != nil {

		var_Version := properties["version"]
		val, err := types.ByResourcePropertyType(model.ResourceProperty_INT32).UnPack(var_Version)

		if err != nil {
			panic(err)
		}

		var_Version_mapped := val.(int32)

		s.Version = var_Version_mapped
	}
	if properties["auditData"] != nil && properties["auditData"].AsInterface() != nil {

		var_AuditData := properties["auditData"]
		var mappedValue = PermissionAuditDataMapperInstance.FromProperties(var_AuditData.GetStructValue().Fields)

		var_AuditData_mapped := mappedValue

		s.AuditData = var_AuditData_mapped
	}
	if properties["namespace"] != nil && properties["namespace"].AsInterface() != nil {

		var_Namespace := properties["namespace"]
		val, err := types.ByResourcePropertyType(model.ResourceProperty_STRING).UnPack(var_Namespace)

		if err != nil {
			panic(err)
		}

		var_Namespace_mapped := new(string)
		*var_Namespace_mapped = val.(string)

		s.Namespace = var_Namespace_mapped
	}
	if properties["resource"] != nil && properties["resource"].AsInterface() != nil {

		var_Resource := properties["resource"]
		val, err := types.ByResourcePropertyType(model.ResourceProperty_STRING).UnPack(var_Resource)

		if err != nil {
			panic(err)
		}

		var_Resource_mapped := new(string)
		*var_Resource_mapped = val.(string)

		s.Resource = var_Resource_mapped
	}
	if properties["recordSelector"] != nil && properties["recordSelector"].AsInterface() != nil {

		var_RecordSelector := properties["recordSelector"]
		var mappedValue = BooleanExpressionMapperInstance.FromProperties(var_RecordSelector.GetStructValue().Fields)

		var_RecordSelector_mapped := mappedValue

		s.RecordSelector = var_RecordSelector_mapped
	}
	if properties["operation"] != nil && properties["operation"].AsInterface() != nil {

		var_Operation := properties["operation"]
		var_Operation_mapped := (PermissionOperation)(var_Operation.GetStringValue())

		s.Operation = var_Operation_mapped
	}
	if properties["before"] != nil && properties["before"].AsInterface() != nil {

		var_Before := properties["before"]
		val, err := types.ByResourcePropertyType(model.ResourceProperty_TIMESTAMP).UnPack(var_Before)

		if err != nil {
			panic(err)
		}

		var_Before_mapped := new(time.Time)
		*var_Before_mapped = val.(time.Time)

		s.Before = var_Before_mapped
	}
	if properties["after"] != nil && properties["after"].AsInterface() != nil {

		var_After := properties["after"]
		val, err := types.ByResourcePropertyType(model.ResourceProperty_TIMESTAMP).UnPack(var_After)

		if err != nil {
			panic(err)
		}

		var_After_mapped := new(time.Time)
		*var_After_mapped = val.(time.Time)

		s.After = var_After_mapped
	}
	if properties["user"] != nil && properties["user"].AsInterface() != nil {

		var_User := properties["user"]
		var_User_mapped := UserMapperInstance.FromProperties(var_User.GetStructValue().Fields)

		s.User = var_User_mapped
	}
	if properties["role"] != nil && properties["role"].AsInterface() != nil {

		var_Role := properties["role"]
		var_Role_mapped := RoleMapperInstance.FromProperties(var_Role.GetStructValue().Fields)

		s.Role = var_Role_mapped
	}
	if properties["permit"] != nil && properties["permit"].AsInterface() != nil {

		var_Permit := properties["permit"]
		var_Permit_mapped := (PermissionPermit)(var_Permit.GetStringValue())

		s.Permit = var_Permit_mapped
	}
	if properties["localFlags"] != nil && properties["localFlags"].AsInterface() != nil {

		var_LocalFlags := properties["localFlags"]
		var_LocalFlags_mapped := new(interface{})
		*var_LocalFlags_mapped = unstructured.FromValue(var_LocalFlags)

		s.LocalFlags = var_LocalFlags_mapped
	}
	return s
}

func (m *PermissionMapper) ToUnstructured(permission *Permission) unstructured.Unstructured {
	var properties unstructured.Unstructured = make(unstructured.Unstructured)
	properties["type"] = "system/Permission"

	var_Id := permission.Id

	if var_Id != nil {
		var var_Id_mapped interface{}

		var_Id_mapped = var_Id.String()
		properties["id"] = var_Id_mapped
	}

	var_Version := permission.Version

	var var_Version_mapped interface{}

	var_Version_mapped = var_Version
	properties["version"] = var_Version_mapped

	var_AuditData := permission.AuditData

	if var_AuditData != nil {
		var var_AuditData_mapped interface{}

		var_AuditData_mapped = PermissionAuditDataMapperInstance.ToUnstructured(var_AuditData)
		properties["auditData"] = var_AuditData_mapped
	}

	var_Namespace := permission.Namespace

	if var_Namespace != nil {
		var var_Namespace_mapped interface{}

		var_Namespace_mapped = *var_Namespace
		properties["namespace"] = var_Namespace_mapped
	}

	var_Resource := permission.Resource

	if var_Resource != nil {
		var var_Resource_mapped interface{}

		var_Resource_mapped = *var_Resource
		properties["resource"] = var_Resource_mapped
	}

	var_RecordSelector := permission.RecordSelector

	if var_RecordSelector != nil {
		var var_RecordSelector_mapped interface{}

		var_RecordSelector_mapped = BooleanExpressionMapperInstance.ToUnstructured(var_RecordSelector)
		properties["recordSelector"] = var_RecordSelector_mapped
	}

	var_Operation := permission.Operation

	var var_Operation_mapped interface{}

	var_Operation_mapped = string(var_Operation)
	properties["operation"] = var_Operation_mapped

	var_Before := permission.Before

	if var_Before != nil {
		var var_Before_mapped interface{}

		var_Before_mapped = *var_Before
		properties["before"] = var_Before_mapped
	}

	var_After := permission.After

	if var_After != nil {
		var var_After_mapped interface{}

		var_After_mapped = *var_After
		properties["after"] = var_After_mapped
	}

	var_User := permission.User

	if var_User != nil {
		var var_User_mapped interface{}

		var_User_mapped = UserMapperInstance.ToUnstructured(var_User)
		properties["user"] = var_User_mapped
	}

	var_Role := permission.Role

	if var_Role != nil {
		var var_Role_mapped interface{}

		var_Role_mapped = RoleMapperInstance.ToUnstructured(var_Role)
		properties["role"] = var_Role_mapped
	}

	var_Permit := permission.Permit

	var var_Permit_mapped interface{}

	var_Permit_mapped = string(var_Permit)
	properties["permit"] = var_Permit_mapped

	var_LocalFlags := permission.LocalFlags

	if var_LocalFlags != nil {
		var var_LocalFlags_mapped interface{}

		var_LocalFlags_mapped = var_LocalFlags
		properties["localFlags"] = var_LocalFlags_mapped
	}

	return properties
}

type PermissionAuditDataMapper struct {
}

func NewPermissionAuditDataMapper() *PermissionAuditDataMapper {
	return &PermissionAuditDataMapper{}
}

var PermissionAuditDataMapperInstance = NewPermissionAuditDataMapper()

func (m *PermissionAuditDataMapper) New() *PermissionAuditData {
	return &PermissionAuditData{}
}

func (m *PermissionAuditDataMapper) ResourceIdentity() abs.ResourceIdentity {
	return abs.ResourceIdentity{
		Namespace: "system",
		Name:      "Permission",
	}
}

func (m *PermissionAuditDataMapper) ToProperties(permissionAuditData *PermissionAuditData) map[string]*structpb.Value {
	var properties = make(map[string]*structpb.Value)

	var_CreatedBy := permissionAuditData.CreatedBy

	if var_CreatedBy != nil {
		var var_CreatedBy_mapped *structpb.Value

		var var_CreatedBy_err error
		var_CreatedBy_mapped, var_CreatedBy_err = types.ByResourcePropertyType(model.ResourceProperty_STRING).Pack(*var_CreatedBy)
		if var_CreatedBy_err != nil {
			panic(var_CreatedBy_err)
		}
		properties["createdBy"] = var_CreatedBy_mapped
	}

	var_UpdatedBy := permissionAuditData.UpdatedBy

	if var_UpdatedBy != nil {
		var var_UpdatedBy_mapped *structpb.Value

		var var_UpdatedBy_err error
		var_UpdatedBy_mapped, var_UpdatedBy_err = types.ByResourcePropertyType(model.ResourceProperty_STRING).Pack(*var_UpdatedBy)
		if var_UpdatedBy_err != nil {
			panic(var_UpdatedBy_err)
		}
		properties["updatedBy"] = var_UpdatedBy_mapped
	}

	var_CreatedOn := permissionAuditData.CreatedOn

	if var_CreatedOn != nil {
		var var_CreatedOn_mapped *structpb.Value

		var var_CreatedOn_err error
		var_CreatedOn_mapped, var_CreatedOn_err = types.ByResourcePropertyType(model.ResourceProperty_TIMESTAMP).Pack(*var_CreatedOn)
		if var_CreatedOn_err != nil {
			panic(var_CreatedOn_err)
		}
		properties["createdOn"] = var_CreatedOn_mapped
	}

	var_UpdatedOn := permissionAuditData.UpdatedOn

	if var_UpdatedOn != nil {
		var var_UpdatedOn_mapped *structpb.Value

		var var_UpdatedOn_err error
		var_UpdatedOn_mapped, var_UpdatedOn_err = types.ByResourcePropertyType(model.ResourceProperty_TIMESTAMP).Pack(*var_UpdatedOn)
		if var_UpdatedOn_err != nil {
			panic(var_UpdatedOn_err)
		}
		properties["updatedOn"] = var_UpdatedOn_mapped
	}
	return properties
}

func (m *PermissionAuditDataMapper) FromProperties(properties map[string]*structpb.Value) *PermissionAuditData {
	var s = m.New()
	if properties["createdBy"] != nil && properties["createdBy"].AsInterface() != nil {

		var_CreatedBy := properties["createdBy"]
		val, err := types.ByResourcePropertyType(model.ResourceProperty_STRING).UnPack(var_CreatedBy)

		if err != nil {
			panic(err)
		}

		var_CreatedBy_mapped := new(string)
		*var_CreatedBy_mapped = val.(string)

		s.CreatedBy = var_CreatedBy_mapped
	}
	if properties["updatedBy"] != nil && properties["updatedBy"].AsInterface() != nil {

		var_UpdatedBy := properties["updatedBy"]
		val, err := types.ByResourcePropertyType(model.ResourceProperty_STRING).UnPack(var_UpdatedBy)

		if err != nil {
			panic(err)
		}

		var_UpdatedBy_mapped := new(string)
		*var_UpdatedBy_mapped = val.(string)

		s.UpdatedBy = var_UpdatedBy_mapped
	}
	if properties["createdOn"] != nil && properties["createdOn"].AsInterface() != nil {

		var_CreatedOn := properties["createdOn"]
		val, err := types.ByResourcePropertyType(model.ResourceProperty_TIMESTAMP).UnPack(var_CreatedOn)

		if err != nil {
			panic(err)
		}

		var_CreatedOn_mapped := new(time.Time)
		*var_CreatedOn_mapped = val.(time.Time)

		s.CreatedOn = var_CreatedOn_mapped
	}
	if properties["updatedOn"] != nil && properties["updatedOn"].AsInterface() != nil {

		var_UpdatedOn := properties["updatedOn"]
		val, err := types.ByResourcePropertyType(model.ResourceProperty_TIMESTAMP).UnPack(var_UpdatedOn)

		if err != nil {
			panic(err)
		}

		var_UpdatedOn_mapped := new(time.Time)
		*var_UpdatedOn_mapped = val.(time.Time)

		s.UpdatedOn = var_UpdatedOn_mapped
	}
	return s
}

func (m *PermissionAuditDataMapper) ToUnstructured(permissionAuditData *PermissionAuditData) unstructured.Unstructured {
	var properties unstructured.Unstructured = make(unstructured.Unstructured)

	var_CreatedBy := permissionAuditData.CreatedBy

	if var_CreatedBy != nil {
		var var_CreatedBy_mapped interface{}

		var_CreatedBy_mapped = *var_CreatedBy
		properties["createdBy"] = var_CreatedBy_mapped
	}

	var_UpdatedBy := permissionAuditData.UpdatedBy

	if var_UpdatedBy != nil {
		var var_UpdatedBy_mapped interface{}

		var_UpdatedBy_mapped = *var_UpdatedBy
		properties["updatedBy"] = var_UpdatedBy_mapped
	}

	var_CreatedOn := permissionAuditData.CreatedOn

	if var_CreatedOn != nil {
		var var_CreatedOn_mapped interface{}

		var_CreatedOn_mapped = *var_CreatedOn
		properties["createdOn"] = var_CreatedOn_mapped
	}

	var_UpdatedOn := permissionAuditData.UpdatedOn

	if var_UpdatedOn != nil {
		var var_UpdatedOn_mapped interface{}

		var_UpdatedOn_mapped = *var_UpdatedOn
		properties["updatedOn"] = var_UpdatedOn_mapped
	}

	return properties
}
