<?php

namespace App\Models;

use App\Enums\GameStatus;
use App\Enums\GameType;
use App\Support\PublicId;
use Database\Factories\GameFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class Game extends Model
{
    /** @use HasFactory<GameFactory> */
    use HasFactory, SoftDeletes;

    protected $fillable = [
        'public_id',
        'name',
        'slug',
        'short_name',
        'publisher',
        'description',
        'logo_path',
        'cover_path',
        'game_type',
        'status',
        'sort_order',
        'supports_ranks',
        'supports_roles',
        'supports_characters',
        'supports_regions',
        'supports_servers',
    ];

    public function getRouteKeyName(): string
    {
        return 'slug';
    }

    protected function casts(): array
    {
        return [
            'game_type' => GameType::class,
            'status' => GameStatus::class,
            'sort_order' => 'integer',
            'supports_ranks' => 'boolean',
            'supports_roles' => 'boolean',
            'supports_characters' => 'boolean',
            'supports_regions' => 'boolean',
            'supports_servers' => 'boolean',
        ];
    }

    protected static function booted(): void
    {
        static::creating(function (Game $game): void {
            if (empty($game->public_id)) {
                $game->public_id = PublicId::generate();
            }
        });
    }

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

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

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

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

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

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

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

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

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

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

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

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

    public function translationName(?string $locale = null): ?string
    {
        $locale ??= app()->getLocale();

        return $this->translations->firstWhere('locale', $locale)?->name ?? $this->name;
    }
}
