package app

import (
	"database/sql"
	"time"

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

// UnMarshalBackend decodes swagger to a first class object
func UnMarshalBackend(s *regs_models.Backend) *Backend {
	if s.ID == "" {
		s.ID = uuid.New().String()
	}
	createdDate, e0 := time.Parse(dateTimeFormat, s.CreatedDate)
	lastModfiedDate, e1 := time.Parse(dateTimeFormat, s.LastModifiedDate)
	return &Backend{
		ID:                  s.ID,
		AccountID:           s.AccountID,
		Active:              s.Active,
		APIKey:              s.APIKey,
		ApplicationName:     s.ApplicationName,
		AuthType:            s.AuthType,
		BackendName:         s.BackendName,
		BaseURL:             s.BaseURL,
		CallbackURL:         s.CallbackURL,
		ClientID:            s.ClientID,
		ClientSecret:        s.ClientSecret,
		CompanyID:           s.CompanyID,
		CreatedByID:         s.CreatedByID,
		Description:         s.Description,
		LastModifiedByID:    s.LastModifiedByID,
		LoginURL:            s.LoginURL,
		ManagementPassword:  s.ManagementPassword,
		ManagementURL:       s.ManagementURL,
		ManagementUsername:  s.ManagementUsername,
		MetrcLicense:        s.MetrcLicense,
		MetrcState:          s.MetrcState,
		OwnerID:             s.OwnerID,
		Password:            s.Password,
		ProjectID:           s.ProjectID,
		ProviderCredentials: s.ProviderCredentials,
		Realm:               s.Realm,
		Ref:                 s.Ref,
		ResellerBackendID:   s.ResellerBackendID,
		SecurityToken:       s.SecurityToken,
		Timeout:             s.Timeout,
		TokenURI:            s.TokenURI,
		Type:                s.Type,
		Username:            s.Username,
		Vendor:              s.Vendor,
		TenantID:            s.TenantID,
		CreatedDate: sql.NullTime{
			Time:  createdDate,
			Valid: e0 == nil,
		},
		LastModifiedDate: sql.NullTime{
			Time:  lastModfiedDate,
			Valid: e1 == nil,
		},
	}
}

// MarshalToSwagger encodes a first class object to swagger
func (obj *Backend) MarshalToSwagger() *regs_models.Backend {
	return &regs_models.Backend{
		ID:                  obj.ID,
		AccountID:           obj.AccountID,
		Active:              obj.Active,
		APIKey:              obj.APIKey,
		ApplicationName:     obj.ApplicationName,
		AuthType:            obj.AuthType,
		BackendName:         obj.BackendName,
		BaseURL:             obj.BaseURL,
		CallbackURL:         obj.CallbackURL,
		ClientID:            obj.ClientID,
		ClientSecret:        obj.ClientSecret,
		CompanyID:           obj.CompanyID,
		CreatedByID:         obj.CreatedByID,
		CreatedDate:         obj.CreatedDate.Time.Format(dateTimeFormat),
		Description:         obj.Description,
		LastModifiedByID:    obj.LastModifiedByID,
		LastModifiedDate:    obj.LastModifiedDate.Time.Format(dateTimeFormat),
		LoginURL:            obj.LoginURL,
		ManagementPassword:  obj.ManagementPassword,
		ManagementURL:       obj.ManagementURL,
		ManagementUsername:  obj.ManagementUsername,
		MetrcLicense:        obj.MetrcLicense,
		MetrcState:          obj.MetrcState,
		OwnerID:             obj.OwnerID,
		Password:            obj.Password,
		ProjectID:           obj.ProjectID,
		ProviderCredentials: obj.ProviderCredentials,
		Realm:               obj.Realm,
		Ref:                 obj.Ref,
		ResellerBackendID:   obj.ResellerBackendID,
		SecurityToken:       obj.SecurityToken,
		TenantID:            obj.TenantID,
		Timeout:             obj.Timeout,
		TokenURI:            obj.TokenURI,
		Type:                obj.Type,
		Username:            obj.Username,
		Vendor:              obj.Vendor,
	}
}
