package job

import (
	"context"

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

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

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

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