package models

import (
	"fmt"

	"bsm/full/module/social/feed/internal/impl"
	"gorm.io/gorm"
)

func InitData() error {
	return nil
}

func CreatePost(data *FeedPost) error {
	err := impl.DBService.Transaction(func(tx *gorm.DB) error {
		if err := tx.Create(&data).Error; err != nil {
			return err
		}
		if err := tx.Create(&data.Attachs).Error; err != nil {
			return err
		}
		if err := tx.Create(&data.Tags).Error; err != nil {
			return err
		}
		return nil
	})

	return err
}
func ChargePost(data *FeedPost) error {
	err := impl.DBService.Where("identity = ?", data.Identity).Updates(&data).Error
	return err
}
func DeletePost(identity string) error {
	err := impl.DBService.Where("identity = ?", identity).Delete(&FeedPost{}).Error
	return err
}

// 点赞/踩操作
func LikeAction(action_op, action_type, identity string) (err error) {
	var column string
	tx := impl.DBService
	if action_type == "post" {
		tx = tx.Model(&FeedPost{})
	} else if action_type == "comment" {
		tx = tx.Model(&FeedComment{})
	}
	if action_op == "ilike" {
		column = "cnt_like"
	} else if action_type == "unlike" {
		column = "cnt_unlike"
	}
	if err = tx.Where("identity = ?", identity).UpdateColumn(column, gorm.Expr(fmt.Sprintf("%s + ?", column), 1)).Error; err != nil {
		return err
	}
	return
}

// PostList
func PostList(page, size int64, tag string) (list []*FeedPost, cnt int64, err error) {
	tx := impl.DBService.Order("created_at desc")
	if tag != "" {
		tx = tx.Joins("join feed_relate_tags on feed_relate_tags.post_identity = feed_post.identity", impl.DBService.Where(&FeedRelateTags{Key: tag}))
		tx = tx.Where("key = ?", tag)
	}

	tx.Limit(int(page)).Offset(int((page - 1) * size))
	err = tx.Find(&list).Count(&cnt).Error
	fmt.Print("posts", list)
	return
}

func AddTags(data *[]FeedTags) (err error) {
	err = impl.DBService.Create(data).Error
	if err != nil {
		return err
	}
	return err
}
func ModifyTags(data *FeedTags) (err error) {
	err = impl.DBService.Where("identity = ?", data.Key).Save(data).Error
	if err != nil {
		return err
	}
	return err
}

func DeleteTags(key string) (err error) {
	err = impl.DBService.Where("identity = ?", key).Delete(&FeedTags{}).Error
	if err != nil {
		return err
	}
	return err
}

func TagsList() (list []*FeedTags, cnt int64, err error) {
	tags := make([]*FeedTags, 0)
	tx := impl.DBService.Order("created_at desc")
	err = tx.Find(&tags).Count(&cnt).Error
	return tags, cnt, err
}

func AddComment(comment *FeedComment, authorIdentity string) (err error) {
	err = impl.DBService.Transaction(func(tx *gorm.DB) error {
		// 添加评论,更新文章评论量
		err := tx.Create(comment).Table("feed_post").Where("identity = ?", comment.PostIdentity).UpdateColumn("cnt_comment", gorm.Expr("cnt_comment + ?", 1)).Error
		if err != nil {
			return err
		}
		if comment.ParentIdentity != "" {
			//更新被评论的评论数
			err = tx.Where("identity = ?", comment.ParentIdentity).UpdateColumn("cnt_comment", gorm.Expr("cnt_comment + ?", 1)).Error
			if err != nil {
				return err
			}
		}
		return nil
	})
	return
}

func DeleteComment(identity string) (err error) {

	return
}
