package bookmark

import (
	"context"
	"time"

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

// 获取书签详情
func GetBookmark(ctx context.Context, in *pb.IDRequest) (reply *pb.CloudBookmarkItem, err error) {
	// parse authorization meta.
	auth, err := service.ParseMetaCtx(ctx, nil)
	if err != nil {
		return nil, err
	}

	// validate request id
	if in.Id == 0 {
		return nil, errcode.ErrInvalidArgument
	}

	// logic code
	var bookmark models.CloudBookmark
	query := impl.DBService.Where("passport_id = ? AND id = ?", auth.ID, in.Id)

	if err := query.First(&bookmark).Error; err != nil {
		printer.Error("Bookmark not found: %v", err)
		return nil, errcode.ErrInvalidArgument
	}

	reply = &pb.CloudBookmarkItem{
		Id:          uint64(bookmark.ID),
		Identity:    bookmark.Identity,
		Title:       bookmark.Title,
		Url:         bookmark.URL,
		Description: bookmark.Description,
		Category:    bookmark.Category,
		Tags:        bookmark.Tags,
		Icon:        bookmark.Icon,
		IsPrivate:   bookmark.IsPrivate,
		CreatedAt:   bookmark.CreatedAt.Format(time.RFC3339),
		UpdatedAt:   bookmark.UpdatedAt.Format(time.RFC3339),
	}

	return reply, nil
}
