package post

import (
	"context"
	"time"

	"bsm/full/module/base/cms/internal/models"
	pb "bsm/full/module/base/cms/pb"
	"git.apinb.com/bsm-sdk/core/errcode"
	"git.apinb.com/bsm-sdk/core/printer"
)

// 文章列表
func Fetch(ctx context.Context, in *pb.PostListRequest) (reply *pb.PostListReply, err error) {
	if in.GetPage() <= 0 {
		in.Page = 1
	}
	if in.GetSize() <= 0 {
		in.Size = 10
	}
	if in.GetSize() > 50 {
		in.Size = 50
	}
	list, cnt, err := models.PostList(in.Page, in.Size, in.CategoryIdentity, in.Keyword, int64(in.Type))
	if err != nil {
		printer.Error(err.Error())
		return nil, errcode.ErrDB
	}
	var res = pb.PostListReply{
		Count: cnt,
		Data:  make([]*pb.PostItem, 0),
	}
	for _, val := range list {
		var (
			accessoryData = make([]*pb.AccessoryItem, 0)
			accessories   = make([]string, 0)

			categoryData = make([]*pb.CategoryItem, 0)
			categories   = make([]string, 0)

			TagsData          = make([]*pb.TagsItem, 0)
			tagsIdentityArray = make([]string, 0)
		)
		for _, accessory := range val.Accessories {
			accessoryData = append(accessoryData, &pb.AccessoryItem{
				Identity: accessory.Identity,
				Title:    accessory.Title,
				FilePath: accessory.FilePath,
			})
			accessories = append(accessories, accessory.Identity)
		}
		for _, category := range val.Categories {
			categoryData = append(categoryData, &pb.CategoryItem{
				Id:        int64(category.Category.ID),
				Identity:  category.Category.Identity,
				ParentId:  int64(category.Category.ParentId),
				Title:     category.Category.Title,
				CoverPath: category.Category.CoverPath,
				Intro:     category.Category.Intro,
			})
			categories = append(categories, category.CategoryIdentity)
		}
		for _, tags := range val.Tags {
			TagsData = append(TagsData, &pb.TagsItem{
				Id:        int64(tags.Tags.ID),
				Identity:  tags.TagsIdentity,
				Title:     tags.Tags.Title,
				Intro:     tags.Tags.Intro,
				CoverPath: tags.Tags.CoverPath,
			})
			tagsIdentityArray = append(tagsIdentityArray, tags.TagsIdentity)
		}

		res.Data = append(res.Data, &pb.PostItem{
			Identity:               val.Identity,
			OwnerId:                int64(val.OwnerID),
			OwnerIdentity:          val.OwnerIdentity,
			Title:                  val.Title,
			CoverPath:              val.CoverPath,
			Author:                 val.Author,
			AuthorIdentity:         val.AuthorIdentity,
			Content:                val.Content,
			TargetUrl:              val.TargetUrl,
			SourceUrl:              val.SourceUrl,
			Hits:                   val.Hits,
			HasAccessory:           val.HasAccessory,
			CreatedAt:              val.CreatedAt.Format(time.DateTime),
			UpdatedAt:              val.UpdatedAt.Format(time.DateTime),
			Description:            val.Description,
			LikeHits:               val.LikeHits,
			UnlikeHits:             val.UnlikeHits,
			CommentHits:            val.CommentHits,
			PostType:               val.Types,
			Key:                    val.Hash,
			Hash:                   val.Hash,
			AccessoryData:          accessoryData,
			AccessoryIdentityArray: accessories,
			CategoryData:           categoryData,
			CategoryIdentityArray:  categories,
			TagsData:               TagsData,
			TagsIdentityArray:      tagsIdentityArray,
		})

	}

	return &res, nil
}
