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

async function upsertCard(card) {
  const {
    id,
    name,
    type,
    frameType,
    level,
    race,
    attribute,
    linkval,
    tcg_date,
    ocg_date
  } = card;

  const sql = `
    INSERT INTO cards (id, name, card_type, frame_type, level, race, attribute, link_val, tcg_date, ocg_date)
    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
    ON DUPLICATE KEY UPDATE
      name = VALUES(name),
      card_type = VALUES(card_type),
      frame_type = VALUES(frame_type),
      level = VALUES(level),
      race = VALUES(race),
      attribute = VALUES(attribute),
      link_val = VALUES(link_val),
      tcg_date = VALUES(tcg_date),
      ocg_date = VALUES(ocg_date)
  `;
  await db.execute(sql, [id, name, type, frameType, level, race, attribute, linkval, tcg_date, ocg_date]);
}

module.exports = { upsertCard };