<?php
require_once 'admin/core/Database.php';

class Gallery {
    private $db;

    public function __construct() {
        $this->db = new Database();
    }

    public function add($data) {
        $stmt = $this->db->conn->prepare("INSERT INTO gallery_images (image) VALUES (:image)");
        $stmt->execute([':image' => $data['image']]);
    }

    public function update($id, $data) {
        $stmt = $this->db->conn->prepare("UPDATE gallery_images SET image = :image WHERE id = :id");
        $stmt->execute([
            ':image' => $data['image'],
            ':id' => $id
        ]);
    }

    public function delete($id) {
        $stmt = $this->db->conn->prepare("DELETE FROM gallery_images WHERE id = :id");
        $stmt->execute([':id' => $id]);
    }

    public function get($id) {
        $stmt = $this->db->conn->prepare("SELECT * FROM gallery_images WHERE id = :id LIMIT 1");
        $stmt->execute([':id' => $id]);
        return $stmt->fetch(PDO::FETCH_ASSOC);
    }

    public function getAll() {
        $stmt = $this->db->conn->query("SELECT * FROM gallery_images ORDER BY created_at DESC");
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    public function countAllGallery() {
        $stmt = $this->db->conn->prepare("SELECT COUNT(*) as total FROM gallery_images");
        $stmt->execute();
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        return $row ? $row['total'] : 0;
    }
}
