package config

import (
	"testing"
	"time"
)

func TestMiningModeNormalized(t *testing.T) {
	c := RuntimeConfig{BuiltinConfig: BuiltinConfig{MiningMode: ""}}
	if c.MiningModeNormalized() != "always" {
		t.Fatal("empty -> always")
	}
	c.MiningMode = "  SCHEDULE  "
	if c.MiningModeNormalized() != "schedule" {
		t.Fatalf("got %q", c.MiningModeNormalized())
	}
}

func TestParseClockMinutes(t *testing.T) {
	if _, ok := parseClockMinutes(""); ok {
		t.Fatal("empty invalid")
	}
	if _, ok := parseClockMinutes("bad"); ok {
		t.Fatal("bad invalid")
	}
	m, ok := parseClockMinutes("09:30")
	if !ok || m != 9*60+30 {
		t.Fatalf("09:30 = %d ok=%v", m, ok)
	}
	m, ok = parseClockMinutes("23:59:59")
	if !ok || m != 23*60+59 {
		t.Fatalf("23:59:59 = %d", m)
	}
}

func TestInScheduleWindowInvalidSchedule(t *testing.T) {
	c := RuntimeConfig{BuiltinConfig: BuiltinConfig{ScheduleStart: "bad", ScheduleEnd: "10:00"}}
	if !c.InScheduleWindow(time.Now()) {
		t.Fatal("invalid schedule should allow mining (true)")
	}
}

func TestInScheduleWindowSameStartEnd(t *testing.T) {
	c := RuntimeConfig{BuiltinConfig: BuiltinConfig{ScheduleStart: "08:00", ScheduleEnd: "08:00"}}
	if !c.InScheduleWindow(time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC)) {
		t.Fatal("same start/end = always in window")
	}
}

func TestInScheduleWindowDaytime(t *testing.T) {
	c := RuntimeConfig{BuiltinConfig: BuiltinConfig{ScheduleStart: "09:00", ScheduleEnd: "17:00"}}
	inside := time.Date(2026, 1, 1, 10, 0, 0, 0, time.UTC)
	outside := time.Date(2026, 1, 1, 20, 0, 0, 0, time.UTC)
	if !c.InScheduleWindow(inside) {
		t.Fatal("10:00 should be inside 09-17")
	}
	if c.InScheduleWindow(outside) {
		t.Fatal("20:00 should be outside 09-17")
	}
}

func TestInScheduleWindowOvernight(t *testing.T) {
	c := RuntimeConfig{BuiltinConfig: BuiltinConfig{ScheduleStart: "22:00", ScheduleEnd: "06:00"}}
	late := time.Date(2026, 1, 1, 23, 0, 0, 0, time.UTC)
	mid := time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC)
	early := time.Date(2026, 1, 1, 3, 0, 0, 0, time.UTC)
	if !c.InScheduleWindow(late) {
		t.Fatal("23:00 in overnight window")
	}
	if c.InScheduleWindow(mid) {
		t.Fatal("12:00 outside overnight window")
	}
	if !c.InScheduleWindow(early) {
		t.Fatal("03:00 in overnight window")
	}
}
