import { readdir, stat } from 'node:fs/promises'
import { join, relative } from 'node:path'

interface MediaFile {
  name: string
  path: string
  size: number
  type: string
  modifiedAt: string
}

export default defineEventHandler(async () => {
  const publicDir = join(process.cwd(), 'public')
  const files: MediaFile[] = []

  await walk(publicDir, publicDir, files)

  return files.sort((a, b) => b.modifiedAt.localeCompare(a.modifiedAt))
})

async function walk(dir: string, root: string, files: MediaFile[]) {
  const entries = await readdir(dir, { withFileTypes: true })

  for (const entry of entries) {
    const fullPath = join(dir, entry.name)

    // Skip hidden files and gratewizard-app
    if (entry.name.startsWith('.') || entry.name === 'gratewizard-app') continue

    if (entry.isDirectory()) {
      await walk(fullPath, root, files)
    }
    else {
      const info = await stat(fullPath)
      const relPath = '/' + relative(root, fullPath)
      const ext = entry.name.split('.').pop()?.toLowerCase() ?? ''

      let type = 'other'
      if (['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg'].includes(ext)) type = 'image'
      else if (['mp3', 'wav', 'ogg', 'flac', 'm4a'].includes(ext)) type = 'audio'
      else if (['pdf'].includes(ext)) type = 'document'

      files.push({
        name: entry.name,
        path: relPath,
        size: info.size,
        type,
        modifiedAt: info.mtime.toISOString(),
      })
    }
  }
}
