<?php

namespace Tests\Unit;

use Exception;
use App\Http\Controllers\WebhookController;
use App\NotifyMe;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Tests\TestCase;

// Create a test class that uses the trait
class TestNotifier
{
    use NotifyMe;
}

class NotifyMeTest extends TestCase
{
    public $notifier;
    protected function setUp(): void
    {
        parent::setUp();

        $this->notifier = new TestNotifier;
    }

    /** @test */
    public function it_sends_telegram_notification_successfully(): void
    {
        Config::set('app.notify_tg_bot_token', 'test_bot_token');
        Config::set('app.notify_tg_chat_id', 'test_chat_id');

        Http::fake([
            'https://api.telegram.org/bottest_bot_token/sendMessage' => Http::response([
                'ok' => true,
                'result' => ['message_id' => 123],
            ], 200),
        ]);

        $result = $this->notifier->sendTelegramNotification('Test message');

        $this->assertTrue($result);
        Http::assertSent(fn(array $request): bool => $request->url() === 'https://api.telegram.org/bottest_bot_token/sendMessage' &&
               $request['chat_id'] === 'test_chat_id' &&
               $request['text'] === 'Test message' &&
               $request['parse_mode'] === 'HTML');
    }

    /** @test */
    public function it_fails_when_bot_token_is_not_configured(): void
    {
        Config::set('app.notify_tg_bot_token');
        Config::set('app.notify_tg_chat_id', 'test_chat_id');

        Log::shouldReceive('error')
            ->once()
            ->with('Telegram bot token or chat ID not configured');

        $result = $this->notifier->sendTelegramNotification('Test message');

        $this->assertFalse($result);
    }

    /** @test */
    public function it_fails_when_chat_id_is_not_configured(): void
    {
        Config::set('app.notify_tg_bot_token', 'test_bot_token');
        Config::set('app.notify_tg_chat_id');

        Log::shouldReceive('error')
            ->once()
            ->with('Telegram bot token or chat ID not configured');

        $result = $this->notifier->sendTelegramNotification('Test message');

        $this->assertFalse($result);
    }

    /** @test */
    public function it_handles_http_errors_gracefully(): void
    {
        Config::set('app.notify_tg_bot_token', 'test_bot_token');
        Config::set('app.notify_tg_chat_id', 'test_chat_id');

        Http::fake([
            'https://api.telegram.org/bottest_bot_token/sendMessage' => Http::response([
                'ok' => false,
                'error_code' => 400,
                'description' => 'Bad Request',
            ], 400),
        ]);

        $result = $this->notifier->sendTelegramNotification('Test message');

        $this->assertFalse($result);
    }

    /** @test */
    public function it_handles_network_exceptions(): void
    {
        Config::set('app.notify_tg_bot_token', 'test_bot_token');
        Config::set('app.notify_tg_chat_id', 'test_chat_id');

        Http::fake([
            'https://api.telegram.org/bottest_bot_token/sendMessage' => Http::throw(function ($request): void {
                throw new Exception('Network error');
            }),
        ]);

        Log::shouldReceive('error')
            ->once();

        $result = $this->notifier->sendTelegramNotification('Test message');

        $this->assertFalse($result);
    }

    /** @test */
    public function it_sends_messages_with_html_parsing_mode(): void
    {
        Config::set('app.notify_tg_bot_token', 'test_bot_token');
        Config::set('app.notify_tg_chat_id', 'test_chat_id');

        Http::fake([
            'https://api.telegram.org/bottest_bot_token/sendMessage' => Http::response([
                'ok' => true,
                'result' => ['message_id' => 123],
            ], 200),
        ]);

        $htmlMessage = '<b>Bold text</b> and <i>italic text</i>';
        $this->notifier->sendTelegramNotification($htmlMessage);

        Http::assertSent(fn(array $request): bool => $request['parse_mode'] === 'HTML' &&
               $request['text'] === $htmlMessage);
    }

    /** @test */
    public function it_can_be_used_in_controller_context(): void
    {
        Config::set('app.notify_tg_bot_token', 'test_bot_token');
        Config::set('app.notify_tg_chat_id', 'test_chat_id');

        Http::fake([
            'https://api.telegram.org/bottest_bot_token/sendMessage' => Http::response([
                'ok' => true,
                'result' => ['message_id' => 123],
            ], 200),
        ]);

        $controller = new WebhookController;
        $result = $controller->sendTelegramNotification('Test from controller');

        $this->assertTrue($result);
    }
}
