import type { PositionSnapshot } from "../../domain/src/types";

export interface AccountPosition extends PositionSnapshot {
  accountId: string;
  fundingRate?: number;
  fundingCorrelation?: number;
}

export interface AggregatedPosition {
  symbol: string;
  base: number;
  quote: number;
  ts: number;
  accounts: AccountPosition[];
}

export class PositionManager {
  async snapshot(symbol: string, snapshots: Array<PositionSnapshot & { accountId?: string }>): Promise<AggregatedPosition> {
    const accounts: AccountPosition[] = snapshots.map((snap, idx) => ({
      accountId: snap.accountId ?? `account-${idx}`,
      symbol: snap.symbol,
      base: snap.base,
      quote: snap.quote,
      entryPx: snap.entryPx,
      ts: snap.ts,
      fundingRate: (snap as any).fundingRate,
      fundingCorrelation: (snap as any).fundingCorrelation
    }));

    const base = accounts.reduce((sum, snap) => sum + snap.base, 0);
    const quote = accounts.reduce((sum, snap) => sum + snap.quote, 0);
    const ts = Date.now();

    return { symbol, base, quote, ts, accounts };
  }
}
