package core

import (
	"reflect"
	"testing"
)

func TestParseCronExpr(t *testing.T) {
	t.Run("*/15 expande minutos a [0 15 30 45]", func(t *testing.T) {
		sched, err := ParseCronExpr("*/15 * * * *")
		if err != nil {
			t.Fatalf("unexpected error: %v", err)
		}
		want := []int{0, 15, 30, 45}
		if !reflect.DeepEqual(sched.Minute, want) {
			t.Errorf("Minute = %v, want %v", sched.Minute, want)
		}
		// Hour should be all 24 hours
		if len(sched.Hour) != 24 {
			t.Errorf("Hour len = %d, want 24", len(sched.Hour))
		}
	})

	t.Run("@daily resuelve a 0 0 en todos los campos restantes", func(t *testing.T) {
		sched, err := ParseCronExpr("@daily")
		if err != nil {
			t.Fatalf("unexpected error: %v", err)
		}
		if !reflect.DeepEqual(sched.Minute, []int{0}) {
			t.Errorf("Minute = %v, want [0]", sched.Minute)
		}
		if !reflect.DeepEqual(sched.Hour, []int{0}) {
			t.Errorf("Hour = %v, want [0]", sched.Hour)
		}
		// DayOfMonth should be all days
		if len(sched.DayOfMonth) != 31 {
			t.Errorf("DayOfMonth len = %d, want 31", len(sched.DayOfMonth))
		}
	})

	t.Run("0 9 1,15 * * expande dias a [1 15]", func(t *testing.T) {
		sched, err := ParseCronExpr("0 9 1,15 * *")
		if err != nil {
			t.Fatalf("unexpected error: %v", err)
		}
		if !reflect.DeepEqual(sched.Minute, []int{0}) {
			t.Errorf("Minute = %v, want [0]", sched.Minute)
		}
		if !reflect.DeepEqual(sched.Hour, []int{9}) {
			t.Errorf("Hour = %v, want [9]", sched.Hour)
		}
		if !reflect.DeepEqual(sched.DayOfMonth, []int{1, 15}) {
			t.Errorf("DayOfMonth = %v, want [1, 15]", sched.DayOfMonth)
		}
	})

	t.Run("0 9 * * 1-5 expande dia de semana a [1 2 3 4 5]", func(t *testing.T) {
		sched, err := ParseCronExpr("0 9 * * 1-5")
		if err != nil {
			t.Fatalf("unexpected error: %v", err)
		}
		want := []int{1, 2, 3, 4, 5}
		if !reflect.DeepEqual(sched.DayOfWeek, want) {
			t.Errorf("DayOfWeek = %v, want %v", sched.DayOfWeek, want)
		}
	})

	t.Run("expresion con 4 campos retorna error", func(t *testing.T) {
		_, err := ParseCronExpr("0 9 * *")
		if err == nil {
			t.Error("expected error for 4-field expression, got nil")
		}
	})

	t.Run("minuto fuera de rango retorna error", func(t *testing.T) {
		_, err := ParseCronExpr("60 * * * *")
		if err == nil {
			t.Error("expected error for minute=60, got nil")
		}
	})
}
