<?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 Team extends Model
{
    use HasFactory;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'logo',
        'nb_players',
        'contact_email',
        'contact_phone',
        'status',
    ];

    /**
     * Get the competitions this team is participating in.
     */
    public function competitions(): BelongsToMany
    {
        return $this->belongsToMany(Competition::class)
            ->withTimestamps()
            ->withPivot('status');
    }

    /**
     * Get the matches where this team is the home team.
     */
    public function homeMatches(): HasMany
    {
        return $this->hasMany(MatchGame::class, 'home_team_id');
    }

    /**
     * Get the matches where this team is the away team.
     */
    public function awayMatches(): HasMany
    {
        return $this->hasMany(MatchGame::class, 'away_team_id');
    }

    /**
     * Get all matches for this team (both home and away).
     */
    public function matches()
    {
        return $this->homeMatches->merge($this->awayMatches);
    }

    /**
     * Get the break periods assigned to this team.
     */
    public function breakPeriods(): BelongsToMany
    {
        return $this->belongsToMany(BreakPeriod::class)
            ->withTimestamps();
    }
}
