package app

import (
	"database/sql"
	"time"

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

// UnMarshalCluster decodes swagger to first class object
func UnMarshalCluster(s *devops_models.Cluster) *Cluster {
	if s.ID == "" {
		s.ID = uuid.New().String()
	}
	createdDate, e0 := time.Parse(dateTimeFormat, s.CreatedDate)
	lastModfiedDate, e1 := time.Parse(dateTimeFormat, s.LastModifiedDate)
	return &Cluster{
		ID:               s.ID,
		CreatedByID:      s.CreatedByID,
		Description:      s.Description,
		Environment:      s.Environment,
		Gateway:          s.Gateway,
		IPAddress:        s.IPAddress,
		LastModifiedByID: s.LastModifiedDate,
		Name:             s.Name,
		OwnerID:          s.OwnerID,
		Ref:              s.Ref,
		Status:           s.Status,
		Subnet:           s.Subnet,
		Type:             s.Type,
		Zone:             s.Zone,
		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 *Cluster) MarshalToSwagger() *devops_models.Cluster {
	return &devops_models.Cluster{
		ID:               obj.ID,
		CreatedByID:      obj.CreatedByID,
		CreatedDate:      obj.CreatedDate.Time.Format(dateTimeFormat),
		Description:      obj.Description,
		Environment:      obj.Environment,
		Gateway:          obj.Gateway,
		IPAddress:        obj.IPAddress,
		LastModifiedByID: obj.LastModifiedByID,
		LastModifiedDate: obj.LastModifiedDate.Time.Format(dateTimeFormat),
		Name:             obj.Name,
		OwnerID:          obj.OwnerID,
		Ref:              obj.Ref,
		Status:           obj.Status,
		Subnet:           obj.Subnet,
		Type:             obj.Type,
		Zone:             obj.Zone,
	}
}
