<?php

class ProductRelease {
    private $conn;
    private $table_name = "product_releases";

    public $id;
    public $title;
    public $description;
    public $file_path;
    public $created_at;

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

    // Create a new product release record
    public function create() {
        $query = "INSERT INTO " . $this->table_name . "
                  SET
                    title=:title,
                    description=:description,
                    file_path=:file_path";

        $stmt = $this->conn->prepare($query);

        // Sanitize
        $this->title = htmlspecialchars(strip_tags($this->title));
        $this->description = htmlspecialchars(strip_tags($this->description));
        $this->file_path = htmlspecialchars(strip_tags($this->file_path));

        // Bind values
        $stmt->bindParam(":title", $this->title);
        $stmt->bindParam(":description", $this->description);
        $stmt->bindParam(":file_path", $this->file_path);

        if ($stmt->execute()) {
            return true;
        }

        return false;
    }

    // Read all product releases
    public function read() {
        $query = "SELECT
                    id, title, description, file_path, created_at
                  FROM
                    " . $this->table_name . "
                  ORDER BY
                    created_at DESC";

        $stmt = $this->conn->prepare($query);
        $stmt->execute();

        return $stmt;
    }

    // Delete a product release
    public function delete() {
        $query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
        $stmt = $this->conn->prepare($query);

        $this->id = htmlspecialchars(strip_tags($this->id));
        $stmt->bindParam(1, $this->id);

        if ($stmt->execute()) {
            return true;
        }

        return false;
    }
}
