<?php

require_once BASE_PATH . '/config/database.php';

class BookingStatusLog
{
    private PDO $db;

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

    public function create(
        int $bookingId,
        ?int $actorId,
        ?string $oldStatus,
        string $newStatus,
        ?string $note = null
    ): void {
        $stmt = $this->db->prepare(
            'INSERT INTO booking_status_logs (
                booking_id, actor_id, old_status, new_status, note
             ) VALUES (
                :booking_id, :actor_id, :old_status, :new_status, :note
             )'
        );
        $stmt->execute([
            'booking_id' => $bookingId,
            'actor_id' => $actorId,
            'old_status' => $oldStatus,
            'new_status' => $newStatus,
            'note' => $note
        ]);
    }

    public function getForBooking(int $bookingId): array
    {
        $stmt = $this->db->prepare(
            "SELECT l.*, u.full_name AS actor_name
             FROM booking_status_logs l
             LEFT JOIN users u ON u.id = l.actor_id
             WHERE l.booking_id = :booking_id
             ORDER BY l.created_at ASC, l.id ASC"
        );
        $stmt->execute(['booking_id' => $bookingId]);

        return $stmt->fetchAll();
    }
}
