<?php

namespace App\Models;

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

class SegmentService extends Model
{
    use HasFactory, hasCurrency, Uniqid;

    protected $fillable = ['segment_id','ticket_id','details', 'article_id', 'car_id', 'price', 'quantity','ticket_number'];

    protected $casts = [
        'details' => 'array',
    ];

    protected $currency_attributes = ['price'];

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

    public function car()
    {
        return $this->belongsTo(Car::class, 'car_id');
    }
    public function ticket()
    {
        return $this->belongsTo(Ticket::class, 'ticket_id');
    }

    public function article()
    {
        return $this->belongsTo(Article::class, 'article_id');
    }

    function getSubjectAttribute()
    {
        if ($this->car_id) return $this->car;
        if ($this->article_id) return $this->article;
    }

    function getNameAttribute()
    {
        if ($this->car_id) return $this->car->name;
        if ($this->article_id) return $this->article->name;
    }

    function getTotalAttribute()
    {
        return $this->quantity * $this->price;
    }

    function getRatePriceAttribute()
    {
        return $this->price / $this->exchange_rate;
    }
    
    public function mix()
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'price' => $this->price,
            'quantity' => $this->quantity,
            'car_id' => $this->car_id,
            'article_id' => $this->article_id
        ];
    }
}
