package app

import (
	"database/sql"
	"time"

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

// UnMarshalOrderItem decodes swagger to first class object
func UnMarshalOrderItem(s *ops_models.OrderItem) *OrderItem {
	if s.ID == "" {
		s.ID = uuid.New().String()
	}
	createdDate, e0 := time.Parse(dateTimeFormat, s.CreatedDate)
	lastModifiedDate, e1 := time.Parse(dateTimeFormat, s.LastModifiedDate)
	dateDelivered, e2 := time.Parse(dateTimeFormat, s.DateDelivered)
	dateOrdered, e3 := time.Parse(dateTimeFormat, s.DateDelivered)
	datePromised, e4 := time.Parse(dateTimeFormat, s.DatePromised)
	serviceDate, e5 := time.Parse(dateTimeFormat, s.ServiceDate)
	return &OrderItem{
		ID:                  s.ID,
		Activated:           s.Activated,
		ActiveatedByID:      s.ActiveatedByID,
		AvailableQuantity:   s.AvailableQuantity,
		COGS:                s.COGS,
		CreatedByID:         s.CreatedByID,
		CreateReservation:   s.CreateReservation,
		Description:         s.Description,
		Discount:            s.Discount,
		Family:              s.Family,
		InvoiceItemID:       s.InvoiceItemID,
		LastModifiedByID:    s.LastModifiedByID,
		ListPrice:           s.ListPrice,
		LocationID:          s.LocationID,
		MRCInterval:         s.MRCInterval,
		OrderID:             s.OrderID,
		OriginalOrderItemID: s.OriginalOrderItemID,
		ParentFK:            s.ParentFK,
		Posted:              s.Posted,
		ProductCode:         s.ProductCode,
		ProductID:           s.ProductID,
		ProductName:         s.ProductName,
		Quantity:            s.Quantity,
		QuantityOnHand:      s.QuantityOnHand,
		QuoteItemID:         s.QuoteItemID,
		Ref:                 s.Ref,
		ShippingHandling:    s.ShippingHandling,
		Status:              s.Status,
		SubscriptionID:      s.SubscriptionID,
		Subtotal:            s.Subtotal,
		TaxnexusCodeDisplay: s.TaxnexusCodeDisplay,
		TaxnexusCodeID:      s.TaxnexusCodeID,
		TenantID:            s.TenantID,
		TotalPrice:          s.TotalPrice,
		UnitPrice:           s.UnitPrice,
		Units:               s.Units,
		CreatedDate: sql.NullTime{
			Time:  createdDate,
			Valid: e0 == nil,
		},
		LastModifiedDate: sql.NullTime{
			Time:  lastModifiedDate,
			Valid: e1 == nil,
		},
		DateDelivered: sql.NullTime{
			Time:  dateDelivered,
			Valid: e2 == nil,
		},
		DateOrdered: sql.NullTime{
			Time:  dateOrdered,
			Valid: e3 == nil,
		},
		DatePromised: sql.NullTime{
			Time:  datePromised,
			Valid: e4 == nil,
		},
		ServiceDate: sql.NullTime{
			Time:  serviceDate,
			Valid: e5 == nil,
		},
	}
}

// MarshalToSwagger encodes first class object
func (obj *OrderItem) MarshalToSwagger() *ops_models.OrderItem {
	return &ops_models.OrderItem{
		ID:                  obj.ID,
		Activated:           obj.Activated,
		ActiveatedByID:      obj.ActiveatedByID,
		AvailableQuantity:   obj.AvailableQuantity,
		COGS:                obj.COGS,
		CreatedByID:         obj.CreatedByID,
		CreatedDate:         obj.CreatedDate.Time.Format(dateTimeFormat),
		CreateReservation:   obj.CreateReservation,
		DateDelivered:       obj.DateDelivered.Time.Format(dateTimeFormat),
		DateOrdered:         obj.DateOrdered.Time.Format(dateTimeFormat),
		DatePromised:        obj.DatePromised.Time.Format(dateTimeFormat),
		Description:         obj.Description,
		Discount:            obj.Discount,
		Family:              obj.Family,
		InvoiceItemID:       obj.InvoiceItemID,
		LastModifiedByID:    obj.LastModifiedByID,
		LastModifiedDate:    obj.LastModifiedDate.Time.Format(dateTimeFormat),
		ListPrice:           obj.ListPrice,
		LocationID:          obj.LocationID,
		MRCInterval:         obj.MRCInterval,
		OrderID:             obj.OrderID,
		OriginalOrderItemID: obj.OriginalOrderItemID,
		ParentFK:            obj.ParentFK,
		Posted:              obj.Posted,
		ProductCode:         obj.ProductCode,
		ProductID:           obj.ProductID,
		ProductName:         obj.ProductName,
		Quantity:            obj.Quantity,
		QuantityOnHand:      obj.QuantityOnHand,
		QuoteItemID:         obj.QuoteItemID,
		Ref:                 obj.Ref,
		ServiceDate:         obj.ServiceDate.Time.Format(dateTimeFormat),
		ShippingHandling:    obj.ShippingHandling,
		Status:              obj.Status,
		SubscriptionID:      obj.SubscriptionID,
		Subtotal:            obj.Subtotal,
		TaxnexusCodeDisplay: obj.TaxnexusCodeDisplay,
		TaxnexusCodeID:      obj.TaxnexusCodeID,
		TenantID:            obj.TenantID,
		TotalPrice:          obj.TotalPrice,
		UnitPrice:           obj.UnitPrice,
		Units:               obj.Units,
	}
}
