<?php

namespace App\Models;

use App\Traits\hasCurrency;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class BoardingPass extends Model
{
    use HasFactory,hasCurrency;

    public $with = ['bags'];
    protected $fillable = ['ticket_id', 'room_id','user_id' ,'number_bags', 'weight_bags', 'amount', 'code'];
    
    protected $currency_attributes = ['amount'];

    public function ticket()
    {
        return $this->belongsTo('App\Models\Ticket', 'ticket_id', 'id');
    }

    public function room()
    {
        return $this->belongsTo('App\Models\Room', 'room_id', 'id');
    }

    public function bags()
    {
        return $this->hasMany(Bag::class, 'boarding_pass_id', 'id');
    }

    protected static function booted()
    {
        static::creating(function ($model) {
            if(auth()->check()){
                $model->created_by = auth()->user()->admin?auth()->user()->admin->id:auth()->id();
            }
            
        });
    }
}
