#!/usr/bin/env node

/**
 * Migration: Comprehensive Document Recategorization
 *
 * This migration:
 * 1. Renames "Value Pluralism FAQ" to "Understanding Value Pluralism"
 * 2. Recategorizes all documents into proper categories
 * 3. Restores relevant archived documents to public visibility
 * 4. Assigns proper order values for intuitive sidebar organization
 */

require('dotenv').config();
const { MongoClient } = require('mongodb');

const MONGO_URI = process.env.MONGODB_URI || process.env.MONGO_URI || 'mongodb://localhost:27017';
const DB_NAME = process.env.MONGODB_DB || process.env.MONGO_DB || 'tractatus_dev';

// Document updates: { slug, updates }
const DOCUMENT_UPDATES = [
  // ========================================
  // GETTING STARTED
  // ========================================
  {
    slug: 'introduction-to-the-tractatus-framework',
    updates: {
      category: 'getting-started',
      visibility: 'public',
      order: 1
    }
  },
  {
    slug: 'architectural-overview-and-research-status',
    updates: {
      category: 'getting-started',
      visibility: 'public',
      order: 2
    }
  },
  {
    slug: 'core-concepts-of-the-tractatus-framework',
    updates: {
      category: 'getting-started',
      visibility: 'public',
      order: 3
    }
  },
  {
    slug: 'tractatus-ai-safety-framework-core-values-and-principles',
    updates: {
      category: 'getting-started',
      visibility: 'public',
      order: 4
    }
  },
  {
    slug: 'tractatus-agentic-governance-system-glossary-of-terms',
    updates: {
      category: 'getting-started',
      visibility: 'public',
      order: 5
    }
  },

  // ========================================
  // TECHNICAL REFERENCE
  // ========================================
  {
    slug: 'technical-architecture',
    updates: {
      category: 'technical-reference',
      visibility: 'public',
      order: 1
    }
  },
  {
    slug: 'implementation-guide',
    updates: {
      category: 'technical-reference',
      visibility: 'public',
      order: 2
    }
  },
  {
    slug: 'tractatus-framework-implementation-guide',
    updates: {
      category: 'technical-reference',
      visibility: 'public',
      order: 3
    }
  },
  {
    slug: 'api-reference-complete',
    updates: {
      category: 'technical-reference',
      visibility: 'public',
      order: 4
    }
  },
  {
    slug: 'api-javascript-examples',
    updates: {
      category: 'technical-reference',
      visibility: 'public',
      order: 5
    }
  },
  {
    slug: 'api-python-examples',
    updates: {
      category: 'technical-reference',
      visibility: 'public',
      order: 6
    }
  },
  {
    slug: 'openapi-specification',
    updates: {
      category: 'technical-reference',
      visibility: 'public',
      order: 7
    }
  },

  // ========================================
  // THEORY & RESEARCH
  // ========================================
  {
    slug: 'research-foundations-scholarly-review-and-context',
    updates: {
      category: 'research-theory',
      visibility: 'public',
      order: 1
    }
  },
  {
    slug: 'organizational-theory-foundations-of-the-tractatus-framework',
    updates: {
      category: 'research-theory',
      visibility: 'public',
      order: 2
    }
  },
  {
    slug: 'pluralistic-values-research-foundations',
    updates: {
      category: 'research-theory',
      visibility: 'public',
      order: 3
    }
  },
  {
    slug: 'research-topic-rule-proliferation-and-transactional-overhead-in-ai-governance',
    updates: {
      category: 'research-theory',
      visibility: 'public',
      order: 4
    }
  },
  {
    slug: 'research-topic-concurrent-session-architecture-limitations-in-claude-code-governance',
    updates: {
      category: 'research-theory',
      visibility: 'public',
      order: 5
    }
  },

  // ========================================
  // ADVANCED TOPICS
  // ========================================
  {
    slug: 'value-pluralism-faq',
    updates: {
      title: 'Understanding Value Pluralism in Tractatus',
      category: 'advanced-topics',
      visibility: 'public',
      order: 1
    }
  },
  {
    slug: 'pluralistic-values-deliberation-plan-v2',
    updates: {
      category: 'advanced-topics',
      visibility: 'public',
      order: 2
    }
  },
  {
    slug: 'comparison-matrix-claude-code-claudemd-and-tractatus-framework',
    updates: {
      category: 'advanced-topics',
      visibility: 'public',
      order: 3
    }
  },
  {
    slug: 'research-scope-feasibility-of-llm-integrated-tractatus-framework',
    updates: {
      category: 'advanced-topics',
      visibility: 'public',
      order: 4
    }
  },

  // ========================================
  // CASE STUDIES
  // ========================================
  {
    slug: 'the-27027-incident-a-case-study-in-pattern-recognition-bias',
    updates: {
      category: 'case-studies',
      visibility: 'public',
      order: 1
    }
  },
  {
    slug: 'when-frameworks-fail-and-why-thats-ok',
    updates: {
      category: 'case-studies',
      visibility: 'public',
      order: 2
    }
  },
  {
    slug: 'our-framework-in-action-detecting-and-correcting-ai-fabrications',
    updates: {
      category: 'case-studies',
      visibility: 'public',
      order: 3
    }
  },
  {
    slug: 'real-world-ai-governance-a-case-study-in-framework-failure-and-recovery',
    updates: {
      category: 'case-studies',
      visibility: 'public',
      order: 4
    }
  },
  {
    slug: 'framework-governance-in-action-pre-publication-security-audit',
    updates: {
      category: 'case-studies',
      visibility: 'public',
      order: 5
    }
  },
  {
    slug: 'case-studies-real-world-llm-failure-modes',
    updates: {
      category: 'case-studies',
      visibility: 'public',
      order: 6
    }
  },

  // ========================================
  // BUSINESS & LEADERSHIP
  // ========================================
  {
    slug: 'executive-brief-tractatus-based-llm-architecture-for-ai-safety',
    updates: {
      category: 'business-leadership',
      visibility: 'public',
      order: 1
    }
  },
  {
    slug: 'ai-governance-business-case-template-tractatus-framework',
    updates: {
      category: 'business-leadership',
      visibility: 'public',
      order: 2
    }
  },
  {
    slug: 'implementation-roadmap-24-month-deployment-plan',
    updates: {
      category: 'business-leadership',
      visibility: 'public',
      order: 3
    }
  },

  // ========================================
  // ARCHIVE (outdated/superseded)
  // ========================================
  {
    slug: 'phase-5-poc-session-1-summary',
    updates: {
      category: 'archived',
      visibility: 'archived',
      order: 999
    }
  },
  {
    slug: 'phase-5-poc-session-2-summary',
    updates: {
      category: 'archived',
      visibility: 'archived',
      order: 999
    }
  },

  // All other Phase 2 documents - stay archived
  // All session handoffs - stay archived
  // Test reports - stay archived
  // Blog outlines - stay archived
  // Duplicate/superseded documents - stay archived
];

async function migrate() {
  console.log('🔧 Starting comprehensive document categorization migration...');
  console.log(`   Database: ${DB_NAME}`);
  console.log('');

  const client = new MongoClient(MONGO_URI);

  try {
    await client.connect();
    const db = client.db(DB_NAME);
    const collection = db.collection('documents');

    let updated = 0;
    let notFound = 0;
    let unchanged = 0;

    for (const { slug, updates } of DOCUMENT_UPDATES) {
      const result = await collection.updateOne(
        { slug },
        { $set: updates }
      );

      if (result.matchedCount === 0) {
        console.log(`   ⚠️  Document not found: ${slug}`);
        notFound++;
      } else if (result.modifiedCount === 0) {
        console.log(`   ℹ️  Already up to date: ${slug}`);
        unchanged++;
      } else {
        console.log(`   ✅ Updated: ${slug}`);
        if (updates.title) {
          console.log(`      - title: ${updates.title}`);
        }
        console.log(`      - category: ${updates.category}`);
        console.log(`      - visibility: ${updates.visibility}`);
        console.log(`      - order: ${updates.order}`);
        updated++;
      }
    }

    console.log('');
    console.log('📊 Migration Summary:');
    console.log(`   ✅ Updated: ${updated}`);
    console.log(`   ℹ️  Unchanged: ${unchanged}`);
    console.log(`   ⚠️  Not found: ${notFound}`);
    console.log('');

    // Verification by category
    console.log('🔍 Verification - Documents by Category:');
    const publicDocs = await collection.find({ visibility: 'public' }).sort({ category: 1, order: 1 }).toArray();

    const byCategory = {};
    publicDocs.forEach(doc => {
      if (!byCategory[doc.category]) {
        byCategory[doc.category] = [];
      }
      byCategory[doc.category].push(doc);
    });

    Object.keys(byCategory).sort().forEach(cat => {
      console.log(`\n   ${cat}:`);
      byCategory[cat].forEach(doc => {
        console.log(`      [${doc.order}] ${doc.title}`);
      });
    });

    console.log('');
    console.log('✨ Migration complete!');

  } catch (error) {
    console.error('❌ Migration failed:', error);
    process.exit(1);
  } finally {
    await client.close();
  }
}

migrate();
