const PocketBase = require('pocketbase').default

// Initialize the PocketBase client
const pb = new PocketBase('http://127.0.0.1:8090')

async function main () {
  try {
    // Authenticate as an admin
    await pb.admins.authWithPassword('karkinge@gmail.com', 'XXXXXXX') // Replace with your admin credentials

    // Parse JSON file with categories
    const data = require('../../gyoza/database_backups/20250320.json')

    // Perform some operations, e.g., create a new record in the 'categories' collection
    for (let history of data.history) {
      if (!history) continue
      if (history.image) {
        // Extract the filename from a url like : "https://firebasestorage.googleapis.com/v0/b/gyoza-hfs-stock.appspot.com/o/1678571846485IMG_20230311_225636.jpg?alt=media&token=215c00a8-e873-497f-8f11-b75c7e2fab42"
        const filename = history.image.split('?')[0].split('/').pop()
        history.image = filename
      }
      let newData = {
        ref: history.ref,
        text: history.text,
        user: history.user,
        image: history.image,
        date: new Date(history.timestamp).toISOString()
      }
      console.log(newData)
      const newHistory = await pb.collection('history').create(newData)
    }
  } catch (error) {
    console.error('Error:', error)
  }
}

main()
