package app

import (
	"database/sql"
	"time"

	"code.tnxs.net/taxnexus/lib/api/devops/devops_models"
	"github.com/google/uuid"
)

// UnMarshalRole decodes swagger to first class object
func UnMarshalRole(s *devops_models.Role) *Role {
	if s.ID == "" {
		s.ID = uuid.New().String()
	}
	createdDate, e0 := time.Parse(dateTimeFormat, s.CreatedDate)
	lastModfiedDate, e1 := time.Parse(dateTimeFormat, s.LastModifiedDate)
	return &Role{
		ID:               s.ID,
		Auth0RoleID:      s.Auth0RoleID,
		CreatedByID:      s.CreatedByID,
		Description:      s.Description,
		LastModifiedByID: s.LastModifiedByID,
		RoleName:         s.RoleName,
		TenantID:         s.TenantID,
		CreatedDate: sql.NullTime{
			Time:  createdDate,
			Valid: e0 == nil,
		},
		LastModifiedDate: sql.NullTime{
			Time:  lastModfiedDate,
			Valid: e1 == nil,
		},
	}
}

// MarshalToSwagger encodes a first class object to swagger
func (obj *Role) MarshalToSwagger() *devops_models.Role {
	return &devops_models.Role{
		ID:               obj.ID,
		Auth0RoleID:      obj.Auth0RoleID,
		CreatedByID:      obj.CreatedByID,
		CreatedDate:      obj.CreatedDate.Time.Format(dateTimeFormat),
		Description:      obj.Description,
		LastModifiedByID: obj.LastModifiedByID,
		LastModifiedDate: obj.LastModifiedDate.Time.Format(dateTimeFormat),
		RoleName:         obj.RoleName,
		TenantID:         obj.TenantID,
	}
}
