package system

import (
	"context"
	"errors"
	"testing"

	bizsystem "kra/internal/biz/system"
	"kra/internal/service/dto"
)

type versionImportRepo struct {
	bizsystem.VersionRepo
	saveErr error
}

func (r *versionImportRepo) ImportVersionBundle(context.Context, *bizsystem.VersionBundle, *bizsystem.Version) error {
	return &bizsystem.VersionStageError{Stage: bizsystem.VersionStageSave, Err: r.saveErr}
}

func TestImportRequestReturnsVersionAuditSaveFailure(t *testing.T) {
	repo := &versionImportRepo{saveErr: errors.New("database unavailable")}
	service := NewVersionService(bizsystem.NewVersionUsecase(repo))
	request := &dto.ImportVersionRequest{}
	request.Version.Name = "release"
	request.Version.Code = "v1"
	err := service.ImportRequest(context.Background(), request)
	var stageErr *bizsystem.VersionStageError
	if !errors.As(err, &stageErr) || stageErr.Stage != bizsystem.VersionStageSave {
		t.Fatalf("ImportRequest() error = %v, want save stage error", err)
	}
}
