<?php

namespace App\Http\Controllers;

use App\Helpers\XenForoHelpers;
use App\Services\ActivityService;
use App\Services\XenforoApiService;
use App\Services\XenforoService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class DynamicLoadController extends Controller
{
    public function hovercard( Request $request, int $user_id ){

        $data = Cache::remember("xf_hovercard_{$user_id}", 300, function() use($user_id){

            $service = app(XenforoService::class);
            $user = $service->getXfUser( $user_id );

            if( !$user ){
                return response()->json(['error' => 'User not found'], 404);
            }

            return [
                'user_id' => $user_id,
                'username' => $user->username,
                'avatar_url' => $user->getAvatarUrl(),
                'avatar_color' => XenForoHelpers::getAvatarColor( $user ),
                'avatar_letter' => XenForoHelpers::getAvatarLetter( $user ),
                'group_name' => $service->getXfUserGroup( $user?->user_group_id ?? 0 )?->title ?? 'Guest',
                'joined' => \DateTimeImmutable::createFromTimestamp( $user->register_date ?? 0 )->format('Y-m-d'),
                'last_seen' => \DateTimeImmutable::createFromTimestamp( $user->last_activity ?? 0 )->format('Y-m-d'),
                'message_count' => $user->message_count,
                'reaction_score' => $user->reaction_score,
                'trophy_points' => $user->trophy_points,
                'entries_count' => $user->rhpz_entry_count,
            ];

        });


        return response()->json( ['user' => $data] );
    }

    public function getNotifications( Request $request ){

        $service = app(XenforoApiService::class);
        $data = $service->getUserAlerts(\Auth::user()->user_id);

        return response()->json( $data );
    }

    public function markAllRead( Request $request ){
        $service = app(XenforoApiService::class);
        $service->markAllNotificationsRead(\Auth::user()->user_id);

        return response()->json( ['success' => true] );
    }

    public function getConversations( Request $request ){
        $service = app(XenforoApiService::class);
        $data = $service->getConversations(\Auth::user()->user_id);

        return response()->json( $data );
    }

    public function activityFeed(Request $request): JsonResponse
    {
        $availableFilters = ['entries', 'news', 'messages', 'threads', 'clubs', 'reviews'];

        $requested = $request->query('filters')
            ? explode(',', $request->query('filters'))
            : [];

        $activeFilters = !empty($requested)
            ? array_intersect($requested, $availableFilters)
            : $availableFilters;

        $service = app(ActivityService::class);
        $items = $service->getActivities(array_values($activeFilters));

        return response()->json([
            'html' => view('activity.timeline', compact('items'))->render(),
        ]);
    }
}
