package app

import (
	"database/sql"
	"time"

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

// UnMarshalCountry decodes swagger to first class object
func UnMarshalCountry(s *geo_models.Country) *Country {
	if s.ID == "" {
		s.ID = uuid.New().String()
	}
	createdDate, e0 := time.Parse(dateTimeFormat, s.CreatedDate)
	lastModfiedDate, e1 := time.Parse(dateTimeFormat, s.LastModifiedDate)
	theInstances := []*TaxInstance{}
	for _, itm := range s.TaxInstances {
		theInstances = append(theInstances, UnMarshalTaxInstance(itm))
	}
	return &Country{
		ID:                  s.ID,
		AccountID:           s.AccountID,
		Amount:              s.Amount,
		Code:                s.Code,
		ContactID:           s.ContactID,
		CreatedByID:         s.CreatedByID,
		EnrollmentStatus:    s.EnrollmentStatus,
		Interest:            s.Interest,
		LastModifiedByID:    s.LastModifiedByID,
		Latitude:            s.Latitude,
		Longitude:           s.Longitude,
		Name:                s.Name,
		OwnerID:             s.OwnerID,
		Penalty:             s.Penalty,
		Ref:                 s.Ref,
		ReportedAdjustments: s.ReportedAdjustments,
		ReportedDeductions:  s.ReportedDeductions,
		ReportedNetRevenue:  s.ReportedNetRevenue,
		ReportedRate:        s.ReportedRate,
		ReportedRevenue:     s.ReportedRevenue,
		RevenueBase:         s.RevenueBase,
		RevenueNet:          s.RevenueNet,
		RevenueNotTaxable:   s.RevenueNotTaxable,
		Status:              s.Status,
		Subtotal:            s.Subtotal,
		TaxInstances:        theInstances,
		TemplateID:          s.TemplateID,
		TotalAmount:         s.TotalAmount,
		UnitBase:            s.UnitBase,
		CreatedDate: sql.NullTime{
			Time:  createdDate,
			Valid: e0 == nil,
		},
		LastModifiedDate: sql.NullTime{
			Time:  lastModfiedDate,
			Valid: e1 == nil,
		},
	}
}

// MarshalToSwagger encodes first class object
func (obj *Country) MarshalToSwagger() *geo_models.Country {
	taxInstances := []*geo_models.TaxInstance{}
	for _, itm := range obj.TaxInstances {
		taxInstances = append(taxInstances, itm.MarshalToSwagger())
	}
	return &geo_models.Country{
		ID:                  obj.ID,
		AccountID:           obj.AccountID,
		Amount:              obj.Amount,
		Code:                obj.Code,
		ContactID:           obj.ContactID,
		CreatedByID:         obj.CreatedByID,
		CreatedDate:         obj.CreatedDate.Time.Format(dateTimeFormat),
		EnrollmentStatus:    obj.EnrollmentStatus,
		Interest:            obj.Interest,
		LastModifiedByID:    obj.LastModifiedByID,
		LastModifiedDate:    obj.LastModifiedDate.Time.Format(dateTimeFormat),
		Latitude:            obj.Latitude,
		Longitude:           obj.Longitude,
		Name:                obj.Name,
		OwnerID:             obj.OwnerID,
		Penalty:             obj.Penalty,
		Ref:                 obj.Ref,
		ReportedAdjustments: obj.ReportedAdjustments,
		ReportedDeductions:  obj.ReportedDeductions,
		ReportedNetRevenue:  obj.ReportedNetRevenue,
		ReportedRate:        obj.ReportedRate,
		ReportedRevenue:     obj.ReportedRevenue,
		RevenueBase:         obj.RevenueBase,
		RevenueNet:          obj.RevenueNet,
		RevenueNotTaxable:   obj.RevenueNotTaxable,
		Status:              obj.Status,
		Subtotal:            obj.Subtotal,
		TaxInstances:        taxInstances,
		TemplateID:          obj.TemplateID,
		TotalAmount:         obj.TotalAmount,
		UnitBase:            obj.UnitBase,
	}
}
