package usecase

import (
	"testing"

	"haixun-backend/internal/library/clock"
	"haixun-backend/internal/model/job/domain/entity"

	"go.mongodb.org/mongo-driver/bson/primitive"
)

func TestBuildScheduleRunPayload_InjectsTimezoneMetadata(t *testing.T) {
	triggeredAt := clock.NowUnixNano()
	schedule := &entity.Schedule{
		ID:       primitive.NewObjectID(),
		Timezone: "Asia/Taipei",
		Cron:     "0 9 * * *",
		PayloadTemplate: map[string]any{
			"target": "demo",
		},
	}

	payload := buildScheduleRunPayload(schedule, triggeredAt)
	if payload["target"] != "demo" {
		t.Fatalf("target = %v, want demo", payload["target"])
	}
	meta, ok := payload[schedulePayloadKey].(map[string]any)
	if !ok {
		t.Fatal("expected _schedule metadata")
	}
	if meta["timezone"] != "Asia/Taipei" {
		t.Fatalf("timezone = %v", meta["timezone"])
	}
	if meta["storage_tz"] != clock.StorageTimezone {
		t.Fatalf("storage_tz = %v", meta["storage_tz"])
	}
	if meta["triggered_at"] != triggeredAt {
		t.Fatalf("triggered_at = %v", meta["triggered_at"])
	}
}
