<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class JobOffer extends Model
{
    const STATUS_ACTIVE = 'active';
    const STATUS_INACTIVE = 'inactive';

    protected $casts = [
        'active_until' => 'date',
        'contact_by_email' => 'boolean',
        'contact_by_letter' => 'boolean',
    ];

    public function companyProfile(): BelongsTo
    {
        return $this->belongsTo(CompanyProfile::class);
    }

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

    public function specialisations(): BelongsToMany
    {
        return $this->belongsToMany(
            UserProfileSpecialisation::class,
            'job_offer_specialisations'
        );
    }

    public function watchlists(): BelongsToMany
    {
        return $this->belongsToMany(Watchlist::class, 'watchlist_job_offers', 'job_offer_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();
    }
}
