import { getDb } from './db.js';

export function listSuppliers() {
  return getDb().prepare(`SELECT * FROM suppliers ORDER BY name`).all();
}

export function getSupplier(id) {
  return getDb().prepare(`SELECT * FROM suppliers WHERE id = ?`).get(id);
}

export function createSupplier(input) {
  const stmt = getDb().prepare(`
    INSERT INTO suppliers (name, phone, address, notes)
    VALUES (@name, @phone, @address, @notes)
  `);
  const result = stmt.run({
    name:    (input.name || '').trim(),
    phone:   input.phone?.trim()   || null,
    address: input.address?.trim() || null,
    notes:   input.notes?.trim()   || null
  });
  return result.lastInsertRowid;
}

export function deleteSupplier(id) {
  getDb().prepare(`DELETE FROM suppliers WHERE id = ?`).run(id);
}
