<?php

namespace App\Services;

use App\Models\Setting;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

class TelegramService
{
    /**
     * Send a notification message to Telegram.
     *
     * @param string $message Formatted HTML message content
     * @return bool Success status
     */
    public static function sendOrderNotification($message): bool
    {
        $token = Setting::get('telegram_bot_token');
        $chatId = Setting::get('telegram_chat_id');

        if (empty($token) || empty($chatId)) {
            Log::warning('Telegram notifications skipped: Bot token or Chat ID is empty in settings.');
            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' => false,
            ]);

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

            Log::error('Telegram API response error: ' . $response->body());
            return false;
        } catch (\Exception $e) {
            Log::error('Telegram notification exception: ' . $e->getMessage());
            return false;
        }
    }
}
