// Package migrate seeds per-REAL-service databases by dumping schema and
// optionally data from the staging database and applying it to the local
// TiDB Cloud Zero instance.
package migrate

import (
	"fmt"
	"strings"
)

// Result holds the outcome of a migration run.
type Result struct {
	Services []ServiceResult `json:"services"`
	Reset    bool            `json:"reset,omitempty"`
}

// ServiceResult holds the outcome for a single service.
type ServiceResult struct {
	Service string `json:"service"`
	Status  string `json:"status"` // "ok", "skipped", "failed", "cached"
	Error   string `json:"error,omitempty"`
}

// FormatText implements model.TextFormatter.
func (r *Result) FormatText() string {
	var b strings.Builder
	if r.Reset {
		b.WriteString("database migration seed (reset)\n\n")
	} else {
		b.WriteString("database migration seed\n\n")
	}
	for _, svc := range r.Services {
		switch svc.Status {
		case "ok":
			fmt.Fprintf(&b, "  [ok]     %s\n", svc.Service)
		case "cached":
			fmt.Fprintf(&b, "  [cached] %s\n", svc.Service)
		case "skipped":
			fmt.Fprintf(&b, "  [skip]   %s\n", svc.Service)
		case "failed":
			fmt.Fprintf(&b, "  [FAIL]   %s: %s\n", svc.Service, svc.Error)
		}
	}
	return b.String()
}
