<?php

namespace App\Models;

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

class Service extends Model
{
    use HasFactory, Currencies;

    protected $fillable = ['ship_id', 'origin_port', 'destination_port', 'qty', 'room_type_id', 'car_id', 'article_id', 'price','price_two','ticket_id'];
    
    protected $attributes = [
        'qty' => 1,
        'price_two'=> 0
    ];

    public function ship()
    {
        return $this->belongsTo(Ship::class);
    }

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


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

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

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

    public function mix()
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'price' => $this->convertCurrency($this->price),
            'price_two' => $this->convertCurrency($this->price_two),
            'capacity' => $this->qty,
            'car_id' => $this->car_id,
            'article_id' => $this->article_id
        ];
    }
}
