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

class Bhajan {
  static async getAll() {
    const [rows] = await db.query('SELECT * FROM bhajans ORDER BY created_at DESC');
    return rows;
  }

  static async getLatest(limit = 5) {
    const [rows] = await db.query('SELECT * FROM bhajans ORDER BY created_at DESC LIMIT ?', [Number(limit)]);
    return rows;
  }

  static async create({ title, singer, audio_url, category, duration, description, lyrics }) {
    const [result] = await db.query(
      'INSERT INTO bhajans (title, singer, audio_url, category, duration, description, lyrics) VALUES (?, ?, ?, ?, ?, ?, ?)',
      [
        title,
        singer || 'Traditional',
        audio_url,
        category || 'Mantra',
        duration || null,
        description || null,
        lyrics || null
      ]
    );
    return {
      id: result.insertId,
      title,
      singer: singer || 'Traditional',
      audio_url,
      category: category || 'Mantra',
      duration,
      description,
      lyrics
    };
  }

  static async delete(id) {
    const [result] = await db.query('DELETE FROM bhajans WHERE id = ?', [id]);
    return result.affectedRows > 0;
  }
}

module.exports = Bhajan;
