// import redisClient from '@/src/lib/redis-client'
import {
  getAllBannersForCache,
  getBanners,
  getBannerById as getBannerByIdFromDb,
  type BannerData,
} from '@/src/dbService'
import { scaffoldAssetUrl } from '@/src/lib/s3-client'

// Banner Type (matches getBanners return)
interface Banner {
  id: number
  name: string
  imageUrl: string | null
  serialNum: number | null
  productIds: number[] | null
  createdAt: Date
}

export async function initializeBannerStore(): Promise<void> {
  try {
    console.log('Initializing banner store in Redis...')

    const banners = await getAllBannersForCache()

    /*
    // Old implementation - direct DB queries:
    import { db } from '@/src/db/db_index'
    import { homeBanners } from '@/src/db/schema'
    import { isNotNull, asc } from 'drizzle-orm'

    const banners = await db.query.homeBanners.findMany({
      where: isNotNull(homeBanners.serialNum),
      orderBy: asc(homeBanners.serialNum),
    });
    */

    // Store each banner in Redis
    // for (const banner of banners) {
    //   const signedImageUrl = banner.imageUrl
    //     ? scaffoldAssetUrl(banner.imageUrl)
    //     : banner.imageUrl
    //
    //   const bannerObj: Banner = {
    //     id: banner.id,
    //     name: banner.name,
    //     imageUrl: signedImageUrl,
    //     serialNum: banner.serialNum,
    //     productIds: banner.productIds,
    //     createdAt: banner.createdAt,
    //   }
    //
    //   await redisClient.set(`banner:${banner.id}`, JSON.stringify(bannerObj))
    // }

    console.log('Banner store initialized successfully')
  } catch (error) {
    console.error('Error initializing banner store:', error)
  }
}

export async function getBannerById(id: number): Promise<Banner | null> {
  try {
    // const key = `banner:${id}`
    // const data = await redisClient.get(key)
    // if (!data) return null
    // return JSON.parse(data) as Banner

    const banner = await getBannerByIdFromDb(id)
    if (!banner) return null

    const signedImageUrl = banner.imageUrl
      ? scaffoldAssetUrl(banner.imageUrl)
      : banner.imageUrl

    return {
      id: banner.id,
      name: banner.name,
      imageUrl: signedImageUrl,
      serialNum: banner.serialNum,
      productIds: banner.productIds,
      createdAt: banner.createdAt,
    }
  } catch (error) {
    console.error(`Error getting banner ${id}:`, error)
    return null
  }
}

export async function getAllBanners(): Promise<Banner[]> {
  try {
    // Get all keys matching the pattern "banner:*"
    // const keys = await redisClient.KEYS('banner:*')
    //
    // if (keys.length === 0) return []
    //
    // // Get all banners using MGET for better performance
    // const bannersData = await redisClient.MGET(keys)
    //
    // const banners: Banner[] = []
    // for (const bannerData of bannersData) {
    //   if (bannerData) {
    //     banners.push(JSON.parse(bannerData) as Banner)
    //   }
    // }
    //
    // // Sort by serialNum to maintain the same order as the original query
    // banners.sort((a, b) => (a.serialNum || 0) - (b.serialNum || 0))
    //
    // return banners

    const banners = await getBanners()

    return banners.map((banner) => {
      const signedImageUrl = banner.imageUrl
        ? scaffoldAssetUrl(banner.imageUrl)
        : banner.imageUrl

      return {
        id: banner.id,
        name: banner.name,
        imageUrl: signedImageUrl,
        serialNum: banner.serialNum,
        productIds: banner.productIds,
        createdAt: banner.createdAt,
      }
    })
  } catch (error) {
    console.error('Error getting all banners:', error)
    return []
  }
}
