package app

import (
	"database/sql"
	"time"

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

// UnMarshalTemplateSwagger decodes swagger to a first class object
func UnMarshalTemplateSwagger(s *devops_models.Template) *Template {
	if s.ID == "" {
		s.ID = uuid.New().String()
	}
	createdDate, e0 := time.Parse(dateTimeFormat, s.CreatedDate)
	lastModfiedDate, e1 := time.Parse(dateTimeFormat, s.LastModifiedDate)
	return &Template{
		ID:               s.ID,
		CompanyID:        s.CompanyID,
		CreatedByID:      s.CreatedByID,
		Description:      s.Description,
		HTML:             string(s.HTML),
		IsActive:         s.IsActive,
		IsMaster:         s.IsMaster,
		LastModifiedByID: s.LastModifiedByID,
		Name:             s.Name,
		ObjectType:       s.ObjectType,
		RecordTypeName:   s.RecordTypeName,
		TenantID:         s.TenantID,
		Type:             s.Type,
		URL:              s.URL,
		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 *Template) MarshalToSwagger() *devops_models.Template {
	return &devops_models.Template{
		ID:               obj.ID,
		CompanyID:        obj.CompanyID,
		CreatedByID:      obj.CreatedByID,
		CreatedDate:      obj.CreatedDate.Time.Format(dateTimeFormat),
		Description:      obj.Description,
		HTML:             []byte(obj.HTML),
		IsActive:         obj.IsActive,
		IsMaster:         obj.IsMaster,
		LastModifiedByID: obj.LastModifiedByID,
		LastModifiedDate: obj.LastModifiedDate.Time.Format(dateTimeFormat),
		Name:             obj.Name,
		ObjectType:       obj.ObjectType,
		RecordTypeName:   obj.RecordTypeName,
		Type:             obj.Type,
		TenantID:         obj.TenantID,
		URL:              obj.URL,
	}
}
