import { publicProcedure, router } from '@/src/trpc/trpc-index'
import { scaffoldAssetUrl } from '@/src/lib/s3-client'
import { getUserActiveBanners as getUserActiveBannersInDb } from '@/src/dbService'
import type { UserBannersResponse } from '@packages/shared'

export async function scaffoldBanners(): Promise<UserBannersResponse> {
  const banners = await getUserActiveBannersInDb()

  /*
  // Old implementation - direct DB queries:
  const banners = await db.query.homeBanners.findMany({
    where: isNotNull(homeBanners.serialNum), // Only show assigned banners
    orderBy: asc(homeBanners.serialNum), // Order by slot number 1-4
  });
  */

  const bannersWithSignedUrls = banners.map((banner) => ({
    ...banner,
    imageUrl: banner.imageUrl ? scaffoldAssetUrl(banner.imageUrl) : banner.imageUrl,
  }))

  return {
    banners: bannersWithSignedUrls,
  }
}

export const bannerRouter = router({
  getBanners: publicProcedure
    .query(async () => {
      const response = await scaffoldBanners();
      return response;
    }),
});
