package api

import (
	"time"
	"net/http"

	"github.com/labstack/echo/v4"

	"sku61.com/InvestAccnt/api/gen"
)


// Service domain health
// (GET /health)
func (self *BianApiService) GetHealth(ctx echo.Context) error {

	self.Lock.Lock()
	defer self.Lock.Unlock()

	description := "API for XXX"
	serviceName := ""

	health := gen.HealthModel{
		Description: &description,
		ServiceId: &serviceName,
		Status: gen.PASS,
	}

	health.Links = []gen.LinksModel{}
	
	hmc := self.newHealthModelChecks()

	asAt := time.Now()

	{
		var items1 []gen.HealthcheckModel
		status1 := "PASS"
		cid1 := "local-file-store"
		cty1 := "datastore"

		item1 := gen.HealthcheckModel{
			AsAt: &asAt,
			Status: &status1,
			ComponentId: &cid1,
			ComponentType: &cty1,
		}
		items1 = append(items1,item1)
		
		status2 := "PASS"
		cid2 := "process-server"
		cty2 := "system"

		item2 := gen.HealthcheckModel{
			AsAt: &asAt,
			Status: &status2,
			ComponentId: &cid2,
			ComponentType: &cty2,
		}
		items1 = append(items1,item2)

		hmc.Set("dependencies", items1)
	}

	{
		var items []gen.HealthcheckModel

		status1 := "PASS"
		cid1 := "process-server"
		cty1 := "system"
		observedValue := float32(asAt.Sub(self.StartTime).Seconds())
		observedUnit := "s"

		item := gen.HealthcheckModel{
			AsAt: &asAt,
			Status: &status1,
			ComponentId: &cid1,
			ComponentType: &cty1,
			ObservedValue: &observedValue,
			ObservedUnit: &observedUnit,
		}
		items = append(items,item)

		hmc.Set("uptime", items)
	}

	health.Checks = hmc

	return ctx.JSON(http.StatusOK, health)
}


func (self *BianApiService) newHealthModelChecks() gen.HealthModel_Checks {
	
		obj := gen.HealthModel_Checks{}		
		return obj
}
	


