package main

import (
	"flag"
	"fmt"
	"os"

	"github.com/jfraeys/fetch_ml/tools"
)

func main() {
	baselinePath := flag.String("baseline", "", "Path to baseline 'go test -bench' output")
	currentPath := flag.String("current", "", "Path to current 'go test -bench' output")
	threshold := flag.Float64("threshold", 10.0, "Regression threshold percentage")
	flag.Parse()

	if *baselinePath == "" || *currentPath == "" {
		_, _ = fmt.Fprintln(os.Stderr, "usage: performance-regression-detector -baseline <file> -current <file> [-threshold <percent>]")
		os.Exit(2)
	}

	baselineResults, err := tools.ParseGoBenchFile(*baselinePath)
	if err != nil {
		_, _ = fmt.Fprintf(os.Stderr, "failed to parse baseline: %v\n", err)
		os.Exit(1)
	}

	currentResults, err := tools.ParseGoBenchFile(*currentPath)
	if err != nil {
		_, _ = fmt.Fprintf(os.Stderr, "failed to parse current: %v\n", err)
		os.Exit(1)
	}

	detector := tools.NewPerformanceRegressionDetector("", *threshold)
	report, err := detector.AnalyzeParsedResults(baselineResults, currentResults)
	if err != nil {
		_, _ = fmt.Fprintf(os.Stderr, "failed to analyze results: %v\n", err)
		os.Exit(1)
	}

	detector.PrintReport(report)

	if len(report.Regressions) > 0 {
		os.Exit(1)
	}
}
