<?php

namespace App\Models;

use App\Enums\ChampionshipStatusEnum;
use App\Traits\FilterableTrait;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\HasTranslations;

class Championship extends Model
{
    use HasFactory, FilterableTrait, HasTranslations;

    public array $translatable = ['name'];
    protected array $filterable = ['game_id', 'status'];
    protected array $searchable = ['name'];
    protected array $sortable = ['id', 'name', 'created_at'];

    protected $fillable = [
        'name',
        'image',
        'price',
        'game_id',
        'players_count',
        'status',
    ];

    protected function casts(): array
    {
        return [
            'game_id' => 'integer',
            'price' => 'decimal:2',
            'players_count' => 'integer',
            'status' => 'integer',
        ];
    }

    public function game()
    {
        return $this->belongsTo(Game::class);
    }

    public function teams()
    {
        return $this->hasMany(ChampionshipTeam::class);
    }

    public function groups()
    {
        return $this->hasMany(ChampionshipGroup::class);
    }

    public function matches()
    {
        return $this->hasMany(ChampionshipMatch::class);
    }

    public function banners()
    {
        return $this->morphMany(Banner::class, 'linkable');
    }

    public function scopeForIndex($query)
    {
        return $query->select([
            'id', 'name', 'image', 'price', 'game_id',
            'players_count', 'status', 'created_at',
        ]);
    }

    public function scopeRegistrationOpen($query)
    {
        return $query->where('status', ChampionshipStatusEnum::REGISTRATION_OPEN->value);
    }
}
