package audit

import (
	"context"
	"encoding/json"
	"net/http"

	"repository.basistech.ru/BASIS/dynamix-golang-sdk/v14/internal/validators"
)

// LinkedJobsRequest struct to get information about jobs linked with Audit
type LinkedJobsRequest struct {
	// Audit GUID
	// Required: true
	AuditGuid string `url:"audit_guid" json:"audit_guid" validate:"required"`
}

// LinkedJobs gets information about Linked Jobs as a ListLinkedJobs struct
func (a Audit) LinkedJobs(ctx context.Context, req LinkedJobsRequest) (*ListLinkedJobs, error) {
	res, err := a.GetRawLinkedJobs(ctx, req)
	if err != nil {
		return nil, err
	}

	info := ListLinkedJobs{}

	err = json.Unmarshal(res, &info)
	if err != nil {
		return nil, err
	}

	return &info, nil
}

// GetRawLinkedJobs gets information about Linked Jobs as an array of bytes
func (a Audit) GetRawLinkedJobs(ctx context.Context, req LinkedJobsRequest) ([]byte, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/audit/linkedJobs"

	res, err := a.client.DecortApiCall(ctx, http.MethodPost, url, req)
	return res, err
}
