package app

import (
	"database/sql"
	"time"

	"code.tnxs.net/taxnexus/lib/api/ledger/ledger_models"
	"github.com/google/uuid"
)

func UnMarshalGlAccount(s *ledger_models.GlAccount) *GlAccount {
	if s.ID == "" {
		s.ID = uuid.New().String()
	}
	createdDate, err1 := time.Parse(dateTimeFormat, s.CreatedDate)
	lastModifiedDate, err2 := time.Parse(dateTimeFormat, s.LastModifiedDate)
	return &GlAccount{
		ID:                s.ID,
		AccountID:         s.AccountID,
		AccountLevel:      s.AccountLevel,
		AccountName:       s.AccountName,
		AccountNumber:     s.AccountNumber,
		AccountSign:       s.AccountSign,
		AccountType:       s.AccountType,
		CreatedByID:       s.CreatedByID,
		Description:       s.Description,
		IsActive:          s.IsActive,
		IsBankAccount:     s.IsBankAccount,
		IsSummary:         s.IsSummary,
		LastModifiedByID:  s.LastModifiedByID,
		Name:              s.Name,
		ParentFK:          s.ParentFK,
		ParentGlAccountID: s.ParentGLAccountID,
		Ref:               s.Ref,
		Status:            s.Status,
		TenantID:          s.TenantID,
		CreatedDate: sql.NullTime{
			Time:  createdDate,
			Valid: err1 == nil,
		},
		LastModifiedDate: sql.NullTime{
			Time:  lastModifiedDate,
			Valid: err2 == nil,
		},
	}
}

// MarshalToSwagger encodes first class object
func (obj *GlAccount) MarshalToSwagger() *ledger_models.GlAccount {
	return &ledger_models.GlAccount{
		ID:                obj.ID,
		AccountID:         obj.AccountID,
		AccountLevel:      obj.AccountLevel,
		AccountName:       obj.AccountName,
		AccountNumber:     obj.AccountNumber,
		AccountSign:       obj.AccountSign,
		AccountType:       obj.AccountType,
		Balances:          []*ledger_models.GlBalance{},
		CreatedByID:       obj.CreatedByID,
		CreatedDate:       obj.CreatedDate.Time.Format(dateTimeFormat),
		Description:       obj.Description,
		IsActive:          obj.IsActive,
		IsBankAccount:     obj.IsBankAccount,
		IsSummary:         obj.IsSummary,
		LastModifiedByID:  obj.LastModifiedByID,
		LastModifiedDate:  obj.LastModifiedDate.Time.Format(dateTimeFormat),
		Name:              obj.Name,
		ParentFK:          obj.ParentFK,
		ParentGLAccountID: obj.ParentGlAccountID,
		Ref:               obj.Ref,
		Status:            obj.Status,
		TenantID:          obj.TenantID,
	}
}
