<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;

use App\Http\Controllers\AuthController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\UserDomainController;
use App\Http\Controllers\FundController;

use App\Http\Controllers\Admin\AdminAuthController;
use App\Http\Controllers\Admin\AdminUserController;
use App\Http\Controllers\Admin\AdminDomainController;
use App\Http\Controllers\Admin\AdminOrderController;
use App\Http\Controllers\Admin\AdminSettingController;

Route::get('/', function () {
    return view('welcome');
})->name('home');

Route::get('/terms', function () {
    return view('terms');
})->name('terms');

Route::get('/check-domain-live', function(Request $request) {
    $domain = strtolower(trim(preg_replace('/[^a-zA-Z0-9-]/', '', $request->domain)));

    if (empty($domain)) return response()->json([]);

    $extensions = \Illuminate\Support\Facades\DB::table('subdomain_settings')
        ->where('status', 'active')
        ->get();

    $results = [];

    foreach($extensions as $ext) {
        $fullDomain = $domain . '.' . $ext->domain_name;

        $isTaken = \Illuminate\Support\Facades\DB::table('user_domains')
            ->where('domain_name', $fullDomain)
            ->exists();

        $results[] = [
            'id' => $ext->id,
            'extension' => $ext->domain_name,
            'full_domain' => $fullDomain,
            'price' => $ext->price_per_month,
            'available' => !$isTaken
        ];
    }

    return response()->json($results);
})->name('domain.check.live');

Route::post('/telegram/webhook', [\App\Http\Controllers\TelegramBotController::class, 'handleWebhook']);

Route::get('/lang/{locale}', function ($locale) {
    session(['locale' => $locale]);
    return back();
})->name('lang.switch');

Route::middleware('guest')->group(function () {
    Route::get('/login', [AuthController::class, 'showLogin'])->name('login');
    Route::post('/login', [AuthController::class, 'loginPost'])->name('login.post');
    Route::get('/register', [AuthController::class, 'showRegister'])->name('register');
    Route::post('/register', [AuthController::class, 'registerPost'])->name('register.post');
    Route::get('/forgot-password', [AuthController::class, 'showForgotForm'])->name('password.request');
    Route::post('/forgot-password', [AuthController::class, 'sendResetLink'])->name('password.email');
    Route::get('/reset-password/{token}', [AuthController::class, 'showResetForm'])->name('password.reset');
    Route::post('/reset-password', [AuthController::class, 'resetPassword'])->name('password.update');
    Route::get('/auth/google', [AuthController::class, 'redirectToGoogle'])->name('google.login');
    Route::get('/auth/google/callback', [AuthController::class, 'handleGoogleCallback']);
});

Route::get('/email/verify', [AuthController::class, 'showVerifyEmail'])->name('verification.notice');
Route::post('/email/verification-notification', [AuthController::class, 'resendVerificationEmail'])->name('verification.send');
Route::get('/email/verify/{token}', [AuthController::class, 'verifyEmail'])->name('verification.verify');

Route::get('/2fa/verify', [AuthController::class, 'show2faForm'])->name('2fa.verify');
Route::post('/2fa/verify', [AuthController::class, 'verify2fa'])->name('2fa.verify.post');

Route::middleware('auth')->group(function () {
    Route::get('/dashboard', [UserController::class, 'dashboard'])->name('dashboard');
    Route::post('/logout', [AuthController::class, 'logout'])->name('logout');

    Route::get('/buy-domain', [UserDomainController::class, 'buyDomain'])->name('user.domain.buy');
    Route::post('/buy-domain/check', [UserDomainController::class, 'checkDomain'])->name('user.domain.check');
    Route::post('/buy-domain/purchase', [UserDomainController::class, 'purchaseDomain'])->name('user.domain.purchase');

    Route::get('/my-domains', [UserDomainController::class, 'myDomains'])->name('user.domain.index');
    Route::get('/my-domains/{id}/manage', [UserDomainController::class, 'manageDomain'])->name('user.domain.manage');
    Route::post('/my-domains/{id}/dns', [UserDomainController::class, 'updateDNS'])->name('user.domain.dns.update');
    Route::post('/my-domains/{id}/renew', [UserDomainController::class, 'renewDomain'])->name('user.domain.renew');

    Route::get('/add-fund', [FundController::class, 'index'])->name('user.fund.index');
    Route::post('/add-fund/pay', [FundController::class, 'payNow'])->name('user.fund.pay');
    Route::get('/add-fund/callback', [FundController::class, 'payfastbdCallback'])->name('user.fund.callback');

    Route::get('/profile', [UserController::class, 'profile'])->name('user.profile');
    Route::post('/profile/update', [UserController::class, 'updateProfile'])->name('user.profile.update');
    Route::post('/profile/password', [UserController::class, 'updatePassword'])->name('user.profile.password');

    Route::get('/2fa-setup', [UserController::class, 'show2faSetup'])->name('user.2fa.setup');
    Route::post('/2fa/enable', [UserController::class, 'enable2fa'])->name('user.2fa.enable');
    Route::post('/2fa/disable', [UserController::class, 'disable2fa'])->name('user.2fa.disable');
});

Route::prefix('admin')->name('admin.')->group(function () {

    Route::get('/login', [AdminAuthController::class, 'showLogin'])->name('login');
    Route::post('/login', [AdminAuthController::class, 'loginPost'])->name('login.post');
    Route::get('/2fa/verify', [AdminAuthController::class, 'show2faForm'])->name('2fa.verify');
    Route::post('/2fa/verify', [AdminAuthController::class, 'verify2fa'])->name('2fa.verify.post');

    Route::middleware(['auth:admin', 'admin.2fa'])->group(function () {

        Route::get('/dashboard', function () {
            return view('admin.dashboard');
        })->name('dashboard');

        Route::post('/logout', [AdminAuthController::class, 'logout'])->name('logout');

        Route::get('/users', [AdminUserController::class, 'index'])->name('users.index');
        Route::get('/users/{id}', [AdminUserController::class, 'show'])->name('users.show');
        Route::post('/users/{id}/balance', [AdminUserController::class, 'addBalance'])->name('users.balance');
        Route::post('/users/{id}/impersonate', [AdminUserController::class, 'impersonate'])->name('users.impersonate');

        Route::get('/domains', [AdminDomainController::class, 'index'])->name('domains.index');
        Route::get('/domains/{id}', [AdminDomainController::class, 'show'])->name('domains.show');
        Route::post('/domains/{id}/status', [AdminDomainController::class, 'updateStatus'])->name('domains.status');

        Route::get('/orders', [AdminOrderController::class, 'index'])->name('orders.index');

        Route::get('/settings', [AdminSettingController::class, 'index'])->name('settings.index');
        Route::post('/settings', [AdminSettingController::class, 'update'])->name('settings.update');

        Route::get('/settings/bot', [AdminSettingController::class, 'botConfig'])->name('settings.bot');
        Route::post('/settings/bot', [AdminSettingController::class, 'updateBot'])->name('settings.bot.update');

        Route::get('/2fa-setup', [AdminSettingController::class, 'show2faSetup'])->name('2fa.setup');
        Route::post('/2fa/enable', [AdminSettingController::class, 'enable2fa'])->name('2fa.enable');
        Route::post('/2fa/disable', [AdminSettingController::class, 'disable2fa'])->name('2fa.disable');

        Route::get('/extensions', [\App\Http\Controllers\Admin\AdminExtensionController::class, 'index'])->name('extensions.index');
        Route::post('/extensions', [\App\Http\Controllers\Admin\AdminExtensionController::class, 'store'])->name('extensions.store');
        Route::post('/extensions/{id}', [\App\Http\Controllers\Admin\AdminExtensionController::class, 'update'])->name('extensions.update');
        Route::post('/extensions/{id}/delete', [\App\Http\Controllers\Admin\AdminExtensionController::class, 'delete'])->name('extensions.delete');
    });
});
