<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Club extends Model
{
    protected $fillable = [
        'name', 'type', 'short_name', 'logo', 'nationality', 'overall', 'is_active',
    ];

    protected $casts = [
        'is_active' => 'boolean',
        'overall'   => 'integer',
    ];

    const TYPE_CLUB     = 'club';
    const TYPE_NATIONAL = 'national';

    public function isNational(): bool
    {
        return $this->type === self::TYPE_NATIONAL;
    }

    public function isClub(): bool
    {
        return $this->type === self::TYPE_CLUB;
    }

    public function getTypeLabelAttribute(): string
    {
        return $this->type === self::TYPE_NATIONAL ? '🌍 Timnas' : '🏟️ Klub';
    }

    public function getLogoUrlAttribute(): string
    {
        if ($this->logo) {
            return asset('storage/' . $this->logo);
        }
        $bg = $this->type === self::TYPE_NATIONAL ? '16a34a' : '1d4ed8';
        return 'https://ui-avatars.com/api/?name='
            . urlencode($this->short_name ?? $this->name)
            . '&background=' . $bg . '&color=fff&size=64&bold=true&length=3';
    }

    public function bans(): HasMany
    {
        return $this->hasMany(TournamentMatchBan::class);
    }
}
