// Read-only: fetch wj62's signed repo CAR from the bridge, find the profile record block, dag-cbor
// decode it, and report whether avatar.ref is a TRUE CID (tag 42) or a plain {$link:string} map.
// Decides the correct format for a profile update. bare scripts/inspect-profile-ref.mjs
import './lib/bare-globals.js'
import fs from 'bare-fs'
import { CID } from 'multiformats/cid'
import * as dagCbor from '@ipld/dag-cbor'
import { readCar } from 'atproto-repo/lib/repo.js'
// Reads a repo CAR file (curl the getRepo output to a file first). bare inspect-profile-ref.mjs <car>
const carPath = globalThis.Bare.argv[2]
const buf = new Uint8Array(fs.readFileSync(carPath))
const car = await readCar(buf)
let found = null
const types = {}
console.log('CAR blocks:', car.blocks.size)
for (const [cidStr, block] of car.blocks) {
try {
const rec = dagCbor.decode(block.bytes)
const t = rec && rec.$type ? rec.$type : (rec && rec.data ? '<commit>' : (rec && rec.e ? '<mst>' : '<other>'))
types[t] = (types[t] || 0) + 1
if (rec && rec.$type === 'app.bsky.actor.profile') { found = { cidStr, rec }; break }
} catch (e) { types['<decode-err>'] = (types['<decode-err>'] || 0) + 1 }
}
console.log('types:', JSON.stringify(types))
if (!found) { console.log('NO profile record block found in CAR'); globalThis.Bare.exit(0) }
const ref = found.rec.avatar && found.rec.avatar.ref
const isCid = !!CID.asCID(ref)
console.log('profile block cid=' + found.cidStr)
console.log('avatar.ref isTrueCID=' + isCid + ' typeof=' + typeof ref + ' value=' + (isCid ? ref.toString() : JSON.stringify(ref)))
console.log('avatar.size=' + (found.rec.avatar && found.rec.avatar.size) + ' mimeType=' + (found.rec.avatar && found.rec.avatar.mimeType))
globalThis.Bare.exit(0)