package app

import (
	"database/sql"
	"time"

	"code.tnxs.net/taxnexus/lib/api/workflow/workflow_models"
	"github.com/google/uuid"
)

// UnMarshalLog decodes swagger to first class object
func UnMarshalLog(s *workflow_models.AppLog) *Log {
	if s.ID == "" {
		s.ID = uuid.New().String()
	}
	createdDate, e0 := time.Parse(dateTimeFormat, s.CreatedDate)
	sourceTimestamp, e1 := time.Parse(dateTimeFormat, s.SourceTimestamp)
	return &Log{
		AccountID:   s.AccountID,
		CompanyID:   s.CompanyID,
		CreatedByID: s.CreatedByID,
		ID:          s.ID,
		Message:     s.Message,
		ObjectID:    s.ObjectID,
		ObjectType:  s.ObjectType,
		Severity:    s.Severity,
		Source:      s.Source,
		CreatedDate: sql.NullTime{
			Time:  createdDate,
			Valid: e0 == nil,
		},
		SourceTimestamp: sql.NullTime{
			Time:  sourceTimestamp,
			Valid: e1 == nil,
		},
	}
}

// MarshalToSwagger encodes first class object
func (obj *Log) MarshalToSwagger() *workflow_models.AppLog {
	return &workflow_models.AppLog{
		ID:              obj.ID,
		AccountID:       obj.AccountID,
		CompanyID:       obj.CompanyID,
		CreatedByID:     obj.CreatedByID,
		CreatedDate:     obj.CreatedDate.Time.Format(dateTimeFormat),
		Message:         obj.Message,
		ObjectType:      obj.ObjectType,
		ObjectID:        obj.ObjectID,
		Severity:        obj.Severity,
		Source:          obj.Source,
		SourceTimestamp: obj.SourceTimestamp.Time.Format(dateTimeFormat),
	}
}
