package user

import (
	"context"
	"strings"

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

// OrderWithItems 包含订单项的订单信息
type OrderWithItems struct {
	*model.Orders
	Items []*model.OrderItems `json:"items"`
}

func (s *service) ListOrders(ctx context.Context, userID int64, page, pageSize int) (items []*model.Orders, total int64, err error) {
	// 查询用户的所有订单，包括商城直购(1)、抽奖票据(2)和系统发放(3)
	q := s.readDB.Orders.WithContext(ctx).ReadDB().Where(s.readDB.Orders.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.Orders.ID.Desc()).Offset((page - 1) * pageSize).Limit(pageSize).Find()
	if err != nil {
		return nil, 0, err
	}
	return items, total, nil
}

// ListOrdersWithItems 查询用户的订单列表，包含订单项详情
func (s *service) ListOrdersWithItems(ctx context.Context, userID int64, page, pageSize int) (items []*OrderWithItems, total int64, err error) {
	// 查询用户的所有订单，包括商城直购(1)、抽奖票据(2)和系统发放(3)
	q := s.readDB.Orders.WithContext(ctx).ReadDB().Where(s.readDB.Orders.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
	}

	// 查询订单列表
	orders, err := q.Order(s.readDB.Orders.ID.Desc()).Offset((page - 1) * pageSize).Limit(pageSize).Find()
	if err != nil {
		return nil, 0, err
	}

	// 构建订单ID列表
	orderIDs := make([]int64, len(orders))
	for i, order := range orders {
		orderIDs[i] = order.ID
	}

	var allItems []*model.OrderItems
	if len(orderIDs) > 0 {
		allItems, err = s.readDB.OrderItems.WithContext(ctx).ReadDB().Where(s.readDB.OrderItems.OrderID.In(orderIDs...)).Find()
		if err != nil {
			return nil, 0, err
		}

		ids := make(map[int64]struct{})
		for _, it := range allItems {
			if strings.TrimSpace(it.Title) == "" || strings.TrimSpace(it.Title) == "系统发放奖励" || it.ProductImages == "" || it.ProductImages == "[]" {
				ids[it.ProductID] = struct{}{}
			}
		}
		if len(ids) > 0 {
			pidList := make([]int64, 0, len(ids))
			for id := range ids {
				pidList = append(pidList, id)
			}
			pros, err2 := s.readDB.Products.WithContext(ctx).ReadDB().Where(s.readDB.Products.ID.In(pidList...)).Find()
			if err2 != nil {
				return nil, 0, err2
			}
			pm := make(map[int64]*model.Products, len(pros))
			for _, p := range pros {
				pm[p.ID] = p
			}
			for _, it := range allItems {
				p := pm[it.ProductID]
				if p == nil {
					continue
				}
				if strings.TrimSpace(it.Title) == "" || strings.TrimSpace(it.Title) == "系统发放奖励" {
					it.Title = p.Name
				}
				if it.ProductImages == "" || it.ProductImages == "[]" {
					it.ProductImages = p.ImagesJSON
				}
			}
		}
	}

	// 构建订单ID到订单项的映射
	itemsMap := make(map[int64][]*model.OrderItems)
	for _, item := range allItems {
		itemsMap[item.OrderID] = append(itemsMap[item.OrderID], item)
	}

	// 构建返回结果
	items = make([]*OrderWithItems, len(orders))
	for i, order := range orders {
		items[i] = &OrderWithItems{
			Orders: order,
			Items:  itemsMap[order.ID],
		}
	}

	return items, total, nil
}
