package app

import (
	"database/sql"
	"time"

	"code.tnxs.net/taxnexus/lib/api/regs/regs_models"
	"github.com/google/uuid"
)

// UnMarshalSubmission decodes swagger to first class object
func UnMarshalSubmission(s *regs_models.Submission) *Submission {
	if s.ID == "" {
		s.ID = uuid.New().String()
	}
	createdDate, e0 := time.Parse(dateTimeFormat, s.CreatedDate)
	lastModfiedDate, e1 := time.Parse(dateTimeFormat, s.LastModifiedDate)
	submissionDate, e2 := time.Parse(dateFormat, s.SubmissionDate)
	if e2 != nil {
		submissionDate, e2 = time.Parse(dateFormatAlt, s.SubmissionDate)
	}
	return &Submission{
		ID:                  s.ID,
		CompanyID:           s.CompanyID,
		ContactID:           s.ContactID,
		CreatedByID:         s.CreatedByID,
		LastModifiedByID:    s.LastModifiedByID,
		Notes:               s.Notes,
		Penalty:             s.Penalty,
		ParentFK:            s.ParentFK,
		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,
		SubmissionNumber:    s.SubmissionNumber,
		TaxTypeID:           s.TaxTypeID,
		TenantID:            s.TenantID,
		TotalAmount:         s.TotalAmount,
		CreatedDate: sql.NullTime{
			Time:  createdDate,
			Valid: e0 == nil,
		},
		LastModifiedDate: sql.NullTime{
			Time:  lastModfiedDate,
			Valid: e1 == nil,
		},
		SubmissionDate: sql.NullTime{
			Time:  submissionDate,
			Valid: e2 == nil,
		},
	}
}

// MarshalToSwagger encodes first class object
func (obj *Submission) MarshalToSwagger() *regs_models.Submission {
	return &regs_models.Submission{
		ID:                  obj.ID,
		CompanyID:           obj.CompanyID,
		ContactID:           obj.ContactID,
		CreatedByID:         obj.CreatedByID,
		CreatedDate:         obj.CreatedDate.Time.Format(dateTimeFormat),
		LastModifiedByID:    obj.LastModifiedByID,
		LastModifiedDate:    obj.LastModifiedDate.Time.Format(dateTimeFormat),
		Notes:               obj.Notes,
		ParentFK:            obj.ParentFK,
		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,
		SubmissionDate:      obj.SubmissionDate.Time.Format(dateFormat),
		SubmissionNumber:    obj.SubmissionNumber,
		Subtotal:            obj.Subtotal,
		TaxTypeID:           obj.TaxTypeID,
		TenantID:            obj.TenantID,
		TotalAmount:         obj.TotalAmount,
	}
}
