package app

import (
	"database/sql"
	"time"

	"code.tnxs.net/taxnexus/lib/api/regs/regs_models"
)

// UnMarshalTransaction encodes first class object
func UnMarshalTransaction(s *regs_models.Transaction) *Transaction {
	createdDate, e0 := time.Parse(dateTimeFormat, s.CreatedDate)
	lastModfiedDate, e1 := time.Parse(dateTimeFormat, s.LastModifiedDate)
	return &Transaction{
		ID:               s.ID,
		AccountID:        s.AccountID,
		CreatedByID:      s.CreatedByID,
		LastModifiedByID: s.LastModifiedByID,
		TaxTransactionID: s.TaxTransactionID,
		TaxTypeID:        s.TaxTypeID,
		TenantID:         s.TenantID,
		Valid:            s.Valid,
		CreatedDate: sql.NullTime{
			Time:  createdDate,
			Valid: e0 == nil,
		},
		LastModifiedDate: sql.NullTime{
			Time:  lastModfiedDate,
			Valid: e1 == nil,
		},
	}
}

// MarshalToSwagger encodes first class object
func (obj *Transaction) MarshalToSwagger() *regs_models.Transaction {
	return &regs_models.Transaction{
		ID:               obj.ID,
		AccountID:        obj.AccountID,
		CreatedByID:      obj.CreatedByID,
		CreatedDate:      obj.CreatedDate.Time.Format(dateTimeFormat),
		LastModifiedByID: obj.LastModifiedByID,
		LastModifiedDate: obj.LastModifiedDate.Time.Format(dateTimeFormat),
		TaxTransactionID: obj.TaxTransactionID,
		TaxTypeID:        obj.TaxTypeID,
		TenantID:         obj.TenantID,
		Valid:            obj.Valid,
	}
}
