<?php

namespace App\Services\Notification;

use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

class TelegramService
{
    /**
     * Send a message to the configured Telegram Chat.
     */
    public function sendMessage(string $message): bool
    {
        $token = get_setting('telegram_bot_token');
        $chatId = get_setting('telegram_chat_id');

        if (! $token || ! $chatId) {
            Log::warning('Telegram Notification skipped: Bot Token or Chat ID not configured.');

            return false;
        }

        try {
            $response = Http::post("https://api.telegram.org/bot{$token}/sendMessage", [
                'chat_id' => $chatId,
                'text' => $message,
                'parse_mode' => 'HTML',
                'disable_web_page_preview' => true,
            ]);

            if ($response->successful()) {
                return true;
            }

            Log::error('Telegram API Error: '.$response->body());

            return false;
        } catch (\Exception $e) {
            Log::error('Telegram Service Exception: '.$e->getMessage());

            return false;
        }
    }
}
