<?php

namespace Modules\Bms\Entities;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * Append-only audit trail for a booking's money + lifecycle path
 * (confirmed / contracted / completed / cancelled / rescheduled / payment /
 * refund / note). Never updated or deleted — the booking's history of record.
 */
class BmsBookingEvent extends Model
{
    protected $table = 'bms_booking_events';

    protected $fillable = [
        'business_id', 'booking_id', 'type', 'payload', 'user_id',
    ];

    protected $casts = [
        'payload' => 'array',
    ];

    public function booking(): BelongsTo
    {
        return $this->belongsTo(BmsBooking::class, 'booking_id');
    }

    /** Record an event in one call. */
    public static function record(BmsBooking $booking, string $type, array $payload = [], ?int $userId = null): self
    {
        return static::create([
            'business_id' => (int) $booking->business_id,
            'booking_id'  => $booking->id,
            'type'        => $type,
            'payload'     => $payload,
            'user_id'     => $userId,
        ]);
    }
}
