<?php

namespace App\Models;

use App\Traits\FilterByCurrency;
use App\Traits\hasCurrency;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Activitylog\LogOptions;


class Payement extends Model
{
    use HasFactory, hasCurrency, LogsActivity;

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

    protected $fillable=['booking_id','boarding_pass_id','currency_code','exchange_rate','amount','status'];
    protected $attributes = [
        'status' => self::STATUS_PENDING,
    ];

    protected $currency_attributes = ['amount'];

    public function booking(){
        return $this->belongsTo(Booking::class , 'booking_id' , 'id');
    }

    public function boardingPass(){
        return $this->belongsTo(BoardingPass::class , 'boarding_pass_id' , 'id');
    }

    protected static function booted()
    {
        static::addGlobalScope('office', function ($builder) {
            if(auth()->check() && !auth()->user()->isSuperAdmin()){
                if(auth()->user()->office_id){
                    $builder->whereHas('booking', function($query){
                        $query->where('office_id',auth()->user()->office_id);
                    });    
                }
                elseif(auth()->user()->admin_id){
                    $builder->whereHas('boardingPass', function($query){
                        $query->where('created_by',auth()->user()->admin_id);
                    });
                }
            }

        });
        

    }

        
    public function getActivitylogOptions():LogOptions
    {
        return LogOptions::defaults()
            ->logOnly(['status']);
    }

}
