import db from '../config/db.js';

const Services = {
  async getAll() {
    const [rows] = await db.query('SELECT * FROM services ORDER BY sort_order ASC');
    return rows;
  },

  async getById(id) {
    const [rows] = await db.query('SELECT * FROM services WHERE id = ?', [id]);
    return rows[0] || null;
  },

  async create(data) {
    const { title, description, icon, sort_order } = data;
    const [result] = await db.query(
      'INSERT INTO services (title, description, icon, sort_order) VALUES (?,?,?,?)',
      [title, description || '', icon || 'Package', sort_order || 0]
    );
    return result.insertId;
  },

  async update(id, data) {
    const { title, description, icon, sort_order } = data;
    const [result] = await db.query(
      'UPDATE services SET title=?, description=?, icon=?, sort_order=? WHERE id=?',
      [title, description || '', icon || 'Package', sort_order || 0, id]
    );
    return result.affectedRows;
  },

  async remove(id) {
    const [result] = await db.query('DELETE FROM services WHERE id = ?', [id]);
    return result.affectedRows;
  },
};

export default Services;