package app

import (
	"database/sql"
	"time"

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

// UnmarshalJob decodes swagger to a first class object
func UnmarshalJob(s *devops_models.Job) *Job {
	if s.ID == "" {
		s.ID = uuid.New().String()
	}
	endDate, e2 := time.Parse(dateFormat, s.EndDate)
	startDate, e3 := time.Parse(dateFormat, s.StartDate)
	createdDate, e0 := time.Parse(dateTimeFormat, s.CreatedDate)
	lastModfiedDate, e1 := time.Parse(dateTimeFormat, s.LastModifiedDate)
	jobDate, e4 := time.Parse(dateTimeFormat, s.JobDate)
	return &Job{
		ID:                 s.ID,
		AccountID:          *s.AccountID,
		BackendID:          s.BackendID,
		CompanyID:          s.CompanyID,
		CoordinateID:       s.CoordinateID,
		CreatedByID:        s.CreatedByID,
		Duration:           *s.Duration,
		ErrorReason:        s.ErrorReason,
		Interval:           s.Interval,
		LastModifiedByID:   s.LastModifiedByID,
		Month:              s.Month,
		NextJobID:          s.NextJobID,
		OwnerID:            s.OwnerID,
		Parameters:         s.Parameters,
		PeriodID:           s.PeriodID,
		Quarter:            s.Quarter,
		RatingEngineID:     s.RatingEngineID,
		Ref:                s.Ref,
		Reschedule:         s.Reschedule,
		RescheduleInterval: s.RescheduleInterval,
		SagaID:             s.SagaID,
		SagaType:           *s.SagaType,
		Semiannual:         s.Semiannual,
		Source:             s.Source,
		Status:             s.Status,
		Target:             s.Target,
		TenantID:           s.TenantID,
		Type:               *s.SagaType,
		Year:               s.Year,
		CreatedDate: sql.NullTime{
			Time:  createdDate,
			Valid: e0 == nil,
		},
		LastModifiedDate: sql.NullTime{
			Time:  lastModfiedDate,
			Valid: e1 == nil,
		},
		EndDate: sql.NullTime{
			Time:  endDate,
			Valid: e2 == nil,
		},
		StartDate: sql.NullTime{
			Time:  startDate,
			Valid: e3 == nil,
		},
		JobDate: sql.NullTime{
			Time:  jobDate,
			Valid: e4 == nil,
		},
	}
}

// MarshalToSwagger encodes a first class object to swagger
func (obj *Job) MarshalToSwagger() *devops_models.Job {
	startDate := obj.StartDate.Time.Format(dateTimeFormat)
	return &devops_models.Job{
		ID:                 obj.ID,
		AccountID:          &obj.AccountID,
		BackendID:          obj.BackendID,
		CompanyID:          obj.CompanyID,
		CoordinateID:       obj.CoordinateID,
		CreatedByID:        obj.CreatedByID,
		CreatedDate:        obj.CreatedDate.Time.Format(dateTimeFormat),
		Duration:           &obj.Duration,
		EndDate:            obj.EndDate.Time.Format(dateTimeFormat),
		ErrorReason:        obj.ErrorReason,
		Interval:           obj.Interval,
		JobDate:            obj.JobDate.Time.Format(dateTimeFormat),
		LastModifiedByID:   obj.LastModifiedDate.Time.Format(dateTimeFormat),
		LastModifiedDate:   obj.LastModifiedByID,
		Month:              obj.Month,
		NextJobID:          obj.NextJobID,
		OwnerID:            obj.OwnerID,
		Parameters:         obj.Parameters,
		PeriodID:           obj.PeriodID,
		Quarter:            obj.Quarter,
		RatingEngineID:     obj.RatingEngineID,
		Ref:                obj.Ref,
		Reschedule:         obj.Reschedule,
		RescheduleInterval: obj.RescheduleInterval,
		SagaID:             obj.SagaID,
		SagaType:           &obj.SagaType,
		Semiannual:         obj.Semiannual,
		Source:             obj.Source,
		StartDate:          startDate,
		Status:             obj.Status,
		Target:             obj.Target,
		TenantID:           obj.TenantID,
		Year:               obj.Year,
	}
}
