const fs = require('fs');
const path = require('path');

const inboxDir = path.join(__dirname, '../../../../docs/aiagent/raw_inbox');
const sourcesDir = path.join(__dirname, '../../../../docs/aiagent/raw_sources');

if (!fs.existsSync(sourcesDir)) {
    fs.mkdirSync(sourcesDir, { recursive: true });
}

if (!fs.existsSync(inboxDir)) {
    console.error(`Inbox directory does not exist: ${inboxDir}`);
    process.exit(1);
}

const files = fs.readdirSync(inboxDir);
console.log(`Found ${files.length} files in raw_inbox.`);

const localePattern = /[-.]ja(-SUMMARY)?\.md$/;

files.forEach(f => {
    const srcPath = path.join(inboxDir, f);
    if (fs.statSync(srcPath).isDirectory()) return;

    let subDir = 'articles';
    if (f.startsWith('GUIDE_')) {
        subDir = 'guides';
    } else if (f === 'grill-me.md' || f === 'grill-me-ja.md') {
        subDir = 'commands';
    } else if (f === 'suzanne.md') {
        subDir = 'misc';
    }

    const destDir = path.join(sourcesDir, subDir);
    if (!fs.existsSync(destDir)) {
        fs.mkdirSync(destDir, { recursive: true });
    }

    const destPath = path.join(destDir, f);
    fs.renameSync(srcPath, destPath);
    console.log(`Moved: ${f} -> raw_sources/${subDir}/${f}`);
});

console.log('File migration completed.');

