package usecase

import (
	"context"
	"testing"

	"haixun-backend/internal/model/job/domain/entity"
	"haixun-backend/internal/model/job/domain/enum"
	domusecase "haixun-backend/internal/model/job/domain/usecase"
	jobrepo "haixun-backend/internal/model/job/repository"

	goredis "github.com/redis/go-redis/v9"
)

func TestRequestCancel_RunningSetsRedisCancelSignal(t *testing.T) {
	ctx := context.Background()
	client := goredis.NewClient(&goredis.Options{Addr: "127.0.0.1:6379"})
	if err := client.Ping(ctx).Err(); err != nil {
		t.Skip("redis not available:", err)
	}
	defer client.Close()

	jobID := "507f1f77bcf86cd799439011"
	template := demoTemplate()
	run := &entity.Run{
		TemplateType: template.Type,
		Status:       enum.RunStatusRunning,
		WorkerType:   string(enum.WorkerTypeGo),
	}

	uc := NewUseCase(
		&memoryTemplateRepo{template: template},
		newMemoryRunRepo(run),
		&memoryScheduleRepo{},
		&memoryEventRepo{},
		jobrepo.NewRedisQueueRepository(client),
	)

	updated, err := uc.RequestCancel(ctx, domusecase.CancelRunRequest{
		JobID:  jobID,
		Reason: "test cancel",
	})
	if err != nil {
		t.Fatalf("RequestCancel() error = %v", err)
	}
	if updated.Status != enum.RunStatusCancelRequested {
		t.Fatalf("status = %s, want cancel_requested", updated.Status)
	}

	value, err := client.Get(ctx, "jobs:cancel:"+jobID).Result()
	if err != nil {
		t.Fatalf("redis cancel key missing: %v", err)
	}
	if value != "test cancel" {
		t.Fatalf("cancel value = %q, want test cancel", value)
	}
	_ = client.Del(ctx, "jobs:cancel:"+jobID)
}
