/**
 * Test for createEntities default value bug fix (v4.3.2)
 *
 * Bug: If createEntities was undefined, it defaulted to false
 * Fix: Now defaults to true when undefined
 */

import { describe, it, expect, beforeEach } from 'vitest'
import { Brainy, NounType } from '../../src/index.js'
import * as fs from 'fs'
import * as path from 'path'

describe('createEntities Default Value (v4.3.2 Bug Fix)', () => {
  let brain: Brainy
  // Unique testDir per test invocation so parallel vitest shards don't fight
  // over the same writer lock on filesystem storage.
  const testDir = path.join(
    require('os').tmpdir(),
    `brainy-create-entities-default-${process.pid}-${Math.random().toString(36).slice(2)}`
  )

  beforeEach(async () => {
    if (fs.existsSync(testDir)) {
      fs.rmSync(testDir, { recursive: true })
    }

    brain = new Brainy({ requireSubtype: false,
      storage: {
        type: 'filesystem',
        path: testDir
      }
    })
    await brain.init()
  })

  afterEach(async () => {
    // Release the writer lock before cleaning up the directory, otherwise
    // parallel vitest shards see a stale lockfile on retry.
    if (brain) {
      try { await brain.close() } catch { /* noop */ }
    }
    if (fs.existsSync(testDir)) {
      fs.rmSync(testDir, { recursive: true })
    }
  })

  it('should create graph entities when createEntities is undefined (default behavior)', async () => {
    // Create a minimal CSV to import
    const csvContent = `Name,Type
Alice,person
Bob,person
New York,location`

    const csvPath = path.join(testDir, 'test.csv')
    fs.mkdirSync(testDir, { recursive: true })
    fs.writeFileSync(csvPath, csvContent)

    // Import WITHOUT specifying createEntities (should default to true)
    const result = await brain.import(csvPath, {
      vfsPath: '/imports/test',
      groupBy: 'flat'
      // NOTE: createEntities is NOT specified - should default to true
    })

    console.log('\n📊 Import Result:')
    console.log(`   Entities created: ${result.stats.graphNodesCreated}`)
    console.log(`   VFS files created: ${result.stats.vfsFilesCreated}`)

    // Verify graph entities were created
    expect(result.stats.graphNodesCreated).toBeGreaterThan(0)

    // Verify we can query by type. The exact match-count varies with the
    // import path's neural-extraction behavior; the bug this test guards
    // against is "createEntities defaulted off and no graph entities were
    // produced" — covered by the >0 assertions below.
    const people = await brain.find({ type: NounType.Person, limit: 10 })
    console.log(`\n🔍 Type Filtering:`)
    console.log(`   Person filter: ${people.length}`)

    expect(people.length).toBeGreaterThan(0)

    const locations = await brain.find({ type: NounType.Location, limit: 10 })
    console.log(`   Location filter: ${locations.length}`)

    expect(locations.length).toBeGreaterThan(0)

    console.log('\n✅ Graph entities created by default!')
  })

})
