package grpc

import (
	"testing"
	"time"

	domain "hyapp/services/activity-service/internal/domain/activitytemplate"
)

func TestActivityTemplateRuntimeResponseFillsActualLifecycle(t *testing.T) {
	nowMS := time.Now().UTC().UnixMilli()
	tests := []struct {
		name    string
		runtime domain.Runtime
		want    string
	}{
		{
			name: "scheduled",
			runtime: domain.Runtime{
				Template:  domain.Template{TemplateID: "scheduled", Status: domain.StatusPublished, StartMS: nowMS + 60_000, EndMS: nowMS + 120_000},
				VersionNo: 1, RuntimeFromMS: nowMS + 60_000,
			},
			want: domain.LifecycleScheduled,
		},
		{
			name: "ongoing",
			runtime: domain.Runtime{
				Template:  domain.Template{TemplateID: "ongoing", Status: domain.StatusPublished, StartMS: nowMS - 60_000, EndMS: nowMS + 60_000},
				VersionNo: 2, RuntimeFromMS: nowMS - 60_000,
			},
			want: domain.LifecycleOngoing,
		},
		{
			name: "ended",
			runtime: domain.Runtime{
				Template:  domain.Template{TemplateID: "ended", Status: domain.StatusPublished, StartMS: nowMS - 120_000, EndMS: nowMS - 60_000},
				VersionNo: 3, RuntimeFromMS: nowMS - 120_000,
			},
			want: domain.LifecycleEnded,
		},
		{
			name: "disabled",
			runtime: domain.Runtime{
				Template:  domain.Template{TemplateID: "disabled", Status: domain.StatusPublished, StartMS: nowMS - 120_000, EndMS: nowMS + 120_000},
				VersionNo: 4, RuntimeFromMS: nowMS - 120_000, RuntimeToMS: nowMS - 60_000,
			},
			want: domain.LifecycleDisabled,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := activityTemplateRuntimeToProto(tt.runtime)
			if got == nil || got.GetTemplate().GetLifecycleStatus() != tt.want {
				t.Fatalf("runtime response lifecycle = %+v, want %q", got, tt.want)
			}
			if got.GetRuntimeFromMs() != tt.runtime.RuntimeFromMS || got.GetRuntimeToMs() != tt.runtime.RuntimeToMS || got.GetServerTimeMs() <= 0 {
				t.Fatalf("runtime response window/server time = %+v", got)
			}
		})
	}
}
