import { portfolioUsers } from "./../db/schema/portfolio";
import { eq } from "drizzle-orm";
import { db } from "@/app/db/db";
import { portfolio } from "@/app/db/schema/portfolio";
import type { Portfolio } from "@/app/db/schema/portfolio";

export async function getPortfolios(userId: bigint): Promise<Portfolio[]> {
  const userPortfolios = await db
    .select()
    .from(portfolio)
    .leftJoin(portfolioUsers, eq(portfolio.id, portfolioUsers.portfolioId))
    .where(eq(portfolioUsers.userId, userId));

  const portfolios = userPortfolios.map((data) => data.portfolio);

  return portfolios;
}
