package app

import (
	"database/sql"
	"time"

	"code.tnxs.net/taxnexus/lib/api/ops/ops_models"
	"github.com/google/uuid"
)

// UnMarshalProduct decodes swagger to first class object
func UnMarshalProduct(s *ops_models.Product) *Product {
	if s.ID == "" {
		s.ID = uuid.New().String()
	}
	createdDate, e0 := time.Parse(dateTimeFormat, s.CreatedDate)
	return &Product{
		ID:                      s.ID,
		AccountID:               s.AccountID,
		AgencyType:              s.AgencyType,
		AssetTracking:           s.AssetTracking,
		CreatedByID:             s.CreatedByID,
		Description:             s.Description,
		DescriptionSKU:          s.DescriptionSKU,
		DisplayURL:              s.DisplayURL,
		Family:                  s.Family,
		Image500:                s.Image500,
		ImageFull:               s.ImageFull,
		InventoryTracking:       s.InventoryTracking,
		IsActive:                s.IsActive,
		IsGeneric:               s.IsGeneric,
		LastModifiedByID:        s.LastModifiedByID,
		Manufacturer:            s.Manufacturer,
		ManufacturerProductCode: s.ManufacturerProductCode,
		MRCInterval:             s.MRCInterval,
		MSRP:                    s.MSRP,
		Name:                    s.Name,
		ProductCode:             s.ProductCode,
		Prorateable:             s.Prorateable,
		Publish:                 s.Publish,
		PublishUPC:              s.PublishUPC,
		QuantityUnitOfMeasure:   s.QuantityUnitOfMeasure,
		Refundable:              s.Refundable,
		ShippingWeight:          s.ShippingWeight,
		SKU:                     s.SKU,
		Specifications:          s.Specifications,
		TaxnexusCode:            s.TaxnexusCode,
		TaxnexusCodeID:          s.TaxnexusCodeID,
		TenantID:                s.TenantID,
		Units:                   s.Units,
		VendorID:                s.VendorID,
		VendorName:              s.VendorName,
		VendorPartNumber:        s.VendorPartNumber,
		VendorPrice:             s.VendorPrice,
		CreatedDate: sql.NullTime{
			Time:  createdDate,
			Valid: e0 == nil,
		},
	}
}

// MarshalToSwagger encodes first class object
func (obj *Product) MarshalToSwagger() *ops_models.Product {
	return &ops_models.Product{
		ID:                      obj.ID,
		AccountID:               obj.AccountID,
		AgencyType:              obj.AgencyType,
		AssetTracking:           obj.AssetTracking,
		CreatedByID:             obj.CreatedByID,
		CreatedDate:             obj.CreatedDate.Time.Format(dateTimeFormat),
		Description:             obj.Description,
		DescriptionSKU:          obj.DescriptionSKU,
		DisplayURL:              obj.DisplayURL,
		Family:                  obj.Family,
		Image500:                obj.Image500,
		ImageFull:               obj.ImageFull,
		InventoryTracking:       obj.InventoryTracking,
		IsActive:                obj.IsActive,
		IsGeneric:               obj.IsGeneric,
		LastModifiedByID:        obj.LastModifiedByID,
		Manufacturer:            obj.Manufacturer,
		ManufacturerProductCode: obj.ManufacturerProductCode,
		MRCInterval:             obj.MRCInterval,
		MSRP:                    obj.MSRP,
		Name:                    obj.Name,
		ProductCode:             obj.ProductCode,
		Prorateable:             obj.Prorateable,
		Publish:                 obj.Publish,
		PublishUPC:              obj.PublishUPC,
		QuantityUnitOfMeasure:   obj.QuantityUnitOfMeasure,
		Refundable:              obj.Refundable,
		ShippingWeight:          obj.ShippingWeight,
		SKU:                     obj.SKU,
		Specifications:          obj.Specifications,
		TaxnexusCode:            obj.TaxnexusCode,
		TaxnexusCodeID:          obj.TaxnexusCodeID,
		TenantID:                obj.TenantID,
		Units:                   obj.Units,
		VendorID:                obj.VendorID,
		VendorName:              obj.VendorName,
		VendorPartNumber:        obj.VendorPartNumber,
		VendorPrice:             obj.VendorPrice,
		LastModifiedDate:        obj.LastModifiedDate.Time.Format(dateTimeFormat),
	}
}
