<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Competition extends Model
{
    use HasFactory;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'description',
        'start_date',
        'status',
        'location',
        'max_teams',
        'current_scheduling_mode_id',
        'owner',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'start_date' => 'date',
        'end_date' => 'date',
    ];

    /**
     * Get the teams participating in the competition.
     */
    public function teams(): BelongsToMany
    {
        return $this->belongsToMany(Team::class)
            ->withTimestamps()
            ->withPivot('status');
    }

    /**
     * Get the scheduling modes available for this competition.
     */
    public function schedulingModes(): HasMany
    {
        return $this->hasMany(SchedulingMode::class);
    }

    /**
     * Get the current scheduling mode for this competition.
     */
    public function currentSchedulingMode()
    {
        return $this->belongsTo(SchedulingMode::class, 'current_scheduling_mode_id');
    }

    /**
     * Get the matches for this competition.
     */
    public function matches(): HasMany
    {
        return $this->hasMany(MatchGame::class);
    }

    /**
     * Get the break periods for this competition.
     */
    public function breakPeriods(): HasMany
    {
        return $this->hasMany(BreakPeriod::class);
    }

    /**
     * Get the fields used in this competition.
     */
    public function fields(): BelongsToMany
    {
        return $this->belongsToMany(Field::class)
            ->withTimestamps();
    }
}
