<?php

declare(strict_types=1);

namespace App\Http\Middleware;

use App\Services\GroupService;
use Illuminate\Http\Request;
use Inertia\Middleware;

final class HandleInertiaRequests extends Middleware
{
    protected $rootView = 'upload';

    protected function canReadSessionAuth(Request $request): bool
    {
        if ($request->attributes->get('skinbase.session_skipped') === true) {
            return false;
        }

        return $request->hasSession();
    }

    /**
     * Select the root Blade view based on route prefix.
     */
    public function rootView(Request $request): string
    {
        if ($request->path() === 'leaderboard') {
            return 'leaderboard';
        }

        if (str_starts_with($request->path(), 'studio')) {
            return 'studio';
        }

        // Profile pages: /@{username}
        if (str_starts_with($request->path(), '@')) {
            return 'profile.show';
        }

        // Feed pages — ordered most-specific first
        if ($request->path() === 'feed/trending') {
            return 'feed.trending';
        }

        if ($request->path() === 'feed/saved') {
            return 'feed.saved';
        }

        if (str_starts_with($request->path(), 'feed')) {
            return 'feed.following';
        }

        // Hashtag pages: /tags/{tag}
        if (str_starts_with($request->path(), 'tags/')) {
            return 'feed.hashtag';
        }

        return $this->rootView;
    }

    public function version(Request $request): ?string
    {
        return parent::version($request);
    }

    public function share(Request $request): array
    {
        $canReadSessionAuth = $this->canReadSessionAuth($request);
        $user = $canReadSessionAuth ? $request->user() : null;

        return array_merge(parent::share($request), [
            'auth' => [
                'user' => $user ? [
                    'id' => $user->id,
                    'name' => $user->name,
                    'is_admin' => $user->isAdmin(),
                    'is_moderator' => $user->isModerator(),
                ] : null,
            ],
            'cdn' => [
                'files_url' => config('cdn.files_url'),
            ],
            'features' => [
                'groups' => (bool) config('features.groups', true),
                'groups_v1' => (bool) config('features.groups_v1', true),
                'groups_v2' => (bool) config('features.groups_v2', true),
                'group_posts' => (bool) config('features.group_posts', true),
                'group_recruitment' => (bool) config('features.group_recruitment', true),
                'group_join_requests' => (bool) config('features.group_join_requests', true),
                'group_review_queue' => (bool) config('features.group_review_queue', true),
                'group_projects' => (bool) config('features.group_projects', true),
                'group_challenges' => (bool) config('features.group_challenges', true),
                'group_events' => (bool) config('features.group_events', true),
                'group_assets' => (bool) config('features.group_assets', true),
                'group_activity_feed' => (bool) config('features.group_activity_feed', true),
            ],
            'studio_groups' => $user
                ? app(GroupService::class)->studioOptionsForUser($user)
                : [],
        ]);
    }
}
