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

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

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

  async create(data) {
    const { name, category, image1, image2, description, material, sort_order } = data;
    const [result] = await db.query(
      'INSERT INTO showroom (name, category, image1, image2, description, material, sort_order) VALUES (?,?,?,?,?,?,?)',
      [name, category || '', image1 || '', image2 || '', description || '', material || '', sort_order || 0]
    );
    return result.insertId;
  },

  async update(id, data) {
    const { name, category, image1, image2, description, material, sort_order } = data;
    const [result] = await db.query(
      `UPDATE showroom SET
        name=?, category=?, image1=?, image2=?, description=?, material=?, sort_order=?
       WHERE id=?`,
      [name, category || '', image1 || '', image2 || '', description || '', material || '', sort_order || 0, id]
    );
    return result.affectedRows;
  },

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

export default Showroom;