package darwindb

import (
	"fmt"
	"strings"
)

// Affected tracks the TIPLOCs, RIDs and TSIDs which have been updated.
type Affected struct {
	TIPLOCs map[string]bool
	RIDs    map[string]bool
	TSIDs   map[int]bool
}

// NewAffected creates a new Affected instance.
func NewAffected() *Affected {
	return &Affected{
		TIPLOCs: make(map[string]bool),
		RIDs:    make(map[string]bool),
		TSIDs:   make(map[int]bool),
	}
}

// TIPLOC marks the given TIPLOC code as affected.
func (a *Affected) TIPLOC(t string) {
	if a != nil {
		a.TIPLOCs[t] = true
	}
}

// RID marks the given train RID identifier as affected.
func (a *Affected) RID(r string) {
	if a != nil {
		a.RIDs[r] = true
	}
}

// TSID marks the given train service ID (internal ID) as affected.
func (a *Affected) TSID(t int) {
	if a != nil {
		a.TSIDs[t] = true
	}
}

// Summary returns a string summary of affected assets.
func (a *Affected) Summary() string {
	if a == nil {
		return "change tracking disabled"
	}

	var s []string
	if len(a.TIPLOCs) > 0 {
		s = append(s, fmt.Sprintf("%d TIPLOCs", len(a.TIPLOCs)))
	}
	if len(a.RIDs) > 0 {
		s = append(s, fmt.Sprintf("%d RIDs", len(a.RIDs)))
	}
	if len(a.TSIDs) > 0 {
		s = append(s, fmt.Sprintf("%d TSIDs", len(a.TSIDs)))
	}
	if len(s) == 0 {
		return "no changes"
	}
	return fmt.Sprintf("%v", strings.Join(s, ", "))
}