<?php

namespace App;

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

class Package extends Model
{
    use SoftDeletes;

    protected $fillable = ['image', 'price', 'available_users'];

    public function descriptions()
    {
        return $this->hasMany(PackageDescription::class);
    }

    public function users()
    {
        return $this->hasMany(User::class);
    }

    public static function withDescription($package_id = null)
    {
        $query = self::orderBy('id', 'asc')->join('package_descriptions as sd', 'sd.package_id', 'packages.id')
            ->where('sd.language_id', currentLanguage()->id);
        if ($package_id){
            $query->where('packages.id', $package_id);
        }

        $query->select(['packages.*', 'sd.name']);
        return $package_id ? $query->first() : $query->get();
    }
}
