package imports

import (
	"context"
	"fmt"
	"fonchain-fiee/api/accountFiee"
	apiCast "fonchain-fiee/api/cast"
	"fonchain-fiee/pkg/service"
	"strconv"
)

func publishImage(req publishImageReq) (err error) {
	var infoResp *accountFiee.UserInfoResponse
	list, err := service.AccountFieeProvider.UserList(context.Background(), &accountFiee.UserListRequest{
		Name:   req.ArtistName,
		SubNum: req.SubNum,
	})
	if err != nil {
		return fmt.Errorf("获取用户信息失败: %s", err.Error())
	}
	if len(list.UserList) == 0 {
		return fmt.Errorf("未找到用户信息: %s", req.ArtistName)
	}
	if len(list.UserList) > 0 {
		infoResp, err = service.AccountFieeProvider.Info(context.Background(), &accountFiee.InfoRequest{
			ID:     list.UserList[0].Id,
			Domain: "app",
		})
		if err != nil {
			return fmt.Errorf("获取用户信息失败: %s", err.Error())
		}
	}

	//-----------------------------------------------------获取自媒体账号
	//accountList, err := service.CastProvider.MediaUserList(context.Background(), &apiCast.MediaUserListReq{
	//	ArtistUuid: strconv.FormatUint(list.UserList[0].Id, 10),
	//	ArtistVal:  req.ArtistName,
	//	Page:       1,
	//	PageSize:   10,
	//})
	//if err != nil {
	//	return fmt.Errorf("自媒体账号数量获取失败: %s,账号数量:%d", err.Error(), len(accountList.Data))
	//}
	//if accountList == nil || len(accountList.Data) == 0 {
	//	return fmt.Errorf("自媒体账号数量为0")
	//}
	//var mediaAccountUuids []string
	//var mediaAccountNames []string
	//platformIDs 1 tiktok 2youtube 3ins
	//platformIDs := []apiCast.PlatformIDENUM{}
	//for _, info := range accountList.Data {
	//	if info.ArtistName == req.TikTok || info.ArtistName == req.Instagram {
	//		mediaAccountUuids = append(mediaAccountUuids, info.MediaAccountUuid)
	//		mediaAccountNames = append(mediaAccountNames, info.PlatformUserName)
	//		platformIDs = append(platformIDs, apiCast.PlatformIDENUM(info.PlatformID))
	//	}
	//}

	//---------------------------------------------------发布
	_, err = service.CastProvider.UpdateWorkImage(context.Background(), &apiCast.UpdateWorkImageReq{
		ArtistSubNum:      list.UserList[0].SubNum,
		Title:             req.Title,
		Content:           req.Content,
		Images:            req.GeneratePhotoUrl,
		MediaAccountUuids: req.MediaAccountUuids,
		MediaAccountNames: req.MediaAccountNames,
		PlatformIDs:       []apiCast.PlatformIDENUM{1, 3},
		PublishConfig1: &apiCast.PublishConfig{
			CanComment:    1,
			CanJoin:       1,
			CanQuote:      1,
			ForbidComment: 1,
			IsAI:          1,
			PublicType:    1,
		},
		PublishConfig2: &apiCast.PublishConfig{
			CanComment:    1,
			CanJoin:       1,
			CanQuote:      1,
			ForbidComment: 1,
			IsAI:          1,
			PublicType:    1,
		},
		PublishConfig3: &apiCast.PublishConfig{
			CanComment:    1,
			CanJoin:       1,
			CanQuote:      1,
			ForbidComment: 1,
			IsAI:          1,
			PublicType:    1,
		},
		PublishConfig4:      nil,
		Action:              "submit",
		ArtistPhone:         infoResp.TelNum,
		ArtistUuid:          strconv.FormatUint(list.UserList[0].Id, 10),
		ArtistName:          infoResp.Name,
		ArtistPhoneAreaCode: infoResp.TelAreaCode,
		WorkUuid:            "",
		Source:              2,
		LineNo:              0,
		Remark:              "",
		Success:             false,
	})
	if err != nil {
		return fmt.Errorf("发布"+req.ArtistName+"图文"+"失败: %s", err.Error())
	}
	return nil
}
