<?php

declare(strict_types=1);

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Services\ActivityService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

final class SocialActivityController extends Controller
{
    public function __construct(private readonly ActivityService $activity) {}

    public function index(Request $request): JsonResponse
    {
        $filter = (string) $request->query('filter', 'all');

        if ($this->activity->requiresAuthentication($filter) && ! $request->user()) {
            return response()->json(['error' => 'Unauthenticated'], 401);
        }

        return response()->json(
            $this->activity->communityFeed(
                viewer: $request->user(),
                filter: $filter,
                page: (int) $request->query('page', 1),
                perPage: (int) $request->query('per_page', 20),
                actorUserId: $request->filled('user_id') ? (int) $request->query('user_id') : null,
            )
        );
    }
}