<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Booking extends Model
{
    use \App\Models\Concerns\BelongsToHotel;

    protected $guarded = [];
    protected $casts = [
        'check_in' => 'date',
        'check_out' => 'date',
        'checked_in_at' => 'datetime',
        'checked_out_at' => 'datetime',
    ];

    public function customer()
    {
        return $this->belongsTo(Customer::class);
    }

    public function room()
    {
        return $this->belongsTo(Room::class);
    }

    public function roomType()
    {
        return $this->belongsTo(RoomType::class);
    }

    public function creator()
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    public function payments()
    {
        return $this->hasMany(Payment::class, 'payable_id')->where('payable_type', 'booking');
    }

    public function nights(): int
    {
        return max(1, (int) $this->check_in->diffInDays($this->check_out));
    }

    public function balance(): float
    {
        return (float) $this->total_amount - (float) $this->paid_amount;
    }
}
