'use strict';

const { ensureUserDefaultCategories } = require('../db/database');

// Independent user-owned tables (each has a user_id) wiped wholesale.
const USER_TABLES = [
  'bill_merchant_rules', 'match_suggestion_rejections', 'autopay_suggestion_dismissals',
  'declined_subscription_hints', 'subscription_recommendation_feedback', 'user_catalog_descriptors',
  'spending_category_rules', 'spending_budgets', 'bill_templates', 'snowball_plans',
  'monthly_starting_amounts', 'monthly_income', 'calendar_tokens', 'import_sessions', 'import_history',
];

/**
 * Permanently erase a user's financial + imported data, resetting them to a clean
 * slate — WITHOUT touching the account, sessions, 2FA/WebAuthn, login history, or
 * preferences. Runs in one transaction (child → parent order so it holds with
 * foreign_keys ON), then re-seeds default categories so the app stays usable and
 * writes an audit row to import_history.
 *
 * @returns {{ erased: number, counts: Record<string, number> }}
 */
function eraseUserData(db, userId) {
  const counts = {};
  const del = (label, sql) => { counts[label] = db.prepare(sql).run(userId).changes; };

  const run = db.transaction(() => {
    // Bill-children (no user_id of their own) — remove before bills.
    del('payments', 'DELETE FROM payments WHERE bill_id IN (SELECT id FROM bills WHERE user_id = ?)');
    del('monthly_bill_state', 'DELETE FROM monthly_bill_state WHERE bill_id IN (SELECT id FROM bills WHERE user_id = ?)');
    del('bill_history_ranges', 'DELETE FROM bill_history_ranges WHERE bill_id IN (SELECT id FROM bills WHERE user_id = ?)');

    // Bank-data chain: transactions → accounts → sources.
    del('transactions', 'DELETE FROM transactions WHERE user_id = ?');
    del('financial_accounts', 'DELETE FROM financial_accounts WHERE user_id = ?');
    del('data_sources', 'DELETE FROM data_sources WHERE user_id = ?');

    for (const t of USER_TABLES) del(t, `DELETE FROM ${t} WHERE user_id = ?`);

    // bills → categories → category_groups (bills.category_id, categories.group_id FKs).
    del('bills', 'DELETE FROM bills WHERE user_id = ?');
    del('categories', 'DELETE FROM categories WHERE user_id = ?');
    del('category_groups', 'DELETE FROM category_groups WHERE user_id = ?');
  });
  run();

  // Re-seed default categories so the app is immediately usable again.
  ensureUserDefaultCategories(userId);

  const erased = Object.values(counts).reduce((sum, n) => sum + n, 0);
  db.prepare(`
    INSERT INTO import_history (user_id, imported_at, source_filename, file_type,
      rows_parsed, rows_created, rows_updated, rows_skipped, rows_ambiguous, rows_errored, options_json, summary_json)
    VALUES (?, ?, 'erase-my-data', 'erase-data', ?, 0, 0, 0, 0, 0, ?, ?)
  `).run(userId, new Date().toISOString(), erased, JSON.stringify({ action: 'erase-my-data' }), JSON.stringify(counts));

  return { erased, counts };
}

module.exports = { eraseUserData };
