package child

import (
	"fmt"
	"strconv"

	"github.com/tidbcloud/dataflow-service-ng/pkg/lake"
)

type ResourceNames struct {
	Export         string
	Changefeed     string
	LakeDataSource string
	LakeTask       string
}

// DeterministicResourceNames returns names that are independent of the
// user-editable Pipeline name. Retries and edits therefore address the same
// children, and Lake resources can be adopted after an unknown create result.
func DeterministicResourceNames(pipelineID uint64) (ResourceNames, error) {
	if pipelineID == 0 {
		return ResourceNames{}, fmt.Errorf("%w: pipeline id is required", ErrInvalidPipeline)
	}
	id := strconv.FormatUint(pipelineID, 10)
	lakeNames, err := lake.DeterministicResourceNames(id)
	if err != nil {
		return ResourceNames{}, fmt.Errorf("%w: build Lake names", ErrInvalidPipeline)
	}
	return ResourceNames{
		Export:         "tidb-dp-export-" + id,
		Changefeed:     "tidb-dp-changefeed-" + id,
		LakeDataSource: lakeNames.DataSource,
		LakeTask:       lakeNames.Task,
	}, nil
}
