package activity

import (
    "context"
    "time"

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

// ListActivities 查询活动列表（支持名称、分类、Boss、状态过滤与分页）
// 参数: in 列表查询输入
// 返回: 活动集合、总数与错误
func (s *service) ListActivities(ctx context.Context, in ListActivitiesInput) (items []*model.Activities, total int64, err error) {
	q := s.readDB.Activities.WithContext(ctx).ReadDB()

	if in.Name != "" {
		q = q.Where(s.readDB.Activities.Name.Like("%" + in.Name + "%"))
	}
	if in.CategoryID != 0 {
		q = q.Where(s.readDB.Activities.ActivityCategoryID.Eq(in.CategoryID))
	}
	if in.IsBoss != nil {
		q = q.Where(s.readDB.Activities.IsBoss.Eq(*in.IsBoss))
	}
    if in.Status != nil {
        q = q.Where(s.readDB.Activities.Status.Eq(*in.Status))
    }

    now := time.Now()
    q = q.Where(s.readDB.Activities.EndTime.Gt(now))

	total, err = q.Count()
	if err != nil {
		return nil, 0, err
	}

	if in.Page <= 0 {
		in.Page = 1
	}
	if in.PageSize <= 0 {
		in.PageSize = 20
	}
	if in.PageSize > 100 {
		in.PageSize = 100
	}

	items, err = q.Order(s.readDB.Activities.ID.Desc()).Offset((in.Page - 1) * in.PageSize).Limit(in.PageSize).Find()
	if err != nil {
		return nil, 0, err
	}
	return items, total, nil
}
