<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class CompanyProfile extends Model
{
    protected $casts = [
        'is_active' => 'boolean',
    ];

    public function account()
    {
        return $this->belongsTo(Account::class);
    }

    public function companySpecialisations()
    {
        return $this->belongsToMany(
            CompanySpecialisation::class,
            'company_profile_company_specialisations'
        )->withPivot('position');
    }

    public function members()
    {
        return $this->hasMany(AccountMember::class, 'account_id', 'account_id');
    }

    public function jobOffers()
    {
        return $this->hasMany(JobOffer::class);
    }

    public function coordinationGroupMembers()
    {
        return $this->hasMany(CoordinationGroupMember::class);
    }

    public function resultEntities()
    {
        return $this->hasMany(ResultEntity::class);
    }

    public function projects()
    {
        return $this->hasMany(Project::class)->orderBy('project_date', 'desc');
    }

    public function offices()
    {
        return $this->hasMany(Office::class);
    }

    public function watchlists()
    {
        return $this->belongsToMany(Watchlist::class, 'watchlist_company_profiles', 'company_profile_id', 'watchlist_id');
    }

    // Prevents loading all watchlists when accessing the relation if not eager loaded with user filter
    public function getWatchlistsAttribute()
    {
        return $this->relationLoaded('watchlists') ? $this->getRelationValue('watchlists') : collect();
    }
}
