<?php

namespace App\Models;

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

class Booking extends Model
{
    protected $fillable = [
        'reference', 'user_id', 'name', 'facility_type', 'facility_id', 'facility_name',
        'date_start', 'date_end', 'time_start', 'time_end', 'persons_count',
        'visitor_type', 'payment_method', 'status', 'payment_status', 'total_amount',
        'rejection_note', 'notes', 'metadata', 'phone', 'instapay_sender_phone',
    ];

    protected $casts = [
        'date_start' => 'date',
        'date_end'   => 'date',
        'metadata'   => 'array',
    ];

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    public static function generateReference(): string
    {
        $max = static::max('id') ?? 0;
        return 'BK-' . str_pad($max + 1, 6, '0', STR_PAD_LEFT);
    }
}
