package app

import (
	"database/sql"
	"time"

	"code.tnxs.net/taxnexus/lib/api/devops/devops_models"
	"github.com/google/uuid"
)

// UnMarshalService decodes swagger to a first class object
func UnMarshalService(s *devops_models.Service) *Service {
	if s.ID == "" {
		s.ID = uuid.New().String()
	}
	createdDate, e0 := time.Parse(dateTimeFormat, s.CreatedDate)
	lastModfiedDate, e1 := time.Parse(dateTimeFormat, s.LastModifiedDate)
	return &Service{
		ID:               s.ID,
		Active:           s.Active,
		ClusterID:        s.ClusterID,
		ClusterIP:        s.ClusterIP,
		ClusterURL:       s.ClusterURL,
		CreatedByID:      s.CreatedByID,
		Environment:      s.Environment,
		ExternalURL:      s.ExternalURL,
		GELFAddress:      s.GELFAddress,
		LastModifiedByID: s.LastModifiedByID,
		NetworkAlias:     s.NetworkAlias,
		OpenAPIVersion:   s.OpenAPIVersion,
		OwnerID:          s.OwnerID,
		PortExternal:     s.PortExternal,
		PortTest:         s.PortTest,
		ProxyType:        s.ProxyType,
		Replicas:         s.Replicas,
		RepoURL:          s.RepoURL,
		ServiceName:      s.ServiceName,
		Version:          s.Version,
		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 *Service) MarshalToSwagger() *devops_models.Service {
	return &devops_models.Service{
		ID:               obj.ID,
		Active:           obj.Active,
		ClusterID:        obj.ClusterID,
		ClusterIP:        obj.ClusterIP,
		ClusterURL:       obj.ClusterURL,
		CreatedByID:      obj.CreatedByID,
		CreatedDate:      obj.CreatedDate.Time.Format(dateTimeFormat),
		Environment:      obj.Environment,
		ExternalURL:      obj.ExternalURL,
		GELFAddress:      obj.GELFAddress,
		LastModifiedByID: obj.LastModifiedByID,
		LastModifiedDate: obj.LastModifiedDate.Time.Format(dateTimeFormat),
		NetworkAlias:     obj.NetworkAlias,
		OpenAPIVersion:   obj.OpenAPIVersion,
		OwnerID:          obj.OwnerID,
		PortExternal:     obj.PortExternal,
		PortTest:         obj.PortTest,
		ProxyType:        obj.ProxyType,
		Replicas:         obj.Replicas,
		RepoURL:          obj.RepoURL,
		TenantID:         obj.TenantID,
		ServiceName:      obj.ServiceName,
		Version:          obj.Version,
	}
}
