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

class Testimonial {
    private $db;

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

    // Add Testimonial
    public function add($data) {
        $stmt = $this->db->conn->prepare("INSERT INTO testimonials (title, short_description,image) VALUES (:title, :short_desc, :image)");
        $stmt->execute([
            ':title' => $data['title'],
            ':short_desc' => $data['short_description'],
            ':image' => $data['image']
        ]);
        return $this->db->conn->lastInsertId();
    }

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

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

    // Update Testimonial
    public function update($id, $data) {
        $stmt = $this->db->conn->prepare("UPDATE testimonials SET title = :title, short_description = :short_desc, image = :image WHERE id = :id");
        $stmt->execute([
            ':title' => $data['title'],
            ':short_desc' => $data['short_description'],
            ':image' => $data['image'],
            ':id' => $id
        ]);
        return true;
    }

    // Delete Testimonial
    public function delete($id) {
        $stmt = $this->db->conn->prepare("DELETE FROM testimonials WHERE id = :id");
        $stmt->execute([':id' => $id]);
        return true;
    }

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