package chrono

import (
	"testing"
	"time"
)

// This tests the TimeElapsedToString function and the TimeSinceToString function that implements it
func TestTimeElapsedToString(t *testing.T) {
	tests := []struct {
		startInput time.Time
		endInput   time.Time
		expected   string
	}{
		{time.Time{},
			time.Date(2025, time.June, 27, 11, 0, 0, 0, time.UTC),
			"Never"},
		{time.Date(2025, time.June, 27, 11, 0, 0, 0, time.UTC),
			time.Time{},
			"Never"},
		{time.Date(2025, time.June, 27, 11, 0, 0, 0, time.UTC),
			time.Date(2025, time.June, 27, 11, 0, 0, 999999999, time.UTC),
			"Just now"},
		{time.Date(2025, time.June, 27, 11, 0, 0, 0, time.UTC),
			time.Date(2025, time.June, 27, 11, 0, 0, 1, time.UTC),
			"Just now"},
		{time.Date(2025, time.June, 27, 11, 0, 0, 0, time.UTC),
			time.Date(2025, time.June, 27, 11, 1, 0, 1, time.UTC),
			"1 minute"},
		{time.Date(2025, time.June, 27, 11, 0, 0, 0, time.UTC),
			time.Date(2025, time.June, 27, 11, 3, 0, 1, time.UTC),
			"3 minutes"},
		{time.Date(2025, time.June, 27, 11, 0, 0, 0, time.UTC),
			time.Date(2025, time.June, 27, 12, 0, 0, 1, time.UTC),
			"1 hour"},
		{time.Date(2025, time.June, 27, 11, 0, 0, 0, time.UTC),
			time.Date(2025, time.June, 27, 13, 0, 0, 1, time.UTC),
			"2 hours"},
		{time.Date(2025, time.June, 27, 11, 0, 0, 0, time.UTC),
			time.Date(2025, time.June, 30, 11, 0, 0, 1, time.UTC),
			"3 days"},
		{time.Date(2025, time.January, 1, 0, 0, 0, 0, time.UTC),
			time.Date(2025, time.January, 8, 1, 0, 0, 1, time.UTC),
			"1 week"},
		{time.Date(2025, time.January, 1, 0, 0, 0, 0, time.UTC),
			time.Date(2025, time.January, 15, 1, 0, 0, 1, time.UTC),
			"2 weeks"},
		{time.Date(2025, time.January, 1, 0, 0, 0, 0, time.UTC),
			time.Date(2025, time.February, 7, 0, 0, 0, 1, time.UTC),
			"1 month"},
		{time.Date(2025, time.January, 1, 0, 0, 0, 0, time.UTC),
			time.Date(2025, time.March, 15, 0, 0, 0, 1, time.UTC),
			"2 months"},
		{time.Date(2025, time.January, 1, 0, 0, 0, 0, time.UTC),
			time.Date(2026, time.January, 1, 0, 0, 0, 1, time.UTC),
			"1 year"},
		{time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC),
			time.Date(2025, time.January, 1, 0, 0, 0, 1, time.UTC),
			"5 years"},
	}

	for _, tt := range tests {
		result := TimeElapsedToString(tt.startInput, tt.endInput, "UTC")
		if result != tt.expected {
			t.Errorf("TimeElapsedToString(%q, %q, \"UTC\") = %q; want %q", tt.startInput, tt.endInput, result, tt.expected)
		}
	}

	sinceTests := []struct {
		timeInput time.Time
		expected  string
	}{
		{time.Now().Add(-1 * time.Minute), "1 minute ago"},
		{time.Now().Add(-2 * time.Hour), "2 hours ago"},
		{time.Now().Add(-3 * time.Hour * 24), "3 days ago"},
		{time.Now(), "Just now"},
		{time.Now().Add(-5 * 7 * 24 * time.Hour), "1 month ago"},
	}

	for _, tt := range sinceTests {
		result := TimeSinceToString(tt.timeInput, "UTC")
		if result != tt.expected {
			t.Errorf("TimeSinceToString(%q, \"UTC\") = %q; want %q", tt.timeInput, result, tt.expected)
		}
	}
}
