package job

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"
	"haixun-backend/internal/model/job/resume"

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

type stubJobUseCase struct {
	domusecase.UseCase
	template *entity.Template
}

func (s *stubJobUseCase) GetTemplate(context.Context, string) (*entity.Template, error) {
	return s.template, nil
}

func TestRunner_SkipsSucceededSteps(t *testing.T) {
	steps := []entity.StepProgress{
		{ID: "prepare", Status: enum.StepStatusSucceeded},
		{ID: "execute", Status: enum.StepStatusPending},
		{ID: "finalize", Status: enum.StepStatusPending},
	}
	template := &entity.Template{
		Type: "demo",
		Steps: []entity.TemplateStep{
			{ID: "prepare", WorkerType: "go"},
			{ID: "execute", WorkerType: "go"},
			{ID: "finalize", WorkerType: "go"},
		},
	}

	ran := make([]string, 0, 3)
	for _, step := range template.Steps {
		existing := resume.StepProgressByID(steps, step.ID)
		if existing != nil && resume.ShouldSkipStep(existing.Status) {
			continue
		}
		ran = append(ran, step.ID)
	}

	if len(ran) != 2 || ran[0] != "execute" || ran[1] != "finalize" {
		t.Fatalf("ran = %v, want [execute finalize]", ran)
	}
	_ = primitive.NewObjectID()
}
