<?php

namespace App\Http\Controllers;

use App\Models\PlayerProfile;
use App\Services\MockDataService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\View\View;

class ProfileController extends Controller
{
    public function show(Request $request, PlayerProfile $playerProfile): View
    {
        $this->authorize('view', $playerProfile);

        $playerProfile->load([
            'user',
            'socialLinks',
            'languages',
            'gameProfiles.game',
            'gameProfiles.currentRank',
            'records',
            'videos',
        ]);

        return view('pages.players.show', [
            'player' => $this->toPlayerArray($playerProfile, $request->user()),
            'feed' => array_slice(MockDataService::feed(), 0, 3),
        ]);
    }

    public function edit(Request $request): View
    {
        $profile = $request->user()->playerProfile;

        abort_if($profile === null, 404);

        $this->authorize('update', $profile);

        return view('pages.settings.index', [
            'tab' => 'account',
            'player' => $this->settingsPlayerData($request),
        ]);
    }

    /**
     * @return array<string, mixed>
     */
    private function toPlayerArray(PlayerProfile $profile, $viewer): array
    {
        $user = $profile->user;
        $isOwner = $viewer && $profile->user_id === $viewer->id;

        return [
            'slug' => $user->username,
            'username' => $profile->display_name,
            'real_name' => $user->display_name,
            'country' => $profile->country_code ?? $user->country_code,
            'country_name' => $profile->country_code ?? $user->country_code ?? '',
            'flag' => '',
            'languages' => $profile->languages->pluck('language_code')->all(),
            'avatar' => $this->profileAssetUrl($profile->avatar_path, '/assets/images/generated/players/player-1.webp'),
            'cover' => $this->profileAssetUrl($profile->cover_path, '/assets/images/generated/heroes/hero-arena.webp'),
            'bio' => $profile->bio ?? '',
            'verified' => false,
            'premium' => false,
            'online' => false,
            'availability' => 'available',
            'looking_for_team' => $profile->looking_for_team,
            'current_team' => null,
            'games' => $profile->gameProfiles->map(fn ($gameProfile) => [
                'game_id' => $gameProfile->game?->slug ?? '',
                'game_name' => $gameProfile->game?->name ?? '',
                'rank' => $gameProfile->currentRank?->name ?? '',
                'rank_color' => '#7C5CFC',
                'role' => '',
                'rp' => 0,
            ])->all(),
            'stats' => [
                'win_rate' => 0,
                'kd_ratio' => 0,
                'matches_played' => 0,
                'tournaments_won' => 0,
                'mvp_count' => 0,
                'points' => 0,
            ],
            'social' => $profile->socialLinks->mapWithKeys(fn ($link) => [
                $link->platform => $link->url,
            ])->all(),
            'joined' => $profile->created_at?->toDateString(),
            'last_active' => $profile->updated_at?->toDateString(),
            'is_owner' => $isOwner,
            'completion_percentage' => $profile->profile_completion_percentage,
        ];
    }

    /**
     * @return array<string, mixed>
     */
    private function settingsPlayerData(Request $request): array
    {
        $user = $request->user();
        $profile = $user->playerProfile;

        return [
            'username' => $user->username,
            'real_name' => $user->display_name,
            'bio' => $profile?->bio ?? '',
        ];
    }

    private function profileAssetUrl(?string $path, string $fallback): string
    {
        if (blank($path)) {
            return $fallback;
        }

        return Storage::disk('profiles')->url($path);
    }
}
