// Recent player-vendor sales for a set of game accounts. Shared by the player
// self endpoint (the caller's linked accounts) and the admin user-detail
// endpoint (a target user's linked accounts). Reads the site's own ingested
// event log — no sidecar round-trip — and filters to sales whose owning account
// is in the set. Newest 50, already newest-first from shardEvents.list.

const shardEvents = require('../model/shardEvents/shardEvents.model')

async function salesForAccounts(accounts) {
  const set = accounts instanceof Set ? accounts : new Set(accounts)
  if (set.size === 0) return []
  const events = await shardEvents.list({ kind: 'vendor.sale', limit: 500 })
  return events
    .filter((e) => e.payload && set.has(e.payload.ownerAcct))
    .slice(0, 50)
    .map((e) => ({
      t: e.t,
      itemType: e.payload.itemType,
      amount: e.payload.amount,
      price: e.payload.price,
      commission: e.payload.commission,
      ownerAcct: e.payload.ownerAcct,
    }))
}

module.exports = { salesForAccounts }
