<?php

namespace App\Http\Controllers\Api\Messaging;

use App\Http\Controllers\Controller;
use App\Models\Conversation;
use App\Services\Messaging\MessagingPresenceService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class PresenceController extends Controller
{
    public function __construct(
        private readonly MessagingPresenceService $presence,
    ) {}

    public function heartbeat(Request $request): JsonResponse
    {
        $conversationId = $request->integer('conversation_id') ?: null;

        if ($conversationId) {
            $conversation = Conversation::query()->findOrFail($conversationId);
            $this->authorize('view', $conversation);
        }

        $this->presence->touch($request->user(), $conversationId);

        return response()->json([
            'ok' => true,
            'conversation_id' => $conversationId,
        ]);
    }
}
