<?php

declare(strict_types=1);

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Services\Activity\UserActivityService;
use App\Support\UsernamePolicy;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Models\User;

final class ProfileActivityController extends Controller
{
    public function __construct(private readonly UserActivityService $activities) {}

    public function __invoke(Request $request, string $username): JsonResponse
    {
        $normalized = UsernamePolicy::normalize($username);

        $user = User::query()
            ->with('profile:user_id,avatar_hash')
            ->whereRaw('LOWER(username) = ?', [$normalized])
            ->where('is_active', true)
            ->whereNull('deleted_at')
            ->firstOrFail();

        return response()->json(
            $this->activities->feedForUser(
                $user,
                (string) $request->query('filter', 'all'),
                (int) $request->query('page', 1),
                (int) $request->query('per_page', UserActivityService::DEFAULT_PER_PAGE),
            )
        );
    }
}
