<?php

namespace App\Http\Controllers;

use App\Models\PublicProfileSetting;
use App\Models\User;
use App\Services\DisciplineScoreService;
use Illuminate\View\View;

class PublicProfileController extends Controller
{
    public function show(string $publicUsername, DisciplineScoreService $disciplineScoreService): View
    {
        $settings = PublicProfileSetting::current();
        abort_unless($settings->public_profiles_enabled && $settings->allow_profile_sharing, 404);

        $user = User::query()
            ->where('public_username', $publicUsername)
            ->where('public_profile_enabled', true)
            ->firstOrFail();

        $latestChallenge = $user->challengeParticipants()->with('challenge')->latest()->first();

        return view('public-profile.show', [
            'profileUser' => $user,
            'discipline' => $settings->allow_discipline_visibility ? $disciplineScoreService->monthlyScore($user) : null,
            'totalTrades' => $user->trades()->count(),
            'latestChallenge' => $latestChallenge,
            'settings' => $settings,
        ]);
    }
}
