<?php

namespace App\Models;

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

class Refund extends Model
{
    use HasFactory, hasCurrency;

    const STATUS_SUCCESS = 'success';
    const STATUS_PENDING = 'pending';

    protected $fillable = ['ticket_id', 'segment_id', 'amount', 'status',  'currency_code', 'exchange_rate'];

    protected $attributes = [
        'status' => self::STATUS_PENDING,
    ];

    protected $currency_attributes = ['amount'];

    public function ticket()
    {
        return $this->belongsTo(Ticket::class)->withoutGlobalScopes();
    }

    protected static function booted()
    {
   
        static::addGlobalScope('officeUser', function ($builder) {
            if(auth()->check() && auth()->user()->office && !auth()->user()->isSuperAdmin()){
                $builder->whereHas('ticket.segment', function($query){
                    $query->whereHas('booking', function($query){
                        $query->where('office_id',auth()->user()->office->id);
                    });                  
                });
            }
        });
        

    }
    
}
