// Copyright 2023 The etcd Authors
//
// 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package report

import (
	"encoding/json"
	"fmt"
	"os"
	"path"
	"path/filepath"

	"go.uber.org/zap"

	"go.etcd.io/etcd/tests/v3/framework/e2e"
)

type TestReport struct {
	logger          *zap.Logger
	reportPath      string
	serverDataPaths map[string]string
	clientReports   []ClientReport
	visualize       func(lg *zap.Logger, path string) error
	traffic         *TrafficDetail
	dataSaved       bool
}

func NewTestReport(lg *zap.Logger, reportPath string, serverDataPaths map[string]string, traffic *TrafficDetail) (*TestReport, error) {
	if reportPath == "" {
		return nil, fmt.Errorf("reportPath is not set")
	}
	return &TestReport{
		logger:          lg,
		reportPath:      reportPath,
		serverDataPaths: serverDataPaths,
		traffic:         traffic,
	}, nil
}

func (r *TestReport) SetClientReports(reports []ClientReport) {
	r.clientReports = reports
}

func (r *TestReport) SetVisualizer(visualize func(lg *zap.Logger, path string) error) {
	r.visualize = visualize
}

func (r *TestReport) SaveEtcdData() error {
	if r.dataSaved {
		return nil
	}
	r.logger.Info("Saving etcd data", zap.String("path", r.reportPath))
	err := os.RemoveAll(r.reportPath)
	if err != nil {
		r.logger.Error("Failed to remove report dir", zap.Error(err))
	}
	for server, dataPath := range r.serverDataPaths {
		serverReportPath := filepath.Join(r.reportPath, fmt.Sprintf("server-%s", server))
		r.logger.Info("Saving member data dir", zap.String("member", server), zap.String("data-dir", dataPath), zap.String("path", serverReportPath))
		if err := os.CopyFS(serverReportPath, os.DirFS(dataPath)); err != nil {
			return err
		}
	}
	if r.clientReports != nil {
		if err := persistClientReports(r.logger, r.reportPath, r.clientReports); err != nil {
			return err
		}
	}
	if r.traffic != nil {
		if err := persistTrafficDetail(r.logger, r.reportPath, *r.traffic); err != nil {
			return err
		}
	}
	r.dataSaved = true
	return nil
}

func (r *TestReport) Finalize(tFailed bool, panicked bool) error {
	_, persistResults := os.LookupEnv("PERSIST_RESULTS")
	keep := tFailed || panicked || persistResults
	if !keep {
		if !r.dataSaved {
			return nil
		}
		r.logger.Info("Removing robustness test report", zap.String("path", r.reportPath))
		return os.RemoveAll(r.reportPath)
	}

	if err := r.SaveEtcdData(); err != nil {
		return fmt.Errorf("failed to save etcd data: %w", err)
	}

	if r.visualize == nil {
		r.logger.Info("No visualization available to be saved", zap.String("path", r.reportPath))
		return nil
	}
	r.logger.Info("Adding visualization to test report", zap.String("path", r.reportPath))
	return r.visualize(r.logger, filepath.Join(r.reportPath, "history.html"))
}

func ServerDataPaths(c *e2e.EtcdProcessCluster) map[string]string {
	dataPaths := make(map[string]string)
	for _, member := range c.Procs {
		dataPaths[member.Config().Name] = memberDataDir(member)
	}

	return dataPaths
}

func memberDataDir(member e2e.EtcdProcess) string {
	lazyFS := member.LazyFS()
	if lazyFS != nil {
		return filepath.Join(lazyFS.LazyFSDir, "data")
	}
	return member.Config().DataDirPath
}

type TrafficDetail struct {
	ExpectUniqueRevision bool `json:"expectuniquerevision,omitempty"`
}

const trafficDetailFileName = "traffic.json"

func persistTrafficDetail(lg *zap.Logger, p string, td TrafficDetail) error {
	lg.Info("Saving traffic configuration details", zap.String("path", path.Join(p, trafficDetailFileName)))
	b, err := json.Marshal(td)
	if err != nil {
		return nil
	}
	return os.WriteFile(filepath.Join(p, trafficDetailFileName), b, 0o644)
}

func LoadTrafficDetail(p string) (TrafficDetail, error) {
	var detail TrafficDetail
	b, err := os.ReadFile(filepath.Join(p, trafficDetailFileName))
	if err != nil {
		return TrafficDetail{}, err
	}
	err = json.Unmarshal(b, &detail)
	return detail, err
}
