/**
 * Archive For_Web Files
 *
 * Moves files older than the current year into year-based subfolders.
 * e.g., X:\For_Web\2024\12345-1.TXT
 *
 * The TestDataSheetUploader only uploads files modified in the current year,
 * so archived files won't be re-uploaded. Keeps the active folder small and fast.
 *
 * Usage:
 *   node archive-for-web.js                Archive all pre-current-year files
 *   node archive-for-web.js --dry-run      Show what would be moved
 *   node archive-for-web.js --year 2024    Only archive files from 2024
 */

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

const FOR_WEB = 'X:\\For_Web';

function run() {
    const args = process.argv.slice(2);
    const dryRun = args.includes('--dry-run');
    const yearIdx = args.indexOf('--year');
    const targetYear = yearIdx >= 0 ? parseInt(args[yearIdx + 1]) : null;
    const currentYear = new Date().getFullYear();

    console.log('========================================');
    console.log('Archive For_Web Files');
    console.log('========================================');
    console.log(`Source: ${FOR_WEB}`);
    console.log(`Current year: ${currentYear}`);
    console.log(`Dry run: ${dryRun}`);
    if (targetYear) console.log(`Target year: ${targetYear}`);
    console.log(`Start: ${new Date().toISOString()}`);
    console.log('');

    if (!fs.existsSync(FOR_WEB)) {
        console.error('ERROR: For_Web directory not found');
        process.exit(1);
    }

    // Scan files
    console.log('Scanning files...');
    const entries = fs.readdirSync(FOR_WEB, { withFileTypes: true });

    const yearCounts = {};
    let scanned = 0;
    let toMove = 0;
    let moved = 0;
    let errors = 0;

    for (const entry of entries) {
        if (!entry.isFile()) continue;
        scanned++;

        if (scanned % 50000 === 0) {
            process.stdout.write(`\rScanned: ${scanned}`);
        }

        const filePath = path.join(FOR_WEB, entry.name);
        let stat;
        try {
            stat = fs.statSync(filePath);
        } catch (err) {
            continue;
        }

        const fileYear = stat.mtime.getFullYear();

        // Skip current year files
        if (fileYear >= currentYear) continue;

        // If targeting a specific year, skip others
        if (targetYear && fileYear !== targetYear) continue;

        yearCounts[fileYear] = (yearCounts[fileYear] || 0) + 1;
        toMove++;

        if (!dryRun) {
            // Create year subdirectory if needed
            const yearDir = path.join(FOR_WEB, String(fileYear));
            if (!fs.existsSync(yearDir)) {
                fs.mkdirSync(yearDir);
                console.log(`\nCreated directory: ${yearDir}`);
            }

            const destPath = path.join(yearDir, entry.name);
            try {
                fs.renameSync(filePath, destPath);
                moved++;
            } catch (err) {
                // If rename fails (cross-device), try copy+delete
                try {
                    fs.copyFileSync(filePath, destPath);
                    fs.unlinkSync(filePath);
                    moved++;
                } catch (err2) {
                    console.error(`\nERROR moving ${entry.name}: ${err2.message}`);
                    errors++;
                }
            }

            if (moved % 10000 === 0) {
                process.stdout.write(`\rMoved: ${moved}`);
            }
        }
    }

    console.log('\n');
    console.log('========================================');
    console.log('Archive Summary');
    console.log('========================================');
    console.log(`Files scanned: ${scanned}`);
    console.log(`Files to archive: ${toMove}`);

    if (Object.keys(yearCounts).length > 0) {
        console.log('\nBy year:');
        for (const [year, count] of Object.entries(yearCounts).sort()) {
            console.log(`  ${year}: ${count.toLocaleString()} files`);
        }
    }

    if (!dryRun) {
        console.log(`\nFiles moved: ${moved}`);
        console.log(`Errors: ${errors}`);
    }

    console.log(`\nEnd: ${new Date().toISOString()}`);
}

run();
