// Standing warnings (separate from mod_actions so /warnings can list a
// user's active warnings). expires_at is always NULL for now — decay/escalation
// (e.g. "3 active warns -> auto-mute") is deferred past Phase 2, see
// warn.command.js.
const db = require('../db')

async function add({ guildId, targetUserId, targetTag, staffUserId, staffTag, reason }) {
  await db.query(
    `INSERT INTO warnings (guild_id, target_user_id, target_tag, staff_user_id, staff_tag, reason)
     VALUES (?, ?, ?, ?, ?, ?)`,
    [guildId, targetUserId, targetTag || null, staffUserId, staffTag || null, reason || null],
  )
}

// Active = not expired. Every row is active today since expires_at is never
// set, but the query is written to already respect it once decay lands.
async function listActive(guildId, targetUserId) {
  return db.query(
    `SELECT id, reason, staff_tag, created_at FROM warnings
     WHERE guild_id = ? AND target_user_id = ? AND (expires_at IS NULL OR expires_at > NOW())
     ORDER BY created_at DESC`,
    [guildId, targetUserId],
  )
}

module.exports = { add, listActive }
