import type { RowDataPacket, ResultSetHeader } from 'mysql2'
import { db, DB } from './db'

export interface GoldOption {
  id: number
  gold_amount: number
  price: number
}

export async function listGoldOptions(): Promise<GoldOption[]> {
  const [rows] = await db(DB.default).query<RowDataPacket[]>(
    'SELECT id, gold_amount, price FROM goldprice ORDER BY gold_amount',
  )
  return rows.map((r) => ({ id: r.id, gold_amount: r.gold_amount, price: Number(r.price) }))
}

export async function createGoldOption(goldAmount: number, price: number): Promise<number> {
  const [res] = await db(DB.default).query<ResultSetHeader>(
    'INSERT INTO goldprice (gold_amount, price) VALUES (?, ?)',
    [goldAmount, price],
  )
  return res.insertId
}

export async function deleteGoldOption(id: number): Promise<void> {
  await db(DB.default).query('DELETE FROM goldprice WHERE id = ?', [id])
}
