package app

import (
	"database/sql"
	"time"

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

func UnMarshalTaxInstance(s *geo_models.TaxInstance) *TaxInstance {
	if s.ID == "" {
		s.ID = uuid.New().String()
	}
	createdDate, e0 := time.Parse(dateTimeFormat, s.CreatedDate)
	lastModfiedDate, e1 := time.Parse(dateTimeFormat, s.LastModifiedDate)
	return &TaxInstance{
		ID:               s.ID,
		CountryID:        s.CountryID,
		CountyID:         s.CountyID,
		CreatedByID:      s.CreatedByID,
		LastModifiedByID: s.LastModifiedByID,
		PlaceID:          s.PlaceID,
		StateID:          s.StateID,
		TaxTypeID:        s.TaxTypeID,
		Type:             s.Type,
		CreatedDate: sql.NullTime{
			Time:  createdDate,
			Valid: e0 == nil,
		},
		LastModifiedDate: sql.NullTime{
			Time:  lastModfiedDate,
			Valid: e1 == nil,
		},
	}
}

// MarshalToSwagger encodes first class object
func (obj *TaxInstance) MarshalToSwagger() *geo_models.TaxInstance {
	return &geo_models.TaxInstance{
		ID:               obj.ID,
		CountryID:        obj.CountryID,
		CountyID:         obj.CountyID,
		CreatedByID:      obj.CreatedByID,
		CreatedDate:      obj.CreatedDate.Time.Format(dateTimeFormat),
		LastModifiedByID: obj.LastModifiedByID,
		LastModifiedDate: obj.LastModifiedDate.Time.Format(dateTimeFormat),
		PlaceID:          obj.PlaceID,
		StateID:          obj.StateID,
		TaxTypeID:        obj.TaxTypeID,
		Type:             obj.Type,
	}
}
