package app

import (
	"database/sql"
	"time"

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

// UnMarshalSwaggerAuthority decodes swagger to first class object
func UnMarshalSwaggerAuthority(s *regs_models.Authority) *Authority {
	if s.ID == "" {
		s.ID = uuid.New().String()
	}
	authorityDate, e4 := time.Parse(dateFormat, s.Date)
	createdDate, e0 := time.Parse(dateTimeFormat, s.CreatedDate)
	dateApproved, e2 := time.Parse(dateFormat, s.DateApproved)
	if e2 != nil {
		dateApproved, e2 = time.Parse(dateFormatAlt, s.DateApproved)
	}
	lastModfiedDate, e1 := time.Parse(dateTimeFormat, s.LastModifiedDate)
	transferDate, e3 := time.Parse(dateFormat, s.TransferDate)
	if e3 != nil {
		transferDate, e3 = time.Parse(dateFormatAlt, s.TransferDate)
	}
	return &Authority{
		ID:               s.ID,
		AccountID:        s.AccountID,
		AddressLine1:     s.AddressLine1,
		AddressLine2:     s.AddressLine2,
		BTN:              s.BTN,
		City:             s.City,
		ContactID:        s.ContactID,
		Country:          s.Country,
		CreatedByID:      s.CreatedByID,
		LastModifiedByID: s.LastModifiedByID,
		LosingCarrier:    s.LosingCarrier,
		Name:             s.Name,
		NameLine1:        s.NameLine1,
		NameLine2:        s.NameLine2,
		OpportunityID:    s.OpportunityID,
		OrderID:          s.OrderID,
		ParentFK:         s.ParentFK,
		PostalCode:       s.PostalCode,
		QuoteID:          s.QuoteID,
		Ref:              s.Ref,
		State:            s.State,
		Status:           s.Status,
		TemplateID:       s.TemplateID,
		TenantID:         s.TenantID,
		Type:             s.Type,
		Date: sql.NullTime{
			Time:  authorityDate,
			Valid: e4 == nil,
		},
		CreatedDate: sql.NullTime{
			Time:  createdDate,
			Valid: e0 == nil,
		},
		LastModifiedDate: sql.NullTime{
			Time:  lastModfiedDate,
			Valid: e1 == nil,
		},
		DateApproved: sql.NullTime{
			Time:  dateApproved,
			Valid: e2 == nil,
		},
		TransferDate: sql.NullTime{
			Time:  transferDate,
			Valid: e3 == nil,
		},
	}
}

// MarshalToSwagger encodes first class object
func (obj *Authority) MarshalToSwagger() *regs_models.Authority {
	return &regs_models.Authority{
		ID:               obj.ID,
		AccountID:        obj.AccountID,
		AddressLine1:     obj.AddressLine1,
		AddressLine2:     obj.AddressLine2,
		BTN:              obj.BTN,
		City:             obj.City,
		ContactID:        obj.ContactID,
		Country:          obj.Country,
		CreatedByID:      obj.CreatedByID,
		CreatedDate:      obj.CreatedDate.Time.Format(dateTimeFormat),
		Date:             obj.Date.Time.Format(dateFormat),
		DateApproved:     obj.DateApproved.Time.Format(dateFormat),
		LastModifiedByID: obj.LastModifiedByID,
		LastModifiedDate: obj.LastModifiedDate.Time.Format(dateTimeFormat),
		LosingCarrier:    obj.LosingCarrier,
		Name:             obj.Name,
		NameLine1:        obj.NameLine1,
		NameLine2:        obj.NameLine2,
		OpportunityID:    obj.OpportunityID,
		OrderID:          obj.OrderID,
		ParentFK:         obj.ParentFK,
		PostalCode:       obj.PostalCode,
		QuoteID:          obj.QuoteID,
		Ref:              obj.Ref,
		State:            obj.State,
		Status:           obj.Status,
		TransferDate:     obj.TransferDate.Time.Format(dateFormat),
		TenantID:         obj.TenantID,
		TemplateID:       obj.TemplateID,
		Type:             obj.Type,
	}
}
