package executor

import (
	"testing"
	"time"
)

func TestDurationEncodeDecode(t *testing.T) {
	cases := []cqlDuration{
		{months: 0, days: 0, nanos: 0},
		{months: 1, days: 2, nanos: 3},
		{months: -2, days: -3, nanos: -4},
		{months: 12, days: 0, nanos: int64(5*time.Hour + 6*time.Minute + 7*time.Second + 8*time.Millisecond + 9*time.Microsecond + 10)},
	}

	for _, tc := range cases {
		encoded := encVints(tc.months, tc.days, tc.nanos)
		decoded, err := decVints(encoded)
		if err != nil {
			t.Fatalf("decVints failed: %v", err)
		}
		if decoded.months != tc.months || decoded.days != tc.days || decoded.nanos != tc.nanos {
			t.Fatalf("round-trip mismatch: got %+v, want %+v", decoded, tc)
		}
	}
}

func TestDurationParseFormatRoundTrip(t *testing.T) {
	inputs := []string{
		"0",
		"1y2mo3d4h5m6s7ms8us9ns",
		"-2mo3d4h5m",
		"1:2:3",
	}

	for _, input := range inputs {
		parsed, ok := parseDurationString(input)
		if !ok {
			t.Fatalf("failed to parse duration %q", input)
		}
		formatted := formatDurationValue(parsed)
		parsedAgain, ok := parseDurationString(formatted)
		if !ok {
			t.Fatalf("failed to parse formatted duration %q", formatted)
		}
		if parsedAgain != parsed {
			t.Fatalf("round-trip mismatch: input=%q formatted=%q got=%+v want=%+v", input, formatted, parsedAgain, parsed)
		}
	}
}
