<?php

namespace App\Models;

use App\Models\Backoffice\MediaItem;
use App\Models\Backoffice\Support\ContentTypeModel;
use App\Models\Backoffice\Traits\LanguageSupportTrait;

class TrackedResult extends ContentTypeModel
{
    use LanguageSupportTrait;

    protected $casts = [
        'finished_date' => 'date',
        'updated_date' => 'date',
        'is_public' => 'bool',
    ];

    protected $hidden = ['languages', "websites"];
    protected $with = ['languages', "websites"];

    public function competition()
    {
        return $this->belongsTo(Competition::class);
    }

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

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

    public function images()
    {
        return $this->belongsToMany(MediaItem::class, 'tracked_result_images');
    }

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

    public function manufacturerGroups()
    {
        return $this->hasMany(ManufacturerGroup::class);
    }

    public function executingPartyGroups()
    {
        return $this->hasMany(ExecutingPartyGroup::class);
    }

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