package system

type CheckStatus string

const (
	CheckStatusPass CheckStatus = "pass"
	CheckStatusWarn CheckStatus = "warn"
	CheckStatusFail CheckStatus = "fail"
)

type CheckResult struct {
	Name    string
	Status  CheckStatus
	Details []string
}

type Report struct {
	Checks []CheckResult
}

func (r *Report) Add(status CheckStatus, name string, details ...string) {
	r.Checks = append(r.Checks, CheckResult{
		Name:    name,
		Status:  status,
		Details: append([]string(nil), details...),
	})
}

func (r *Report) AddPass(name string, details ...string) {
	r.Add(CheckStatusPass, name, details...)
}

func (r *Report) AddWarn(name string, details ...string) {
	r.Add(CheckStatusWarn, name, details...)
}

func (r *Report) AddFail(name string, details ...string) {
	r.Add(CheckStatusFail, name, details...)
}

func (r *Report) AddPreflight(name string, checks *Preflight, successDetail string) {
	problems := checks.Problems()
	if len(problems) == 0 {
		if successDetail == "" {
			r.AddPass(name)
			return
		}
		r.AddPass(name, successDetail)
		return
	}
	r.AddFail(name, problems...)
}

func (r Report) HasFailures() bool {
	for _, check := range r.Checks {
		if check.Status == CheckStatusFail {
			return true
		}
	}
	return false
}
