package user

import (
    "context"

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

type ItemCardWithTemplate struct {
    *model.UserItemCards
    Name             string `json:"name"`
    CardType         int32  `json:"card_type"`
    ScopeType        int32  `json:"scope_type"`
    EffectType       int32  `json:"effect_type"`
    StackingStrategy int32  `json:"stacking_strategy"`
    Remark           string `json:"remark"`
}

// ListUserItemCards 获取用户道具卡列表
// 功能描述：
//   - 查询指定用户的道具卡列表
//   - 支持分页查询，默认每页20条，最大100条
//   - 按创建时间倒序排列
//
// 参数说明：
//   - ctx: 上下文
//   - userID: 用户ID
//   - page: 页码，从1开始，小于1时自动设为1
//   - pageSize: 每页条数，小于1时设为20，大于100时设为100
//
// 返回说明：
//   - items: 道具卡列表
//   - total: 总记录数
//   - err: 错误信息
func (s *service) ListUserItemCards(ctx context.Context, userID int64, page, pageSize int) (items []*model.UserItemCards, total int64, err error) {
    q := s.readDB.UserItemCards.WithContext(ctx).ReadDB().Where(s.readDB.UserItemCards.UserID.Eq(userID))
    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.UserItemCards.ID.Desc()).Offset((page-1)*pageSize).Limit(pageSize).Find()
    if err != nil {
        return nil, 0, err
    }
    return items, total, nil
}

func (s *service) ListUserItemCardsWithTemplate(ctx context.Context, userID int64, page, pageSize int) (items []*ItemCardWithTemplate, total int64, err error) {
    q := s.readDB.UserItemCards.WithContext(ctx).ReadDB().Where(s.readDB.UserItemCards.UserID.Eq(userID))
    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
    }
    rows, err := q.Order(s.readDB.UserItemCards.ID.Desc()).Offset((page-1)*pageSize).Limit(pageSize).Find()
    if err != nil {
        return nil, 0, err
    }
    cidMap := make(map[int64]struct{})
    for _, r := range rows {
        if r.CardID > 0 {
            cidMap[r.CardID] = struct{}{}
        }
    }
    tpls := map[int64]*model.SystemItemCards{}
    if len(cidMap) > 0 {
        ids := make([]int64, 0, len(cidMap))
        for id := range cidMap {
            ids = append(ids, id)
        }
        list, err := s.readDB.SystemItemCards.WithContext(ctx).ReadDB().Where(s.readDB.SystemItemCards.ID.In(ids...)).Find()
        if err != nil {
            return nil, 0, err
        }
        for _, it := range list {
            tpls[it.ID] = it
        }
    }
    items = make([]*ItemCardWithTemplate, len(rows))
    for i, r := range rows {
        tpl := tpls[r.CardID]
        var name string
        var cardType, scopeType, effectType, stacking int32
        var remark string
        if tpl != nil {
            name = tpl.Name
            cardType = tpl.CardType
            scopeType = tpl.ScopeType
            effectType = tpl.EffectType
            stacking = tpl.StackingStrategy
            remark = tpl.Remark
        }
        items[i] = &ItemCardWithTemplate{
            UserItemCards:   r,
            Name:            name,
            CardType:        cardType,
            ScopeType:       scopeType,
            EffectType:      effectType,
            StackingStrategy: stacking,
            Remark:          remark,
        }
    }
    return items, total, nil
}

