<?php
class RentalPrice {
    private $conn;
    private $table_name = "rental_prices";

    public $id;
    public $item_id;
    public $client_id;
    public $start_date;
    public $end_date;
    public $daily_price;
    public $created_at;
    public $updated_at;

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

    public function create() {
        $query = "INSERT INTO " . $this->table_name . " SET item_id=:item_id, client_id=:client_id, start_date=:start_date, end_date=:end_date, daily_price=:daily_price, created_at=:created_at, updated_at=:updated_at";

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

        $this->item_id = htmlspecialchars(strip_tags($this->item_id));
        $this->client_id = htmlspecialchars(strip_tags($this->client_id));
        $this->start_date = htmlspecialchars(strip_tags($this->start_date));
        $this->end_date = htmlspecialchars(strip_tags($this->end_date));
        $this->daily_price = htmlspecialchars(strip_tags($this->daily_price));
        $this->created_at = date('Y-m-d H:i:s');
        $this->updated_at = date('Y-m-d H:i:s');

        $stmt->bindParam(":item_id", $this->item_id);
        $stmt->bindParam(":client_id", $this->client_id);
        $stmt->bindParam(":start_date", $this->start_date);
        $stmt->bindParam(":end_date", $this->end_date);
        $stmt->bindParam(":daily_price", $this->daily_price);
        $stmt->bindParam(":created_at", $this->created_at);
        $stmt->bindParam(":updated_at", $this->updated_at);

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

        return false;
    }

    public function read() {
        $query = "SELECT * FROM " . $this->table_name . " WHERE item_id = ? ORDER BY start_date ASC";

        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $this->item_id);
        $stmt->execute();

        return $stmt;
    }

    public function readOne() {
        $query = "SELECT * FROM " . $this->table_name . " WHERE id = ? LIMIT 0,1";

        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $this->id);
        $stmt->execute();

        $row = $stmt->fetch(PDO::FETCH_ASSOC);

        $this->item_id = $row['item_id'];
        $this->client_id = $row['client_id'];
        $this->start_date = $row['start_date'];
        $this->end_date = $row['end_date'];
        $this->daily_price = $row['daily_price'];
        $this->created_at = $row['created_at'];
        $this->updated_at = $row['updated_at'];
    }

    public function update() {
        $query = "UPDATE " . $this->table_name . " SET item_id=:item_id, client_id=:client_id, start_date=:start_date, end_date=:end_date, daily_price=:daily_price, updated_at=:updated_at WHERE id=:id";

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

        $this->item_id = htmlspecialchars(strip_tags($this->item_id));
        $this->client_id = htmlspecialchars(strip_tags($this->client_id));
        $this->start_date = htmlspecialchars(strip_tags($this->start_date));
        $this->end_date = htmlspecialchars(strip_tags($this->end_date));
        $this->daily_price = htmlspecialchars(strip_tags($this->daily_price));
        $this->updated_at = date('Y-m-d H:i:s');

        $stmt->bindParam(":item_id", $this->item_id);
        $stmt->bindParam(":client_id", $this->client_id);
        $stmt->bindParam(":start_date", $this->start_date);
        $stmt->bindParam(":end_date", $this->end_date);
        $stmt->bindParam(":daily_price", $this->daily_price);
        $stmt->bindParam(":updated_at", $this->updated_at);
        $stmt->bindParam(":id", $this->id);

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

        return false;
    }

    public function delete() {
        $query = "DELETE FROM " . $this->table_name . " WHERE id = ?";

        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $this->id);

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

        return false;
    }

    public function getCurrentPrice() {
        $query = "SELECT daily_price FROM " . $this->table_name . " WHERE item_id = ? AND start_date <= CURDATE() AND end_date >= CURDATE() LIMIT 1";

        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $this->item_id);
        $stmt->execute();

        $row = $stmt->fetch(PDO::FETCH_ASSOC);

        if($row) {
            return $row['daily_price'];
        }

        return null;
    }
}
?>
