<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Club extends Model
{
    use SoftDeletes;
    protected $table = 'clubes';
    protected $primaryKey = 'id_club';
    public $timestamps = true;
    public $incrementing = false;

    protected $fillable = [
        'id_club',
        'nombre',
        'es_seleccion',
        'estadio_id',
        'imagen',
        'qr_background',
        'qr_color_texto',
    ];

    protected static function boot()
    {
        parent::boot();
        static::creating(function ($model) {
            if (empty($model->id_club)) {
                $model->id_club = (int) self::withTrashed()->max('id_club') + 1;
            }
        });
    }

    protected $casts = [
        'id_club'       => 'integer',
        'es_seleccion'  => 'boolean',
    ];

    public function equipos()
    {
        return $this->hasMany(Equipo::class, 'id_club', 'id_club');
    }

    public function jugadores()
    {
        return $this->hasMany(Jugador::class, 'id_club_actual', 'id_club');
    }
}
