package app

import (
	"database/sql"
	"time"

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

// UnMarshalSwaggerFiling decodes swagger to first class object
func UnMarshalSwaggerFiling(s *regs_models.Filing) *Filing {
	if s.ID == "" {
		s.ID = uuid.New().String()
	}
	createdDate, e0 := time.Parse(dateTimeFormat, s.CreatedDate)
	lastModfiedDate, e1 := time.Parse(dateTimeFormat, s.LastModifiedDate)
	date, e2 := time.Parse(dateFormat, s.Date)
	if e2 != nil {
		date, e2 = time.Parse(dateFormatAlt, s.Date)
	}
	dueDate, e3 := time.Parse(dateFormat, s.DueDate)
	if e3 != nil {
		dueDate, e3 = time.Parse(dateFormatAlt, s.DueDate)
	}
	return &Filing{
		ID:                  s.ID,
		AccountName:         s.AccountName,
		Amount:              s.Amount,
		ContactID:           s.ContactID,
		CreatedByID:         s.CreatedByID,
		FilingNumber:        s.FilingNumber,
		FilingTypeID:        s.FilingTypeID,
		Frequency:           s.Frequency,
		Interest:            s.Interest,
		InterestRate:        s.InterestRate,
		LastModifiedByID:    s.LastModifiedByID,
		MonthNumber:         s.MonthNumber,
		OwnerID:             s.OwnerID,
		Penalty:             s.Penalty,
		PenaltyDays:         s.PenaltyDays,
		PenaltyRate:         s.PenaltyRate,
		PeriodID:            s.PeriodID,
		PreparerID:          s.PreparerID,
		QuarterNumber:       s.QuarterNumber,
		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,
		SemiannualNumber:    s.SemiannualNumber,
		Status:              s.Status,
		SubmissionID:        s.SubmissionID,
		Subtotal:            s.Subtotal,
		TaxOnTax:            s.TaxOnTax,
		TaxTypeAccountID:    s.TaxTypeAccountID,
		TenantID:            s.TenantID,
		TotalAmount:         s.TotalAmount,
		UnitBase:            s.UnitBase,
		YearNumber:          s.YearNumber,
		CreatedDate: sql.NullTime{
			Time:  createdDate,
			Valid: e0 == nil,
		},
		LastModifiedDate: sql.NullTime{
			Time:  lastModfiedDate,
			Valid: e1 == nil,
		},
		Date: sql.NullTime{
			Time:  date,
			Valid: e2 == nil,
		},
		DueDate: sql.NullTime{
			Time:  dueDate,
			Valid: e3 == nil,
		},
	}
}

// MarshalToSwagger encodes first class object
func (obj *Filing) MarshalToSwagger() *regs_models.Filing {
	return &regs_models.Filing{
		ID:                  obj.ID,
		AccountName:         obj.AccountName,
		Amount:              obj.Amount,
		ContactID:           obj.ContactID,
		CreatedByID:         obj.CreatedByID,
		CreatedDate:         obj.CreatedDate.Time.Format(dateTimeFormat),
		Date:                obj.Date.Time.Format(dateFormat),
		DueDate:             obj.DueDate.Time.Format(dateFormat),
		FilingNumber:        obj.FilingNumber,
		FilingTypeID:        obj.FilingTypeID,
		Frequency:           obj.Frequency,
		Interest:            obj.Interest,
		InterestRate:        obj.InterestRate,
		LastModifiedByID:    obj.LastModifiedByID,
		LastModifiedDate:    obj.LastModifiedDate.Time.Format(dateTimeFormat),
		MonthNumber:         obj.MonthNumber,
		OwnerID:             obj.OwnerID,
		Penalty:             obj.Penalty,
		PenaltyDays:         obj.PenaltyDays,
		PenaltyRate:         obj.PenaltyRate,
		PeriodID:            obj.PeriodID,
		PreparerID:          obj.PreparerID,
		QuarterNumber:       obj.QuarterNumber,
		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,
		SemiannualNumber:    obj.SemiannualNumber,
		Status:              obj.Status,
		SubmissionID:        obj.SubmissionID,
		Subtotal:            obj.Subtotal,
		TaxOnTax:            obj.TaxOnTax,
		TaxTypeAccountID:    obj.TaxTypeAccountID,
		TenantID:            obj.TenantID,
		TotalAmount:         obj.TotalAmount,
		UnitBase:            obj.UnitBase,
		YearNumber:          obj.YearNumber,
	}
}
