
import { prisma } from '../lib/prisma'

// Copy of parseNote from app/actions/notes.ts (since it's not exported)
function parseNote(dbNote: any) {
    const embedding = dbNote.embedding ? JSON.parse(dbNote.embedding) : null

    if (embedding && Array.isArray(embedding)) {
        // Simplified validation check for test
        if (embedding.length !== 1536 && embedding.length !== 768 && embedding.length !== 384) {
            return {
                ...dbNote,
                checkItems: dbNote.checkItems ? JSON.parse(dbNote.checkItems) : null,
                labels: dbNote.labels ? JSON.parse(dbNote.labels) : null,
                images: dbNote.images ? JSON.parse(dbNote.images) : null,
                links: dbNote.links ? JSON.parse(dbNote.links) : null,
                embedding: null,
                sharedWith: dbNote.sharedWith ? JSON.parse(dbNote.sharedWith) : [],
                size: dbNote.size || 'small',
            }
        }
    }

    return {
        ...dbNote,
        checkItems: dbNote.checkItems ? JSON.parse(dbNote.checkItems) : null,
        labels: dbNote.labels ? JSON.parse(dbNote.labels) : null,
        images: dbNote.images ? JSON.parse(dbNote.images) : null,
        links: dbNote.links ? JSON.parse(dbNote.links) : null,
        embedding,
        sharedWith: dbNote.sharedWith ? JSON.parse(dbNote.sharedWith) : [],
        size: dbNote.size || 'small',
    }
}

async function main() {
    console.log('🧪 Testing parseNote logic...')

    // 1. Fetch a real note from DB that is KNOWN to be large
    const rawNote = await prisma.note.findFirst({
        where: { size: 'large' }
    })

    if (!rawNote) {
        console.error('❌ No large note found in DB. Create one first.')
        return
    }

    console.log('📊 Raw Note from DB:', { id: rawNote.id, size: rawNote.size })

    // 2. Pass it through parseNote
    const parsed = parseNote(rawNote)
    console.log('🔄 Parsed Note:', { id: parsed.id, size: parsed.size })

    if (parsed.size === 'large') {
        console.log('✅ parseNote preserves size correctly.')
    } else {
        console.error('❌ parseNote returned wrong size:', parsed.size)
    }

}

main().catch(console.error).finally(() => prisma.$disconnect())
