const { setAmountOwned } = require('../models/collectionModel');

async function updateAmountOwned(req, res) {
  try {
    const { card_id, set_id, rarity_id, amount_owned } = req.body;

    if (
      card_id == null ||
      set_id == null ||
      rarity_id == null ||
      amount_owned == null
    ) {
      return res.status(400).json({ error: 'Missing required fields' });
    }

    if (amount_owned < 0) {
      return res.status(400).json({ error: 'Amount cannot be negative' });
    }

    const result = await setAmountOwned({
      card_id,
      set_id,
      rarity_id,
      amount_owned
    });

    res.json({
      message: 'Amount updated',
      ...result
    });

  } catch (err) {
    console.error('Error updating amount:', err);
    res.status(500).json({ error: 'Failed to update amount owned' });
  }
}

module.exports = { updateAmountOwned };