<?php

use App\Http\Controllers\ProfileController;
use App\Http\Controllers\TestController;
use App\Http\Controllers\TelegramWebhookController;
use App\Http\Controllers\AdminRequiredChatsController;
use App\Http\Controllers\AdminAdsController;
use App\Models\TelegramUser;
use Illuminate\Support\Facades\Route;
use Telegram\Bot\Laravel\Facades\Telegram;

Route::get('/', function () {
    return view('welcome');
});

// TelegramBot
Route::post('/telegram/webhook', [TelegramWebhookController::class, 'handle'])
    ->withoutMiddleware([\App\Http\Middleware\VerifyCsrfToken::class]);

Route::get('/tg-test', function () {
    Telegram::sendMessage([
        'chat_id' => 92493070,
        'text' => 'Laravel 12 + Telegram BOT работает 🚀+++++++'
    ]);
});

// End TelegramBot

Route::get('/test', [TestController::class, 'index']);

Route::get('/storage/app/videos/{filename}', function (string $filename) {
    if (!preg_match('~^[0-9a-fA-F-]{36}\.mp4$~', $filename)) {
        abort(404);
    }

    $path = storage_path('app/public/videos/' . $filename);
    if (!is_file($path)) {
        abort(404);
    }

    return response()->file($path, [
        'Content-Type' => 'video/mp4',
        'Content-Disposition' => 'inline; filename="' . $filename . '"',
    ]);
});

Route::get('/storage/app/{path}', function (string $path) {
    $path = trim($path);
    if ($path === '') {
        abort(404);
    }

    if (!preg_match('~^ads/[0-9A-Za-z._\-/]+\.(png|jpg|jpeg|webp)$~', $path)) {
        abort(404);
    }

    $absolute = storage_path('app/public/' . $path);
    if (!is_file($absolute)) {
        abort(404);
    }

    return response()->file($absolute);
})->where('path', '.*');

Route::get('/dashboard', function () {
    $users = TelegramUser::query()
        ->orderByDesc('downloads_count')
        ->orderByDesc('id')
        ->get();

    return view('dashboard', [
        'users' => $users,
    ]);
})->middleware(['auth', 'verified'])->name('dashboard');

Route::middleware('auth')->group(function () {
    Route::get('/admin/required-chats', [AdminRequiredChatsController::class, 'index'])
        ->name('admin.required-chats.index');
    Route::post('/admin/required-chats/settings', [AdminRequiredChatsController::class, 'updateSettings'])
        ->name('admin.required-chats.settings');
    Route::post('/admin/required-chats', [AdminRequiredChatsController::class, 'store'])
        ->name('admin.required-chats.store');
    Route::post('/admin/required-chats/{telegramRequiredChat}/toggle', [AdminRequiredChatsController::class, 'toggle'])
        ->name('admin.required-chats.toggle');
    Route::delete('/admin/required-chats/{telegramRequiredChat}', [AdminRequiredChatsController::class, 'destroy'])
        ->name('admin.required-chats.destroy');

    Route::get('/admin/ads', [AdminAdsController::class, 'index'])
        ->name('admin.ads.index');
    Route::post('/admin/ads', [AdminAdsController::class, 'store'])
        ->name('admin.ads.store');
    Route::post('/admin/ads/{telegramAd}/toggle', [AdminAdsController::class, 'toggle'])
        ->name('admin.ads.toggle');
    Route::delete('/admin/ads/{telegramAd}', [AdminAdsController::class, 'destroy'])
        ->name('admin.ads.destroy');

    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
});

require __DIR__.'/auth.php';
