<?php

namespace App\Models;

use App\Enums\PlayerLevel;
use App\Enums\ProfileVisibility;
use App\Support\PublicId;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class PlayerProfile extends Model
{
    use HasFactory, SoftDeletes;

    protected $fillable = [
        'public_id',
        'user_id',
        'display_name',
        'bio',
        'avatar_path',
        'cover_path',
        'country_code',
        'city',
        'timezone',
        'birth_date',
        'player_level',
        'looking_for_team',
        'profile_visibility',
        'profile_completion_percentage',
        'status',
    ];

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

    protected function casts(): array
    {
        return [
            'birth_date' => 'date',
            'looking_for_team' => 'boolean',
            'player_level' => PlayerLevel::class,
            'profile_visibility' => ProfileVisibility::class,
        ];
    }

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

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

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

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

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

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

    public function playerGameProfiles(): HasMany
    {
        return $this->gameProfiles();
    }

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

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