package login_record

import (
	"context"
	"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/loginrecord"
	"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/user"
	"git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/utils/hander"

	"git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/svc"
	"git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core"
)

type GetRecordLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetRecordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetRecordLogic {
	return &GetRecordLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// Login Record management
func (l *GetRecordLogic) GetRecord(in *core.RecordReq) (*core.LoginRecordInfo, error) {
	dbData, err := l.svcCtx.DB.LoginRecord.Query().
		Where(loginrecord.HasUserWith(user.IDEQ(in.UserId))).
		First(l.ctx)
	if err != nil {
		return nil, hander.HandleEntErr(err, in)
	}
	return l.svcCtx.Convert.ConvertLoginRecordRpc(l.ctx, dbData), nil
}
