package etcdutil

import (
	"fmt"
	"path"
	"strconv"
)

// WorkerPathPrefix format `/rootPath/worker`
func WorkerPathPrefix(rootPath string) string {
	return path.Join(rootPath, "worker")
}

// WorkerPath format `/rootPath/worker/workerID`
func WorkerPath(rootPath string, workerID uint64) string {
	return path.Join(WorkerPathPrefix(rootPath), fmt.Sprintf("%020d", workerID))
}

// JobPathPrefix format `/rootPath/job`
func JobPathPrefix(rootPath string) string {
	return path.Join(rootPath, "job")
}

// JobPath format `/rootPath/job/jobID`
func JobPath(rootPath string, jobID uint64) string {
	return path.Join(JobPathPrefix(rootPath), fmt.Sprintf("%020d", jobID))
}

// ContractPathPrefix format `/rootPath/contract`
func ContractPathPrefix(rootPath string) string {
	return path.Join(rootPath, "contract")
}

// ContractPath format `worker 1 and job 2 -> /rootPath/contract/1/2`
func ContractPath(rootPath string, jobID uint64, workerID uint64) string {
	return path.Join(ContractPathPrefix(rootPath), fmt.Sprintf("%020d", jobID), fmt.Sprintf("%020d", workerID))
}

// FencePath format `/rootPath/fence/fenceID`
func FencePath(rootPath string, fenceID uint64) string {
	return path.Join(rootPath, "fence", fmt.Sprintf("%020d", fenceID))
}

// PipelinePathPrefix format `/rootPath/pipelines`
func PipelinePathPrefix(rootPath string) string {
	return path.Join(rootPath, "pipelines")
}

// PipelinePath format `/rootPath/pipelines/jobID`
func PipelinePath(rootPath string, jobID uint64) string {
	return path.Join(PipelinePathPrefix(rootPath), fmt.Sprintf("%020d", jobID))
}

// ConsumerPartitionPath returns an consumer partition path.
// /[rootpath]/topic/[topic]/job/[jobid]
func ConsumerPartitionPath(rootpath string, topic string, jobID string) string {
	return PartitionPathPrefix(rootpath, topic, jobID)
}

// ProducerPartitionPath for producer saving partition and offset.
// /[rootpath]/topic/[topic]/job/[producer]/latest
func ProducerPartitionPath(rootpath string, topic string) string {
	return path.Join(PartitionPathPrefix(rootpath, topic, "producer"), "latest")
}

// ProducerPartitionSavepointPath for producer saving block partition and offset.
// [rootpath]/topic/[topic]/job/[producer]/[savepointID]
func ProducerPartitionSavepointPath(rootpath string, topic, savepointID string) string {
	return path.Join(PartitionPathPrefix(rootpath, topic, "producer"), savepointID)
}

// PartitionPathPrefix returns a job path in ether-client.
// /[rootpath]/topic/[topic]/job/[jobid]
func PartitionPathPrefix(rootpath string, topic string, jobID string) string {
	return path.Join(rootpath, "topic", topic, "job", jobID)
}

// SchemaMetaPath returns schema meta info path in ether-client.
// [rootpath]/schema_info/host/port/meta   value: [schema:{db, [table]}]
func SchemaMetaPath(rootpath string, host string, port int32) string {
	return path.Join(SchemaPathPrefix(rootpath, host, port), "meta")
}

// SchemaDetailPath returns schema detail info path in ether-client.
// [rootpath]/schema_info/host/port/detail/schema/[schema]/table/[table]
func SchemaDetailPath(rootpath string, host string, port int32, schema, table string) string {
	return path.Join(SchemaDetailPathPrefix(rootpath, host, port), "schema", schema, "table", table)
}

// SchemaDetailPathPrefix defines prefix path of schema detail.
// e.g. [rootpath]/schema_info/host/port/detail
func SchemaDetailPathPrefix(rootpath string, host string, port int32) string {
	return path.Join(SchemaPathPrefix(rootpath, host, port), "detail")
}

// SchemaPathPrefix defines prefix of schema path.
// [rootpath]/schema_info/host/port
func SchemaPathPrefix(rootpath string, host string, port int32) string {
	portStr := strconv.FormatInt(int64(port), 10)
	return path.Join(rootpath, "schema_info", host, portStr)
}

func fmtInt32(i int32) string {
	return strconv.FormatInt(int64(i), 10)
}
