package activity

import (
	"context"

	"bindbox-game/internal/repository/mysql/model"
)

// ListDrawLogs 查询抽奖记录列表（支持分页）
// 参数: issueID 期ID, page/pageSize 分页参数, level 等级过滤(可选)
// 返回: 抽奖记录集合、总数与错误
func (s *service) ListDrawLogs(ctx context.Context, issueID int64, page, pageSize int, level *int32) (items []*model.ActivityDrawLogs, total int64, err error) {
	q := s.readDB.ActivityDrawLogs.WithContext(ctx).ReadDB().Where(s.readDB.ActivityDrawLogs.IssueID.Eq(issueID))
	if level != nil {
		q = q.Where(s.readDB.ActivityDrawLogs.Level.Eq(*level))
	}
	total, err = q.Count()
	if err != nil {
		return nil, 0, err
	}
	if page <= 0 {
		page = 1
	}
	if pageSize <= 0 {
		pageSize = 20
	}
	if pageSize > 100 {
		pageSize = 100
	}
	items, err = q.Order(s.readDB.ActivityDrawLogs.ID.Desc()).Offset((page - 1) * pageSize).Limit(pageSize).Find()
	if err != nil {
		return nil, 0, err
	}
	return items, total, nil
}
