'use strict';

// Batch 4: richer export — full JSON assembly (money in dollars) and the payments
// export's date-range vs year filtering.
const test = require('node:test');
const assert = require('node:assert/strict');
const os = require('node:os');
const path = require('node:path');
const fs = require('node:fs');

const dbPath = path.join(os.tmpdir(), `bill-tracker-export-richer-${process.pid}.sqlite`);
process.env.DB_PATH = dbPath;

const { getDb, closeDb } = require('../db/database');
const exportRouter = require('../routes/export');
const { getUserExportData } = exportRouter;

let userId, billId;

function callExport(query) {
  const layer = exportRouter.stack.find(l => l.route?.path === '/' && l.route.methods.get);
  const handler = layer.route.stack[0].handle;
  return new Promise((resolve) => {
    const headers = {};
    const req = { query, user: { id: userId, role: 'user' } };
    const res = {
      statusCode: 200,
      setHeader(k, v) { headers[k] = v; },
      status(c) { this.statusCode = c; return this; },
      json(d) { resolve({ status: this.statusCode, headers, json: d }); },
      send(body) { resolve({ status: this.statusCode, headers, body }); },
    };
    handler(req, res);
  });
}

test.before(() => {
  const db = getDb();
  userId = db.prepare("INSERT INTO users (username, password_hash, role, active) VALUES ('export-user','x','user',1)").run().lastInsertRowid;
  billId = db.prepare("INSERT INTO bills (user_id, name, due_day, expected_amount, active) VALUES (?, 'Rent', 1, 120000, 1)").run(userId).lastInsertRowid;
  const pay = db.prepare("INSERT INTO payments (bill_id, amount, paid_date, payment_source) VALUES (?, ?, ?, 'manual')");
  pay.run(billId, 8500, '2025-06-15'); // prior year
  pay.run(billId, 9000, '2026-01-10'); // in range
  pay.run(billId, 9500, '2026-07-20'); // out of range, same year
});

test.after(() => {
  closeDb();
  for (const s of ['', '-wal', '-shm']) { try { fs.unlinkSync(dbPath + s); } catch {} }
});

test('getUserExportData assembles user data with money in dollars', () => {
  const data = getUserExportData(userId);
  assert.equal(data.bills.length, 1);
  assert.equal(data.bills[0].expected_amount, 1200, 'expected_amount is dollars (fromCents)');
  assert.equal(data.payments.length, 3);
  const amounts = data.payments.map(p => p.amount).sort((a, b) => a - b);
  assert.deepEqual(amounts, [85, 90, 95], 'payment amounts in dollars');
  assert.equal(data.metadata.counts.payments, 3);
});

test('payments export by year returns that year only', async () => {
  const { status, json } = await callExport({ year: '2026', format: 'json' });
  assert.equal(status, 200);
  assert.equal(json.count, 2, 'both 2026 payments');
  assert.equal(json.range, '2026');
});

test('payments export by date range filters to the range', async () => {
  const { status, json } = await callExport({ from: '2026-01-01', to: '2026-06-30', format: 'json' });
  assert.equal(status, 200);
  assert.equal(json.count, 1, 'only the Jan 10 payment');
  assert.equal(json.payments[0].paid_amount, 90);
});

test('CSV export sets a filename derived from the range', async () => {
  const { status, headers, body } = await callExport({ from: '2026-01-01', to: '2026-12-31', format: 'csv' });
  assert.equal(status, 200);
  assert.match(headers['Content-Disposition'], /bills-2026-01-01_to_2026-12-31\.csv/);
  assert.match(body, /^Date,Bill,Category/);
});

test('invalid date range is rejected', async () => {
  const bad = await callExport({ from: '2026-12-31', to: '2026-01-01' });
  assert.equal(bad.status, 400);
  const badFmt = await callExport({ from: 'nope', to: '2026-01-01' });
  assert.equal(badFmt.status, 400);
});
