#!/usr/bin/env node
/**
 * Database Cleanup Script
 * Removes junk documents that should never have been imported into MongoDB
 *
 * Categories:
 * 1. Internal session notes (10 documents)
 * 2. Obsolete Phase 2 documents (9 documents)
 * 3. Confidential document duplicates (5 documents - keep filesystem .md files)
 *
 * Total: 24 documents to delete
 */

require('dotenv').config();
const { getDb } = require('../src/utils/db.util');

// Documents to delete organized by category (actual slugs from database)
const documentsToDelete = {
  sessionNotes: [
    'ai-features-implementation-session-2025-10-07',
    'session-handoff-2025-10-07',
    'session-handoff-2025-10-07-part-4-governance-active-progress-review',
    'session-handoff-crossreferencevalidator-debugging',
    'session-handoff-tractatus-framework-activation'
  ],

  obsoletePhase2: [
    'phase-2-cost-estimates-hosting-api-usage',
    'phase-2-deployment-guide-granular-task-instructions',
    'phase-2-infrastructure-plan',
    'phase-2-kickoff-checklist',
    'phase-2-preparation-advisory',
    'phase-2-production-deployment-ai-features',
    'phase-2-progress-report-week-5',
    'phase-2-roadmap-production-deployment-ai-powered-features',
    'phase-2-soft-launch-email-templates',
    'tractatus-blog-post-outlines',
    'tractatus-governance-framework-test-suite-improvement-session-part-2',
    'tractatus-production-comprehensive-testing-checklist',
    'tractatus-production-testing-results',
    'tractatus-project-implementation-progress-report'
  ],

  confidentialDuplicates: [
    'appendix-e-contact-information',
    'cover-letter-for-anthropic-submission',
    'koha-stripe-payment-setup-guide',
    'security-audit-report'
  ]
};

async function cleanupDatabase() {
  console.log('🧹 Starting database cleanup...\n');

  const db = await getDb();
  const collection = db.collection('documents');

  // Track results
  const results = {
    sessionNotes: { deleted: 0, notFound: [] },
    obsoletePhase2: { deleted: 0, notFound: [] },
    confidentialDuplicates: { deleted: 0, notFound: [] }
  };

  // Delete session notes
  console.log('📋 Deleting internal session notes...');
  for (const slug of documentsToDelete.sessionNotes) {
    const result = await collection.deleteOne({ slug });
    if (result.deletedCount > 0) {
      console.log(`  ✅ Deleted: ${slug}`);
      results.sessionNotes.deleted++;
    } else {
      console.log(`  ⚠️  Not found: ${slug}`);
      results.sessionNotes.notFound.push(slug);
    }
  }

  // Delete obsolete Phase 2 documents
  console.log('\n📦 Deleting obsolete Phase 2 documents...');
  for (const slug of documentsToDelete.obsoletePhase2) {
    const result = await collection.deleteOne({ slug });
    if (result.deletedCount > 0) {
      console.log(`  ✅ Deleted: ${slug}`);
      results.obsoletePhase2.deleted++;
    } else {
      console.log(`  ⚠️  Not found: ${slug}`);
      results.obsoletePhase2.notFound.push(slug);
    }
  }

  // Delete confidential duplicates
  console.log('\n🔒 Deleting confidential document duplicates from MongoDB...');
  console.log('   (Filesystem .md files will be preserved)');
  for (const slug of documentsToDelete.confidentialDuplicates) {
    const result = await collection.deleteOne({ slug });
    if (result.deletedCount > 0) {
      console.log(`  ✅ Deleted: ${slug}`);
      results.confidentialDuplicates.deleted++;
    } else {
      console.log(`  ⚠️  Not found: ${slug}`);
      results.confidentialDuplicates.notFound.push(slug);
    }
  }

  // Summary
  console.log('\n' + '='.repeat(70));
  console.log('📊 Cleanup Summary');
  console.log('='.repeat(70));
  console.log(`Session Notes:           ${results.sessionNotes.deleted}/${documentsToDelete.sessionNotes.length} deleted`);
  console.log(`Obsolete Phase 2:        ${results.obsoletePhase2.deleted}/${documentsToDelete.obsoletePhase2.length} deleted`);
  console.log(`Confidential Duplicates: ${results.confidentialDuplicates.deleted}/${documentsToDelete.confidentialDuplicates.length} deleted`);
  console.log('='.repeat(70));

  const totalDeleted = results.sessionNotes.deleted + results.obsoletePhase2.deleted + results.confidentialDuplicates.deleted;
  const totalExpected = documentsToDelete.sessionNotes.length + documentsToDelete.obsoletePhase2.length + documentsToDelete.confidentialDuplicates.length;

  console.log(`\n✅ Total deleted: ${totalDeleted}/${totalExpected} documents\n`);

  // Report not found documents
  const allNotFound = [
    ...results.sessionNotes.notFound,
    ...results.obsoletePhase2.notFound,
    ...results.confidentialDuplicates.notFound
  ];

  if (allNotFound.length > 0) {
    console.log('⚠️  Documents not found (may have been deleted already):');
    allNotFound.forEach(slug => console.log(`   - ${slug}`));
    console.log();
  }

  // Get final document count
  const finalCount = await collection.countDocuments();
  console.log(`📚 Remaining documents in database: ${finalCount}\n`);

  process.exit(0);
}

// Run cleanup
cleanupDatabase().catch(error => {
  console.error('❌ Cleanup failed:', error);
  process.exit(1);
});
