package account

import (
	"context"
	"time"

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

// 删除标签
func TagRemove(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err error) {
	// parse authorization meta.
	AUTH, err := service.ParseMetaCtx(ctx, nil)
	if err != nil {
		return nil, err
	}

	// Validate request identity
	if in.Identity == "" {
		return nil, errcode.ErrInvalidArgument
	}

	// Delete the tag with proper error handling
	result := impl.DBService.Model(&models.PassportTags{}).
		Unscoped().
		Where("passport_id = ? AND identity = ?", AUTH.ID, in.Identity).
		Delete(&models.PassportTags{})

	if result.Error != nil {
		return nil, errcode.ErrDB
	}

	// Check if any record was actually deleted
	if result.RowsAffected == 0 {
		return nil, errcode.ErrNotFound(404, "Tag not found")
	}

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

}
