package app

import (
	"database/sql"
	"time"

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

// UnMarshalPurchaseOrderItem decodes swagger to first class object
func UnMarshalPurchaseOrderItem(s *ops_models.PurchaseOrderItem) *PurchaseOrderItem {
	if s.ID == "" {
		s.ID = uuid.New().String()
	}
	createdDate, e0 := time.Parse(dateTimeFormat, s.CreatedDate)
	lastModifiedDate, e1 := time.Parse(dateTimeFormat, s.LastModifiedDate)
	dueDate, e8 := time.Parse(dateTimeFormat, s.DueDate)
	return &PurchaseOrderItem{
		ID:                  s.ID,
		CreatedByID:         s.CreatedByID,
		Description:         s.Description,
		Family:              s.Family,
		LastModifiedByID:    s.LastModifiedByID,
		LocationID:          s.LocationID,
		MRCInterval:         s.MRCInterval,
		OrderItemID:         s.OrderItemID,
		ParentFK:            s.ParentFK,
		PurchaseOrderID:     s.PurchaseOrderID,
		ProductCode:         s.ProductCode,
		ProductID:           s.ProductID,
		ProductName:         s.ProductName,
		Quantity:            s.Quantity,
		QuoteItemID:         s.QuoteItemID,
		ReceivedQuantity:    s.ReceivedQuantity,
		Ref:                 s.Ref,
		RejectedQuantity:    s.RejectedQuantity,
		ShippmentItemID:     s.ShippmentItemID,
		ShippingHandling:    s.ShippingHandling,
		Status:              s.Status,
		StockedQuantity:     s.StockedQuantity,
		Subtotal:            s.Subtotal,
		TaxnexusCodeDisplay: s.TaxnexusCodeDisplay,
		TaxnexusCodeID:      s.TaxnexusCodeID,
		UnitPrice:           s.UnitPrice,
		Units:               s.Units,
		CreatedDate: sql.NullTime{
			Time:  createdDate,
			Valid: e0 == nil,
		},
		LastModifiedDate: sql.NullTime{
			Time:  lastModifiedDate,
			Valid: e1 == nil,
		},
		DueDate: sql.NullTime{
			Time:  dueDate,
			Valid: e8 == nil,
		},
	}
}

// MarshalToSwagger encodes first class object
func (obj *PurchaseOrderItem) MarshalToSwagger() *ops_models.PurchaseOrderItem {
	return &ops_models.PurchaseOrderItem{
		ID:                  obj.ID,
		CreatedByID:         obj.CreatedByID,
		CreatedDate:         obj.CreatedDate.Time.Format(dateTimeFormat),
		Description:         obj.Description,
		DueDate:             obj.DueDate.Time.Format(dateTimeFormat),
		Family:              obj.Family,
		LastModifiedByID:    obj.LastModifiedByID,
		LastModifiedDate:    obj.LastModifiedDate.Time.Format(dateTimeFormat),
		LocationID:          obj.LocationID,
		MRCInterval:         obj.MRCInterval,
		OrderItemID:         obj.OrderItemID,
		ParentFK:            obj.ParentFK,
		ProductCode:         obj.ProductCode,
		ProductID:           obj.ProductID,
		ProductName:         obj.ProductName,
		PurchaseOrderID:     obj.PurchaseOrderID,
		Quantity:            obj.Quantity,
		QuoteItemID:         obj.QuoteItemID,
		ReceivedQuantity:    obj.ReceivedQuantity,
		Ref:                 obj.Ref,
		RejectedQuantity:    obj.RejectedQuantity,
		ShippingHandling:    obj.ShippingHandling,
		ShippmentItemID:     obj.ShippmentItemID,
		Status:              obj.Status,
		StockedQuantity:     obj.StockedQuantity,
		Subtotal:            obj.Subtotal,
		TaxnexusCodeDisplay: obj.TaxnexusCodeDisplay,
		TaxnexusCodeID:      obj.TaxnexusCodeID,
		TenantID:            obj.TenantID,
		UnitPrice:           obj.UnitPrice,
		Units:               obj.Units,
	}
}
