<?php

namespace App\Models;

use App\Traits\FilterableTrait;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class ChampionshipGroup extends Model
{
    use HasFactory, FilterableTrait;

    protected $fillable = ['championship_id', 'name'];
    protected array $filterable = ['championship_id'];
    protected array $searchable = ['name'];
    protected array $sortable = ['id', 'name', 'created_at'];

    protected function casts(): array
    {
        return [
            'championship_id' => 'integer',
        ];
    }

    public function championship()
    {
        return $this->belongsTo(Championship::class);
    }

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

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

    public function scopeForIndex($query)
    {
        return $query->select(['id', 'championship_id', 'name', 'created_at']);
    }
}
