package core

import (
	"testing"
	"time"
)

func TestCronMatch(t *testing.T) {
	t.Run("9:00 AM coincide con 0 9 * * *", func(t *testing.T) {
		sched, err := ParseCronExpr("0 9 * * *")
		if err != nil {
			t.Fatalf("ParseCronExpr: %v", err)
		}
		ts := time.Date(2026, 4, 11, 9, 0, 0, 0, time.UTC)
		if !CronMatch(sched, ts) {
			t.Errorf("expected match for 9:00 AM with '0 9 * * *'")
		}
	})

	t.Run("9:15 AM NO coincide con 0 9 * * *", func(t *testing.T) {
		sched, err := ParseCronExpr("0 9 * * *")
		if err != nil {
			t.Fatalf("ParseCronExpr: %v", err)
		}
		ts := time.Date(2026, 4, 11, 9, 15, 0, 0, time.UTC)
		if CronMatch(sched, ts) {
			t.Errorf("expected no match for 9:15 AM with '0 9 * * *'")
		}
	})

	t.Run("lunes a las 9 coincide con 0 9 * * 1", func(t *testing.T) {
		sched, err := ParseCronExpr("0 9 * * 1")
		if err != nil {
			t.Fatalf("ParseCronExpr: %v", err)
		}
		// 2026-04-13 is a Monday
		ts := time.Date(2026, 4, 13, 9, 0, 0, 0, time.UTC)
		if !CronMatch(sched, ts) {
			t.Errorf("expected match for Monday 9:00 with '0 9 * * 1'")
		}
	})

	t.Run("domingo a las 9 NO coincide con 0 9 * * 1", func(t *testing.T) {
		sched, err := ParseCronExpr("0 9 * * 1")
		if err != nil {
			t.Fatalf("ParseCronExpr: %v", err)
		}
		// 2026-04-12 is a Sunday (weekday 0)
		ts := time.Date(2026, 4, 12, 9, 0, 0, 0, time.UTC)
		if CronMatch(sched, ts) {
			t.Errorf("expected no match for Sunday 9:00 with '0 9 * * 1'")
		}
	})

	t.Run("wildcard * coincide con cualquier valor", func(t *testing.T) {
		sched, err := ParseCronExpr("*/15 * * * *")
		if err != nil {
			t.Fatalf("ParseCronExpr: %v", err)
		}
		// 9:30 should match */15 (0,15,30,45)
		ts := time.Date(2026, 4, 11, 9, 30, 0, 0, time.UTC)
		if !CronMatch(sched, ts) {
			t.Errorf("expected match for 9:30 with '*/15 * * * *'")
		}
		// 9:07 should NOT match */15
		ts2 := time.Date(2026, 4, 11, 9, 7, 0, 0, time.UTC)
		if CronMatch(sched, ts2) {
			t.Errorf("expected no match for 9:07 with '*/15 * * * *'")
		}
	})

	t.Run("specific month", func(t *testing.T) {
		sched, err := ParseCronExpr("0 0 1 4 *")
		if err != nil {
			t.Fatalf("ParseCronExpr: %v", err)
		}
		// April 1st matches
		ts := time.Date(2026, 4, 1, 0, 0, 0, 0, time.UTC)
		if !CronMatch(sched, ts) {
			t.Errorf("expected match for April 1 with '0 0 1 4 *'")
		}
		// March 1st does NOT match
		ts2 := time.Date(2026, 3, 1, 0, 0, 0, 0, time.UTC)
		if CronMatch(sched, ts2) {
			t.Errorf("expected no match for March 1 with '0 0 1 4 *'")
		}
	})
}
