package job

import (
	"context"

	"haixun-backend/internal/svc"
	"haixun-backend/internal/types"
)

type RetryJobLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewRetryJobLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RetryJobLogic {
	return &RetryJobLogic{ctx: ctx, svcCtx: svcCtx}
}

func (l *RetryJobLogic) RetryJob(req *types.JobIDPath) (*types.JobData, error) {
	tenantID, uid, err := actorFrom(l.ctx)
	if err != nil {
		return nil, err
	}
	existing, err := l.svcCtx.Job.GetRun(l.ctx, req.ID)
	if err != nil {
		return nil, err
	}
	if err := ensureRunAccess(existing, tenantID, uid); err != nil {
		return nil, err
	}
	run, err := l.svcCtx.Job.RetryRun(l.ctx, req.ID)
	if err != nil {
		return nil, err
	}
	data := toJobData(run)
	return &data, nil
}
