const db = require('../config/db');

async function upsertRarity(rarity) {
  const { rarity_name, rarity_code } = rarity;

  const sql = `
    INSERT INTO rarities (rarity_name, rarity_code)
    VALUES (?, ?)
    ON DUPLICATE KEY UPDATE
      rarity_code = VALUES(rarity_code)
  `;
  const [result] = await db.execute(sql, [rarity_name, rarity_code]);
  return result.insertId; // returns id for junction table
}

async function getRarityId(rarity_name) {
  const sql = `SELECT id FROM rarities WHERE rarity_name = ? LIMIT 1`;
  const [rows] = await db.execute(sql, [rarity_name]);
  return rows.length ? rows[0].id : null;
}

module.exports = { upsertRarity, getRarityId };