const { all, audit, now, one, run } = require('../db');

let cache = null;

function clearCache() {
  cache = null;
}

// Sorted ascending by tax_year, cached for the engine.
function rows() {
  if (!cache) cache = all('SELECT * FROM section_179_limits ORDER BY tax_year');
  return cache;
}

// Applicable limit for a placed-in-service year: the most recent year at or before it.
// Returns null when nothing is configured for/under the year, so the engine leaves §179 uncapped.
function getLimit(year) {
  const y = Number(year);
  if (!y) return null;
  let match = null;
  for (const row of rows()) {
    if (row.tax_year <= y) match = row;
    else break;
  }
  return match;
}

function fromRow(row) {
  return {
    id: row.id,
    tax_year: row.tax_year,
    base_limit: row.base_limit,
    phaseout_threshold: row.phaseout_threshold
  };
}

function list() {
  return all('SELECT * FROM section_179_limits ORDER BY tax_year DESC').map(fromRow);
}

function normalize(body) {
  return {
    tax_year: Math.trunc(Number(body.tax_year)) || 0,
    base_limit: Number(body.base_limit) || 0,
    phaseout_threshold: Number(body.phaseout_threshold) || 0
  };
}

function createLimit(body, userId) {
  const input = normalize(body);
  if (!input.tax_year) throw Object.assign(new Error('A valid tax year is required'), { status: 400 });
  if (one('SELECT id FROM section_179_limits WHERE tax_year = ?', [input.tax_year])) {
    throw Object.assign(new Error('A limit for that tax year already exists'), { status: 400 });
  }
  const ts = now();
  run(
    `INSERT INTO section_179_limits (tax_year, base_limit, phaseout_threshold, created_at, updated_at)
     VALUES (?, ?, ?, ?, ?)`,
    [input.tax_year, input.base_limit, input.phaseout_threshold, ts, ts]
  );
  clearCache();
  audit(userId, 'create', 'section_179_limit', String(input.tax_year), null, input);
  return fromRow(one('SELECT * FROM section_179_limits WHERE tax_year = ?', [input.tax_year]));
}

function updateLimit(taxYear, body, userId) {
  const before = one('SELECT * FROM section_179_limits WHERE tax_year = ?', [Number(taxYear)]);
  if (!before) return null;
  const input = normalize({ ...before, ...body, tax_year: before.tax_year });
  run(
    'UPDATE section_179_limits SET base_limit = ?, phaseout_threshold = ?, updated_at = ? WHERE tax_year = ?',
    [input.base_limit, input.phaseout_threshold, now(), before.tax_year]
  );
  clearCache();
  audit(userId, 'update', 'section_179_limit', String(before.tax_year), before, input);
  return fromRow(one('SELECT * FROM section_179_limits WHERE tax_year = ?', [before.tax_year]));
}

function deleteLimit(taxYear, userId) {
  const before = one('SELECT * FROM section_179_limits WHERE tax_year = ?', [Number(taxYear)]);
  if (!before) return false;
  run('DELETE FROM section_179_limits WHERE tax_year = ?', [before.tax_year]);
  clearCache();
  audit(userId, 'delete', 'section_179_limit', String(before.tax_year), before, null);
  return true;
}

module.exports = {
  clearCache,
  createLimit,
  deleteLimit,
  getLimit,
  list,
  updateLimit
};
