// Copyright 2021 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

// This file only contains version related variables and consts, all
// type definitions and functions shall not be implemented here.
// This file is excluded from CI tests.

package configuration

import (
	"io"

	"github.com/pingcap/log"
	"go.uber.org/zap"

	"github.com/pingcap-inc/clinic/pkg/stream"
)

type ConfigurationRecord struct {
	Content    string `json:"content"`
	Host       string `json:"host"`
	Path       string `json:"path"`
	PackageID  string `json:"package_id"`
	InstanceID string `json:"instance"`
	Component  string `json:"component"`
}

type ConfigurationIterator struct {
	reader    io.Reader
	path      string
	packageID string
	host      string
	instance  string
	component string
}

func NewConfigurationIterator(reader io.Reader, path, packageID, instance, host, component string) stream.Iterator {
	return &ConfigurationIterator{
		reader:    reader,
		path:      path,
		packageID: packageID,
		instance:  instance,
		host:      host,
		component: component,
	}
}

// Next will only return once for ConfigurationIterator
func (ci *ConfigurationIterator) Next() (any, bool) {
	content, err := io.ReadAll(ci.reader)
	if err != nil || len(content) == 0 {
		if err != nil && err != io.EOF {
			log.Error("read configuration failed", zap.Error(err))
		}
		return ConfigurationRecord{}, false
	}
	// host, path := splitHostPath(ci.path)
	return ConfigurationRecord{
		Content:    string(content),
		Host:       ci.host,
		Path:       ci.path,
		PackageID:  ci.packageID,
		InstanceID: ci.instance,
		Component:  ci.component,
	}, true
}

func (ci *ConfigurationIterator) Len() (int, bool) {
	return -1, false
}
