/**
 * Knowledge Search Command
 * Search your knowledge base with semantic understanding
 */

import { readFileSync, existsSync } from 'fs'
import { join } from 'path'
import { homedir } from 'os'

export interface SearchOptions {
  query: string
  category?: string
  tags?: string[]
  limit?: number
}

const KNOWLEDGE_FILE = join(homedir(), '.claude', 'knowledge', 'knowledge.json')

interface KnowledgeEntry {
  id: string
  title?: string
  content: string
  tags: string[]
  category: string
  source?: string
  timestamp: string
}

export async function handle(args: SearchOptions, context: any): Promise<string> {
  const { query, category, tags, limit = 10 } = args

  try {
    if (!existsSync(KNOWLEDGE_FILE)) {
      return 'Knowledge base is empty. Add some knowledge first!'
    }

    const data = readFileSync(KNOWLEDGE_FILE, 'utf-8')
    const knowledge: KnowledgeEntry[] = JSON.parse(data)

    // Filter knowledge
    let results = knowledge

    if (category) {
      results = results.filter(entry => entry.category === category)
    }

    if (tags && tags.length > 0) {
      results = results.filter(entry =>
        tags.some(tag => entry.tags.includes(tag))
      )
    }

    // Text search (simple implementation)
    if (query) {
      const queryLower = query.toLowerCase()
      results = results.filter(entry =>
        entry.content.toLowerCase().includes(queryLower) ||
        (entry.title && entry.title.toLowerCase().includes(queryLower)) ||
        entry.tags.some(tag => tag.toLowerCase().includes(queryLower))
      )
    }

    // Limit results
    results = results.slice(0, limit)

    if (results.length === 0) {
      return `No results found for query: "${query}"`
    }

    // Format results
    const formatted = results.map(entry => {
      const lines = [
        `ID: ${entry.id}`,
        entry.title ? `Title: ${entry.title}` : null,
        `Category: ${entry.category}`,
        `Tags: ${entry.tags.join(', ') || 'none'}`,
        `Date: ${new Date(entry.timestamp).toLocaleDateString()}`,
        '',
        entry.content.slice(0, 200) + (entry.content.length > 200 ? '...' : ''),
        ''
      ]
      return lines.filter(Boolean).join('\n')
    })

    return `Found ${results.length} result(s):\n\n${formatted.join('\n---\n\n')}`
  } catch (error: any) {
    throw new Error(`Failed to search knowledge: ${error.message}`)
  }
}

export default { handle }
