package album

import (
	"context"
	"strings"
	"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"
	"git.apinb.com/bsm-sdk/core/types"
	"git.apinb.com/bsm-sdk/core/utils"
)

// 创建相册
func CreateAlbum(ctx context.Context, in *pb.CreateAlbumRequest) (reply *pb.StatusReply, err error) {
	// parse authorization meta.
	auth, err := service.ParseMetaCtx(ctx, nil)
	if err != nil {
		return nil, err
	}

	// valid code
	if strings.TrimSpace(in.Name) == "" {
		return nil, errcode.ErrInvalidArgument
	}
	if strings.TrimSpace(in.CloudIdentity) == "" {
		return nil, errcode.ErrInvalidArgument
	}

	// logic code
	record := models.CloudAlbum{
		Std_IICUDS: types.Std_IICUDS{
			Identity: utils.UUID(),
		},
		Std_Passport: types.Std_Passport{
			PassportID:       auth.ID,
			PassportIdentity: auth.Identity,
		},
		CloudBase: models.CloudBase{
			CloudID:       uint(in.CloudId),
			CloudIdentity: in.CloudIdentity,
		},
		Name:        in.Name,
		Description: in.Description,
		CoverPhoto:  in.CoverPhoto,
		IsPrivate:   in.IsPrivate,
	}

	if err := impl.DBService.Create(&record).Error; err != nil {
		printer.Error("Create album error: %v", err)
		return nil, errcode.ErrDB
	}

	return &pb.StatusReply{
		Details: record.Identity,
		Timeseq: time.Now().UnixMilli(),
	}, nil
}
