package app

import (
	"database/sql"
	"time"

	"code.tnxs.net/taxnexus/lib/api/geo/geo_models"
	"github.com/google/uuid"
)

// UnMarshalTaxnexusCode decodes swagger to first class object
func UnMarshalTaxnexusCode(s *geo_models.TaxnexusCode) *TaxnexusCode {
	if s.ID == "" {
		s.ID = uuid.New().String()
	}
	createdDate, e0 := time.Parse(dateTimeFormat, s.CreatedDate)
	lastModfiedDate, e1 := time.Parse(dateTimeFormat, s.LastModifiedDate)
	return &TaxnexusCode{
		ID:                    s.ID,
		Active:                s.Active,
		Code:                  s.Code,
		CreatedByID:           s.CreatedByID,
		Description:           s.Description,
		DomainID:              s.DomainID,
		DomainName:            s.DomainName,
		LastModifiedByID:      s.LastModifiedByID,
		Level:                 s.Level,
		Part1:                 s.Part1,
		Part2:                 s.Part2,
		Part3:                 s.Part3,
		Part4:                 s.Part4,
		Part5:                 s.Part5,
		PurchasingRulesetCode: s.PurchasingRulesetCode,
		PurchasingRulesetID:   s.PurchasingRulesetID,
		Ref:                   s.Ref,
		RevenueRulesetCode:    s.RevenueRulesetCode,
		RevenueRulesetID:      s.RevenueRulesetID,
		OwnerID:               s.OwnerID,
		CreatedDate: sql.NullTime{
			Time:  createdDate,
			Valid: e0 == nil,
		},
		LastModifiedDate: sql.NullTime{
			Time:  lastModfiedDate,
			Valid: e1 == nil,
		},
	}
}

// MarshalToSwagger encodes first class object
func (obj *TaxnexusCode) MarshalToSwagger() *geo_models.TaxnexusCode {
	swag := geo_models.TaxnexusCode{
		ID:                    obj.ID,
		Active:                obj.Active,
		Code:                  obj.Code,
		CreatedByID:           obj.CreatedByID,
		CreatedDate:           obj.CreatedDate.Time.Format(dateTimeFormat),
		Description:           obj.Description,
		DomainID:              obj.DomainID,
		DomainName:            obj.DomainName,
		LastModifiedByID:      obj.LastModifiedByID,
		LastModifiedDate:      obj.LastModifiedDate.Time.Format(dateTimeFormat),
		Level:                 obj.Level,
		OwnerID:               obj.OwnerID,
		Part1:                 obj.Part1,
		Part2:                 obj.Part2,
		Part3:                 obj.Part3,
		Part4:                 obj.Part4,
		Part5:                 obj.Part5,
		PurchasingRulesetCode: obj.PurchasingRulesetCode,
		PurchasingRulesetID:   obj.PurchasingRulesetID,
		Ref:                   obj.Ref,
		RevenueRulesetCode:    obj.RevenueRulesetCode,
		RevenueRulesetID:      obj.RevenueRulesetID,
	}
	return &swag
}
