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

class Treatment {
    private $db;

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

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

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

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

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

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