<?php

namespace App\Models;

use App\Support\PublicId;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class PlayerGameProfile extends Model
{
    use SoftDeletes;

    protected $fillable = [
        'public_id',
        'player_profile_id',
        'game_id',
        'in_game_name',
        'external_game_id',
        'current_rank_id',
        'highest_rank_id',
        'primary_platform_id',
        'region_id',
        'server_id',
        'years_of_experience',
        'skill_level',
        'biography',
        'is_primary_game',
        'is_public',
    ];

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

    protected function casts(): array
    {
        return [
            'years_of_experience' => 'integer',
            'is_primary_game' => 'boolean',
            'is_public' => 'boolean',
        ];
    }

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

    public function playerProfile(): BelongsTo
    {
        return $this->belongsTo(PlayerProfile::class);
    }

    public function game(): BelongsTo
    {
        return $this->belongsTo(Game::class);
    }

    public function currentRank(): BelongsTo
    {
        return $this->belongsTo(GameRank::class, 'current_rank_id');
    }

    public function highestRank(): BelongsTo
    {
        return $this->belongsTo(GameRank::class, 'highest_rank_id');
    }

    public function primaryPlatform(): BelongsTo
    {
        return $this->belongsTo(GamePlatform::class, 'primary_platform_id');
    }

    public function region(): BelongsTo
    {
        return $this->belongsTo(GameRegion::class, 'region_id');
    }

    public function server(): BelongsTo
    {
        return $this->belongsTo(GameServer::class, 'server_id');
    }

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

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

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

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

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

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