// models/itemAliasModel.js
const { getDb } = require('../dbPool');

const ItemAliasModel = {
  async getAll() {
    const db = await getDb();
    const [rows] = await db.query(
      `SELECT ia.*, ci.item_name, ci.spec, ci.maker, ci.category
       FROM item_aliases ia
       JOIN consumable_items ci ON ia.item_id = ci.item_id
       ORDER BY ci.item_name, ia.alias_name`
    );
    return rows;
  },

  async create(itemId, aliasName) {
    const db = await getDb();
    const [result] = await db.query(
      `INSERT INTO item_aliases (item_id, alias_name) VALUES (?, ?)`,
      [itemId, aliasName.trim()]
    );
    return result.insertId;
  },

  async delete(aliasId) {
    const db = await getDb();
    const [result] = await db.query(
      `DELETE FROM item_aliases WHERE alias_id = ?`,
      [aliasId]
    );
    return result.affectedRows > 0;
  }
};

module.exports = ItemAliasModel;
