<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class TravelSegment extends Model
{
    use HasFactory;

    const ‫‪STATE_OPEN‬‬ = 'O';
    const STATE_‫‪USED‬‬ = '‫‪U‬‬';
    const STATE_‫‪VOID‬‬ = 'V';
    const STATE_‫‪REFUNDED‬‬ = 'R';
    const STATE_‫‪EXCHANGED‬‬ = 'E';
    
    protected $fillable = [
        'booking_id',
        'cruise_id',
        'room_type_id',
        'segment_status'
    ];

    protected $attributes = [
        'segment_status' => self::‫‪STATE_OPEN‬‬,
    ];


    public function cruise()
    {
        return $this->belongsTo(Cruise::class);
    }

    public function tickets()
    {
        return $this->hasMany(Ticket::class,'segment_id','id');
    }

    public function services()
    {
        return $this->hasMany(SegmentService::class,'segment_id','id');
    }


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

    public function booking()
    {
        return $this->belongsTo(Booking::class)->withoutGlobalScopes();
    }
    
    
    function getTotalAttribute()
    {
      return $this->tickets->sum('total')+$this->services->sum('total');
    }

    protected static function booted()
    {

        static::addGlobalScope('status', function ($builder) {
            $builder->whereNotIn('segment_status', [self::STATE_‫‪EXCHANGED‬‬ ]);
        });
        

    }

}
