<?php
class Booking
{
    private $conn;
    private $table_name = "bookings";

    public $id;
    public $guesthouse_id;
    public $user_id;
    public $status;
    public $entry_code;
    public $entry_time;
    public $exit_time;
    public $total_price;
    public $stay_type;
    public $stay_duration;
    public $payment_method;
    public $payment_phone;
    public $created_at;
    public $updated_at;

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

    public function create()
    {
        $query = "INSERT INTO " . $this->table_name . "
                SET
                    guesthouse_id=:guesthouse_id,
                    user_id=:user_id,
                    status=:status,
                    entry_code=:entry_code,
                    entry_time=:entry_time,
                    exit_time=:exit_time,
                    total_price=:total_price,
                    stay_type=:stay_type,
                    stay_duration=:stay_duration,
                    payment_method=:payment_method,
                    payment_phone=:payment_phone";

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

        $this->status = "Aguardando Entrada";
        $this->entry_code = $this->generateCode();

        $stmt->bindParam(":guesthouse_id", $this->guesthouse_id);
        $stmt->bindParam(":user_id", $this->user_id);
        $stmt->bindParam(":status", $this->status);
        $stmt->bindParam(":entry_code", $this->entry_code);
        $stmt->bindParam(":entry_time", $this->entry_time);
        $stmt->bindParam(":exit_time", $this->exit_time);
        $stmt->bindParam(":total_price", $this->total_price);
        $stmt->bindParam(":stay_type", $this->stay_type);
        $stmt->bindParam(":stay_duration", $this->stay_duration);
        $stmt->bindParam(":payment_method", $this->payment_method);
        $stmt->bindParam(":payment_phone", $this->payment_phone);

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

    private function generateCode()
    {
        $chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $code = "AKI-";
        for ($i = 0; $i < 4; $i++) {
            $code .= $chars[rand(0, strlen($chars) - 1)];
        }
        $code .= "-";
        for ($i = 0; $i < 2; $i++) {
            $code .= $chars[rand(0, strlen($chars) - 1)];
        }
        return $code;
    }

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

    public function updateStatus($new_status)
    {
        $query = "UPDATE " . $this->table_name . " SET status = :status WHERE id = :id";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(":status", $new_status);
        $stmt->bindParam(":id", $this->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);
    }
}
?>