package util

import (
	"testing"
	"time"

	basedomain "wucher/internal/domain/base"
)

func TestResolveShiftWindowUsesOperationalShiftWithinDateRange(t *testing.T) {
	base := &basedomain.Base{
		DefaultStartTimeType: basedomain.ShiftTimeTypeFixed,
		DefaultEndTimeType:   basedomain.ShiftTimeTypeFixed,
		DefaultShiftStart:    "09:00",
		DefaultShiftEnd:      "21:00",
		OperationalShiftTimes: []basedomain.BaseOperationalShiftTime{
			{
				DateStart:     timePtr(time.Date(2026, time.June, 1, 0, 0, 0, 0, time.UTC)),
				DateEnd:       timePtr(time.Date(2026, time.June, 30, 0, 0, 0, 0, time.UTC)),
				StartTimeType: basedomain.ShiftTimeTypeFixed,
				EndTimeType:   basedomain.ShiftTimeTypeFixed,
				ShiftStart:    "10:00",
				ShiftEnd:      "22:00",
			},
		},
	}

	start, end, err := ResolveShiftWindow(base, time.Date(2026, time.June, 15, 0, 0, 0, 0, time.UTC))
	if err != nil {
		t.Fatalf("ResolveShiftWindow returned error: %v", err)
	}
	if start != "10:00" || end != "22:00" {
		t.Fatalf("expected operational shift 10:00-22:00, got %s-%s", start, end)
	}

	start, end, err = ResolveShiftWindow(base, time.Date(2026, time.July, 1, 0, 0, 0, 0, time.UTC))
	if err != nil {
		t.Fatalf("ResolveShiftWindow returned error: %v", err)
	}
	if start != "09:00" || end != "21:00" {
		t.Fatalf("expected default shift 09:00-21:00, got %s-%s", start, end)
	}
}

func TestPickShiftForDatePreservesCalendarDateFromLocalTimezone(t *testing.T) {
	jakarta := time.FixedZone("UTC+7", 7*60*60)
	shift := basedomain.BaseOperationalShiftTime{
		DateStart:     timePtr(time.Date(2026, time.June, 1, 0, 0, 0, 0, jakarta)),
		DateEnd:       timePtr(time.Date(2026, time.June, 30, 0, 0, 0, 0, jakarta)),
		StartTimeType: basedomain.ShiftTimeTypeFixed,
		EndTimeType:   basedomain.ShiftTimeTypeFixed,
		ShiftStart:    "10:00",
		ShiftEnd:      "22:00",
	}
	base := &basedomain.Base{
		OperationalShiftTimes: []basedomain.BaseOperationalShiftTime{shift},
	}

	picked := PickShiftForDate(base, time.Date(2026, time.June, 1, 0, 0, 0, 0, time.UTC))
	if picked == nil {
		t.Fatalf("expected operational shift to match calendar date from local timezone")
	}
	if picked.ShiftStart != "10:00" || picked.ShiftEnd != "22:00" {
		t.Fatalf("unexpected picked shift %s-%s", picked.ShiftStart, picked.ShiftEnd)
	}
}

func timePtr(v time.Time) *time.Time {
	return &v
}
