<?php

namespace App\Models;

use App\Enums\WatchlistTypeEnum;
use App\Models\Backoffice\Support\ContentTypeModel;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class Watchlist extends ContentTypeModel
{
    protected $casts = [
        'is_default' => 'boolean',
    ];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function competitions(): BelongsToMany
    {
        return $this->belongsToMany(Competition::class, 'watchlist_competitions', 'watchlist_id', 'competition_id');
    }

    public function results(): BelongsToMany
    {
        return $this->belongsToMany(Result::class, 'watchlist_results', 'watchlist_id', 'result_id');
    }

    public function trackedResults(): BelongsToMany
    {
        return $this->belongsToMany(TrackedResult::class, 'watchlist_tracked_results', 'watchlist_id', 'tracked_result_id');
    }

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

    public function jobs(): BelongsToMany
    {
        return $this->belongsToMany(JobOffer::class, 'watchlist_job_offers', 'watchlist_id', 'job_offer_id');
    }

    public function getItemsRelationName(): string
    {
        return match ($this->type) {
            WatchlistTypeEnum::COMPETITIONS->value => 'competitions',
            WatchlistTypeEnum::RESULTS->value => 'results',
            WatchlistTypeEnum::TRACKED_RESULTS->value => 'trackedResults',
            WatchlistTypeEnum::COMPANY_PROFILES->value => 'companyProfiles',
            WatchlistTypeEnum::JOBS->value => 'jobs',
            default => 'competitions',
        };
    }
}
