package testutils

import (
	"testing"
	"time"
)

// TestTimer mesure la durée d'un test (T0050)
type TestTimer struct {
	start time.Time
	t     *testing.T
	name  string
}

// StartTimer démarre un timer de test (T0050)
func StartTimer(t *testing.T) *TestTimer {
	return &TestTimer{
		start: time.Now(),
		t:     t,
		name:  t.Name(),
	}
}

// StartNamedTimer démarre un timer avec un nom personnalisé (T0050)
func StartNamedTimer(t *testing.T, name string) *TestTimer {
	return &TestTimer{
		start: time.Now(),
		t:     t,
		name:  name,
	}
}

// Stop arrête le timer et log la durée (T0050)
func (tt *TestTimer) Stop() time.Duration {
	duration := time.Since(tt.start)
	tt.t.Logf("Test duration: %v", duration)
	return duration
}

// WarnIfSlow avertit si le test est lent (T0050)
func (tt *TestTimer) WarnIfSlow(threshold time.Duration) time.Duration {
	duration := tt.Stop()
	if duration > threshold {
		tt.t.Logf("WARNING: Test '%s' took %v (threshold: %v)", tt.name, duration, threshold)
	}
	return duration
}

// Elapsed retourne la durée écoulée sans arrêter le timer (T0050)
func (tt *TestTimer) Elapsed() time.Duration {
	return time.Since(tt.start)
}

// Reset réinitialise le timer (T0050)
func (tt *TestTimer) Reset() {
	tt.start = time.Now()
}

// Example usage:
/*
func TestSlowOperation(t *testing.T) {
	timer := StartTimer(t)
	defer timer.WarnIfSlow(5 * time.Second)

	// Test code
	time.Sleep(2 * time.Second)
}

func TestNamedTimer(t *testing.T) {
	timer := StartNamedTimer(t, "database-operation")
	defer timer.WarnIfSlow(3 * time.Second)

	// Test code
}

func TestMultipleOperations(t *testing.T) {
	timer := StartTimer(t)
	defer timer.Stop()

	// First operation
	operation1(timer)

	// Reset for second operation
	timer.Reset()
	operation2(timer)
}

func operation1(timer *TestTimer) {
	// Operation 1 code
	duration := timer.Elapsed()
	timer.t.Logf("Operation 1 took: %v", duration)
}

func operation2(timer *TestTimer) {
	// Operation 2 code
	duration := timer.Elapsed()
	timer.t.Logf("Operation 2 took: %v", duration)
}
*/
