package app

import (
	"database/sql"
	"time"

	"code.tnxs.net/taxnexus/lib/api/devops/devops_models"
	"github.com/google/uuid"
)

// UnmarshalIngest decodes swagger to a first class object
func UnmarshalIngest(s *devops_models.Ingest) *Ingest {
	if s.ID == "" {
		s.ID = uuid.New().String()
	}
	createdDate, e0 := time.Parse(dateTimeFormat, s.CreatedDate)
	lastModfiedDate, e1 := time.Parse(dateTimeFormat, s.LastModifiedDate)
	endDate, e2 := time.Parse(dateTimeFormat, s.EndDate)
	startDate, e3 := time.Parse(dateTimeFormat, s.StartDate)
	ingestDate, e4 := time.Parse(dateTimeFormat, s.IngestDate)
	m1, e5 := time.Parse(dateTimeFormat, s.MetrcLastModifiedEnd)
	m2, e6 := time.Parse(dateTimeFormat, s.MetrcLastModifiedStart)
	return &Ingest{
		ID:                  s.ID,
		AccountID:           *s.AccountID,
		Amount:              s.Amount,
		BackendID:           s.BackendID,
		CompanyID:           s.CompanyID,
		CreatedByID:         s.CreatedByID,
		Description:         s.Description,
		Filename:            s.Filename,
		IngestFailureReason: s.IngestFailureReason,
		IngestType:          s.IngestType,
		InvoiceCount:        s.InvoiceCount,
		JobID:               s.JobID,
		LastModifiedByID:    s.LastModifiedByID,
		MetrcLicense:        s.MetrcLicense,
		MetrcSalesReceiptID: s.MetrcSalesreceiptID,
		MetrcState:          s.MetrcState,
		ObjectType:          *s.ObjectType,
		ParentFK:            s.ParentFK,
		PeriodID:            s.PeriodID,
		PoCount:             s.POCount,
		PostFailureReason:   s.PostFalureReason,
		RatingEngineID:      s.RatingEngineID,
		Ref:                 s.Ref,
		RevenueBase:         s.RevenueBase,
		RevenueNet:          s.RevenueNet,
		RevenueNotTaxable:   s.RevenueNotTaxable,
		SagaID:              s.SagaID,
		SagaType:            s.SagaType,
		Source:              s.Source,
		Status:              s.Status,
		Tax:                 s.Tax,
		TaxOnTax:            s.TaxOnTax,
		TaxTransactionCount: s.TaxTransactionCount,
		TemplateID:          s.TemplateID,
		TenantID:            s.TenantID,
		UnitBase:            s.UnitBase,
		CreatedDate: sql.NullTime{
			Time:  createdDate,
			Valid: e0 == nil,
		},
		LastModifiedDate: sql.NullTime{
			Time:  lastModfiedDate,
			Valid: e1 == nil,
		},
		EndDate: sql.NullTime{
			Time:  endDate,
			Valid: e2 == nil,
		},
		StartDate: sql.NullTime{
			Time:  startDate,
			Valid: e3 == nil,
		},
		IngestDate: sql.NullTime{
			Time:  ingestDate,
			Valid: e4 == nil,
		},
		MetrcLastModifiedEnd: sql.NullTime{
			Time:  m1,
			Valid: e5 == nil,
		},
		MetrcLastModifiedStart: sql.NullTime{
			Time:  m2,
			Valid: e6 == nil,
		},
	}
}

// MarshalToSwagger encodes a first class object to swagger
func (obj *Ingest) MarshalToSwagger() *devops_models.Ingest {
	return &devops_models.Ingest{
		ID:                     obj.ID,
		AccountID:              &obj.AccountID,
		Amount:                 obj.Amount,
		BackendID:              obj.BackendID,
		CompanyID:              obj.CompanyID,
		CreatedByID:            obj.CreatedByID,
		CreatedDate:            obj.CreatedDate.Time.Format(dateTimeFormat),
		Description:            obj.Description,
		EndDate:                obj.EndDate.Time.Format(dateTimeFormat),
		Filename:               obj.Filename,
		IngestDate:             obj.IngestDate.Time.Format(dateTimeFormat),
		IngestFailureReason:    obj.IngestFailureReason,
		IngestType:             obj.IngestType,
		InvoiceCount:           obj.InvoiceCount,
		JobID:                  obj.JobID,
		LastModifiedByID:       obj.LastModifiedByID,
		LastModifiedDate:       obj.LastModifiedDate.Time.Format(dateTimeFormat),
		MetrcLastModifiedEnd:   obj.MetrcLastModifiedEnd.Time.Format(dateTimeFormat),
		MetrcLastModifiedStart: obj.MetrcLastModifiedStart.Time.Format(dateTimeFormat),
		MetrcLicense:           obj.MetrcLicense,
		MetrcSalesreceiptID:    obj.MetrcSalesReceiptID,
		MetrcState:             obj.MetrcState,
		ObjectType:             &obj.ObjectType,
		ParentFK:               obj.ParentFK,
		PeriodID:               obj.PeriodID,
		POCount:                obj.PoCount,
		PostFalureReason:       obj.PostFailureReason,
		RatingEngineID:         obj.RatingEngineID,
		Ref:                    obj.Ref,
		RevenueBase:            obj.RevenueBase,
		RevenueNet:             obj.RevenueNet,
		RevenueNotTaxable:      obj.RevenueNotTaxable,
		SagaID:                 obj.SagaID,
		SagaType:               obj.SagaType,
		Source:                 obj.Source,
		StartDate:              obj.StartDate.Time.Format(dateTimeFormat),
		Status:                 obj.Status,
		Tax:                    obj.Tax,
		TaxOnTax:               obj.TaxOnTax,
		TaxTransactionCount:    obj.TaxTransactionCount,
		TemplateID:             obj.TemplateID,
		UnitBase:               obj.UnitBase,
	}
}
