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

class GalleryPhoto {
  static async getByGalleryId(galleryId) {
    const [rows] = await db.query(
      'SELECT * FROM gallery_photos WHERE gallery_id = ? ORDER BY display_order ASC',
      [galleryId]
    );
    return rows;
  }

  static async create(data) {
    const { gallery_id, photo_url, title, description, display_order } = data;

    const [result] = await db.query(
      `INSERT INTO gallery_photos (gallery_id, photo_url, title, description, display_order)
       VALUES (?, ?, ?, ?, ?)`,
      [gallery_id, photo_url, title || null, description || null, display_order || 0]
    );

    return result.insertId;
  }

  static async update(id, data) {
    const { photo_url, title, description, display_order } = data;

    const [result] = await db.query(
      `UPDATE gallery_photos SET
        photo_url = ?, title = ?, description = ?, display_order = ?
       WHERE id = ?`,
      [photo_url, title, description, display_order, id]
    );

    return result.affectedRows > 0;
  }

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

  static async deleteByGalleryId(galleryId) {
    const [result] = await db.query('DELETE FROM gallery_photos WHERE gallery_id = ?', [galleryId]);
    return result.affectedRows > 0;
  }
}

module.exports = GalleryPhoto;
