import { readdirSync, readFileSync } from 'node:fs';
import { join } from 'node:path';
import { openEmailDb, upsertFromRawEml, setSyncMeta } from '../src/servers/sidecar/email/store';

const DATA_PATH = process.env.DATA_PATH ?? join(process.cwd(), 'data');

// Find all user directories that have Gmail emails
const targetEmail = process.argv[2];

if (targetEmail) {
  migrate(targetEmail);
} else {
  const entries = readdirSync(DATA_PATH, { withFileTypes: true });
  for (const entry of entries) {
    if (!entry.isDirectory()) continue;
    if (!entry.name.includes('@')) continue;
    const emailDir = join(DATA_PATH, entry.name, 'Gmail', 'emails');
    try {
      const files = readdirSync(emailDir).filter((f) => f.endsWith('.eml'));
      if (files.length > 0) migrate(entry.name);
    } catch {
      // no Gmail dir for this user
    }
  }
}

function migrate(userEmail: string): void {
  console.log(`Migrating ${userEmail}...`);
  const emailDir = join(DATA_PATH, userEmail, 'Gmail', 'emails');
  // Obsolete one-off migration (old .eml-file store → SQLite); kept only to compile.
  const db = openEmailDb(userEmail, userEmail);

  let filenames: string[];
  try {
    filenames = readdirSync(emailDir).filter((f) => f.endsWith('.eml'));
  } catch {
    console.log('  No .eml files found');
    db.close();
    return;
  }

  const existingIds = new Set<string>();
  const rows = db.query('SELECT id FROM emails').all() as Array<{ id: string }>;
  for (const row of rows) existingIds.add(row.id);

  let added = 0;
  let skipped = 0;
  let errors = 0;

  db.exec('BEGIN');
  try {
    for (const filename of filenames) {
      const id = filename.replace(/\.eml$/, '');
      if (existingIds.has(id)) {
        skipped++;
        continue;
      }
      try {
        const raw = readFileSync(join(emailDir, filename), 'utf-8');
        upsertFromRawEml({ db, id, raw, integration: 'gmail', emailAccount: userEmail, labels: ['INBOX'] });
        added++;
      } catch {
        errors++;
      }
    }
    db.exec('COMMIT');
  } catch (err) {
    db.exec('ROLLBACK');
    throw err;
  }

  console.log(`  ${filenames.length} .eml files — ${added} added, ${skipped} skipped, ${errors} errors`);

  // Store the latest email date so the next sync only fetches emails after it
  const row = db.query('SELECT date FROM emails ORDER BY date DESC LIMIT 1').get() as { date: string } | null;
  if (row?.date) {
    setSyncMeta(db, 'last_sync_date', row.date);
    console.log(`  Stored last_sync_date: ${row.date}`);
  }
  db.close();
}
