package core

import (
	"testing"
	"time"
)

func TestParseDateOrDefault(t *testing.T) {
	dflt := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)

	t.Run("string vacio retorna dflt", func(t *testing.T) {
		got := ParseDateOrDefault("", dflt, false)
		if !got.Equal(dflt) {
			t.Errorf("got %v, want %v", got, dflt)
		}
	})

	t.Run("formato invalido retorna dflt", func(t *testing.T) {
		got := ParseDateOrDefault("no-es-fecha", dflt, false)
		if !got.Equal(dflt) {
			t.Errorf("got %v, want %v", got, dflt)
		}
	})

	t.Run("YYYY-MM-DD sin endOfDay retorna inicio de dia", func(t *testing.T) {
		got := ParseDateOrDefault("2024-03-15", dflt, false)
		want := time.Date(2024, 3, 15, 0, 0, 0, 0, time.UTC)
		if !got.Equal(want) {
			t.Errorf("got %v, want %v", got, want)
		}
	})

	t.Run("YYYY-MM-DD con endOfDay retorna ultimo nanosegundo del dia", func(t *testing.T) {
		got := ParseDateOrDefault("2024-03-15", dflt, true)
		want := time.Date(2024, 3, 15, 23, 59, 59, 999999999, time.UTC)
		if !got.Equal(want) {
			t.Errorf("got %v, want %v", got, want)
		}
	})

	t.Run("RFC3339Nano sin endOfDay retorna timestamp exacto", func(t *testing.T) {
		input := "2024-03-15T14:30:00.123456789Z"
		got := ParseDateOrDefault(input, dflt, false)
		want, _ := time.Parse(time.RFC3339Nano, input)
		if !got.Equal(want) {
			t.Errorf("got %v, want %v", got, want)
		}
	})

	t.Run("RFC3339Nano con endOfDay no se ajusta", func(t *testing.T) {
		input := "2024-03-15T00:00:00Z"
		got := ParseDateOrDefault(input, dflt, true)
		want, _ := time.Parse(time.RFC3339Nano, input)
		if !got.Equal(want) {
			t.Errorf("RFC3339Nano should not be adjusted; got %v, want %v", got, want)
		}
	})

	t.Run("endOfDay false con dflt no lo modifica", func(t *testing.T) {
		got := ParseDateOrDefault("", dflt, true)
		if !got.Equal(dflt) {
			t.Errorf("got %v, want %v", got, dflt)
		}
	})
}
