<?php

namespace App\Models;

use App\Models\Traits\LanguageSupportTrait;
use Illuminate\Support\Facades\DB;

class Office extends ContentTypeModel
{
    use LanguageSupportTrait;

    protected $hidden = array('languages', "websites");
    protected $with = array("websites", "languages");

    public function contact()
    {
        return $this->belongsTo(Contact::class);
    }

    public function country()
    {
        return $this->belongsTo(Country::class);
    }

    public function scopeFilterByContinent($query, $continent)
    {
        return $query->whereExists(function ($query) use ($continent) {
            $query->select(DB::raw(1))
                ->from("countries")
                ->whereRaw("countries.id = offices.country_id")
                ->where("countries.continent", $continent);
        });
    }

    public function overviewImage()
    {
        return $this->belongsTo(MediaItem::class);
    }
}
