package review_service

import (
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/moderation/v3/model"
)

/*
result.suggestion:

	block：包含敏感信息，不通过
	review：需要人工复检
	pass：不包含敏感信息，通过
*/
func (c *Config) CheckContent(text string) (*string, error) {
	request := &model.RunTextModerationRequest{}
	request.EnterpriseProjectId = &c.ProjectId
	eventType := "article"
	listCategoriesbody := []string{
		"terrorism", "porn", "ban", "abuse",
	}
	databody := model.TextDetectionDataReq{
		Text: text,
	}
	request.Body = &model.TextDetectionReq{
		Data:       &databody,
		EventType:  &eventType,
		Categories: &listCategoriesbody,
	}
	response, err := c.Client.RunTextModeration(request)
	if err != nil {
		res := "文本审核服务异常"
		return &res, err
	}
	result := response.Result
	suggestion := result.Suggestion

	return suggestion, nil
}

/*
result.suggestion:

	block：包含敏感信息，不通过
	review：需要人工复检
	pass：不包含敏感信息，通过
*/
func (c *Config) CheckImage(imageUrl string) (*string, error) {
	request := &model.CheckImageModerationRequest{}
	listCategoriesbody := []string{
		"terrorism", "porn",
	}
	eventType := "head_image"
	request.Body = &model.ImageDetectionReq{
		Url:        &imageUrl,
		Categories: &listCategoriesbody,
		EventType:  &eventType,
	}
	response, err := c.Client.CheckImageModeration(request)
	if err != nil {
		res := "图片审核服务异常"
		return &res, err
	}
	result := response.Result
	suggestion := result.Suggestion

	return suggestion, nil
}

func (c *Config) CheckVideo(videoUrl string) (*string, error) {
	request := &model.RunCreateVideoModerationJobRequest{}
	var listAudioCategoriesbody = []model.VideoCreateRequestAudioCategories{
		model.GetVideoCreateRequestAudioCategoriesEnum().PORN, model.GetVideoCreateRequestAudioCategoriesEnum().MOAN, model.GetVideoCreateRequestAudioCategoriesEnum().ABUSE,
	}
	var listImageCategoriesbody = []model.VideoCreateRequestImageCategories{
		model.GetVideoCreateRequestImageCategoriesEnum().PORN, model.GetVideoCreateRequestImageCategoriesEnum().TERRORISM,
	}
	var eventType = model.GetVideoCreateRequestEventTypeEnum().DEFAULT
	frameIntervalData := int32(2)
	databody := &model.VideoCreateRequestData{
		Url:           videoUrl,
		FrameInterval: &frameIntervalData, // 截帧频率间隔
	}
	//callbackVideoCreateRequest := "http://xxx.xx.xxxx"
	request.Body = &model.VideoCreateRequest{
		AudioCategories: &listAudioCategoriesbody, // 视频中音频需要检测的风险类型
		ImageCategories: &listImageCategoriesbody, // 视频中画面需要检测的风险类型
		EventType:       &eventType,
		Data:            databody,
		//Callback:        &callbackVideoCreateRequest,
	}
	response, err := c.Client.RunCreateVideoModerationJob(request)
	if err != nil {
		res := "视频审核服务异常"
		return &res, err
	}
	jobId := response.JobId

	return jobId, nil
}

/*
result.suggestion:

	block：包含敏感信息，不通过
	review：需要人工复检
	pass：不包含敏感信息，通过
*/
func (c *Config) CheckVideoInfo(jobId string) (status *string, suggestion *string, err error) {
	request := &model.RunQueryVideoModerationJobRequest{}
	request.JobId = jobId
	response, err := c.Client.RunQueryVideoModerationJob(request)
	if err != nil {
		return nil, nil, err
	}
	status1 := response.Status.Value()
	if "succeeded" == status1 {
		suggestion1 := response.Result.Suggestion.Value()
		return &status1, &suggestion1, nil
	} else {
		return &status1, nil, nil
	}
}

func (c *Config) CheckDocument(documentUrl, format string) (*string, error) {
	request := &model.RunCreateDocumentModerationJobRequest{}
	databody := &model.DocumentCreateRequestData{
		Url:    documentUrl,
		Format: format,
	}
	request.Body = &model.DocumentCreateRequest{
		EventType: "default",
		Data:      databody,
	}
	response, err := c.Client.RunCreateDocumentModerationJob(request)
	if err != nil {
		res := "文档审核服务异常"
		return &res, err
	}
	jobId := response.JobId

	return jobId, nil
}

/*
result.suggestion:

	block：包含敏感信息，不通过
	review：需要人工复检
	pass：不包含敏感信息，通过
*/
func (c *Config) CheckDocumentInfo(jobId string) (status *string, suggestion *string, err error) {
	request := &model.RunQueryDocumentModerationJobRequest{}
	request.JobId = jobId
	response, err := c.Client.RunQueryDocumentModerationJob(request)
	if err != nil {
		return nil, nil, err
	}
	status = response.Status
	if "succeeded" == *status {
		suggestion = response.Result.Suggestion
		return status, suggestion, nil
	} else {
		return status, nil, nil
	}
}
