<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class CustomerPassenger extends Model
{
    use SoftDeletes;

    protected $guarded = [];

    protected $casts = [
        'birth_date' => 'date',
        'passport_expiry' => 'date',
        'visa_expiry' => 'date',
    ];

    public function getFullNameAttribute()
    {
        return trim("{$this->first_name} {$this->last_name}");
    }

    public function getInitialsAttribute()
    {
        $f = mb_substr($this->first_name ?? '', 0, 1);
        $l = mb_substr($this->last_name ?? '', 0, 1);
        return strtoupper("{$f}{$l}");
    }

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

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

    /** Bu yolcunun katıldığı satışlar */
    public function sales()
    {
        return $this->belongsToMany(\App\Models\Sale\Sale::class, 'sale_passengers', 'passenger_id', 'sale_id')
            ->withPivot(['room_number', 'seat_number', 'special_request'])
            ->withTimestamps();
    }
}
