<?php

namespace App\Http\Controllers\Webhook;

use App\Http\Controllers\Controller;
use App\Services\Bot\BaleBotService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class BaleWebhookController extends Controller
{
    public function __invoke(Request $request, BaleBotService $botService): JsonResponse
    {
        $secret = (string) config('services.bale.webhook_secret');
        if ($secret !== '') {
            $incoming = (string) $request->header('X-Telegram-Bot-Api-Secret-Token', '');
            if ($incoming !== $secret) {
                return response()->json(['ok' => false, 'message' => 'unauthorized'], 403);
            }
        }

        $botService->handleUpdate($request->all());

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