<?php
class ExtensionRequest
{
    private $conn;
    private $table_name = "extension_requests";

    public $id;
    public $booking_id;
    public $requested_duration;
    public $duration_type;
    public $status;
    public $message;

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

    public function create($booking_id, $requested_duration, $duration_type, $message = '')
    {
        $query = "INSERT INTO " . $this->table_name . " 
                  SET booking_id=:booking_id, requested_duration=:requested_duration, 
                      duration_type=:duration_type, message=:message";
        $stmt = $this->conn->prepare($query);

        $stmt->bindParam(":booking_id", $booking_id);
        $stmt->bindParam(":requested_duration", $requested_duration);
        $stmt->bindParam(":duration_type", $duration_type);
        $stmt->bindParam(":message", $message);

        if ($stmt->execute()) {
            $this->id = $this->conn->lastInsertId();
            return true;
        }
        return false;
    }

    public function updateStatus($id, $status)
    {
        $query = "UPDATE " . $this->table_name . " SET status=:status WHERE id=:id";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(":status", $status);
        $stmt->bindParam(":id", $id);
        return $stmt->execute();
    }

    public function getById($id)
    {
        $query = "SELECT * FROM " . $this->table_name . " WHERE id = :id LIMIT 0,1";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(":id", $id);
        $stmt->execute();
        return $stmt->fetch(PDO::FETCH_ASSOC);
    }
}
