<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Frontend\AuthController;
use App\Http\Controllers\Frontend\HomeController;
use App\Http\Controllers\Frontend\UploadController;
use App\Http\Controllers\Frontend\BlogController;
use App\Http\Controllers\Frontend\ContactController;
use App\Http\Controllers\Frontend\PricingController;
use App\Http\Controllers\Frontend\AffiliateController;
use App\Http\Controllers\Frontend\FileController;
use App\Http\Controllers\Frontend\PayController;
use App\Http\Controllers\Frontend\LegalController;
use App\Http\Controllers\Frontend\PagesController;
use App\Http\Controllers\Frontend\SubscriberController;
use App\Models\Upload;
use Illuminate\Support\Facades\Storage;
use App\Http\Controllers\Frontend\HelperController;
use App\Http\Controllers\Frontend\SitemapController;

Route::get('/', [HomeController::class, 'home'])->name('home');

Route::controller(AuthController::class)
    ->prefix('login')
    ->middleware('auth.guest')
    ->group(function () {
        Route::get('/', 'login')->name('login');
        Route::post('/', 'login_post');
        Route::get('/google/callback', 'login_post_google');
        Route::get('/google/redirect', 'login_post_google_redirect');
    });

Route::controller(AuthController::class)
    ->prefix('register')
    ->middleware('auth.guest')
    ->group(function () {
        Route::get('/', 'register')->name('register');
        Route::post('/', 'register_post');
    });

Route::controller(AuthController::class)
    ->prefix('forgot-password')
    ->middleware('auth.guest')
    ->group(function () {
        Route::get('/', 'forgot_password');
        Route::get('/{key}', 'forgot_password_reset');
        Route::post('/', 'forgot_password_post');
    });

Route::controller(AuthController::class)
    ->prefix('verify-account')
    ->middleware('auth.guest')
    ->group(function () {
        Route::get('/', 'verify_account');
        Route::get('/{key}', 'verify_account_verified');
        Route::post('/', 'verify_account_post');
    });

Route::controller(UploadController::class)
    ->group(function () {
        Route::post('/upload', 'upload');
        Route::post('/upload-link', 'upload_link');
    });

Route::controller(FileController::class)
    ->prefix('file')
    ->group(function () {
        Route::get('/clear', 'clear');
        Route::get('/{filename}', 'file');
        Route::post('/{filename}', 'file_password')
            ->middleware('throttle:filePassword');
        Route::post('/{filename}/report', 'report')
            ->middleware('throttle:report');
    });

Route::get('/get/{filename}', function ($filename) {
    $file = Upload::where('filename', $filename)->first();
    return Storage::disk($file->disk)->exists($file->filename)
        ? Storage::disk($file->disk)->response($filename)
        : response()->file(public_path("assets/image/file-deleted.webp"));
})->middleware('filePrivacy');

Route::controller(BlogController::class)
    ->prefix('blog')
    ->group(function () {
        Route::get('/', 'blog');
        Route::get('/{slug}', 'inner');
        Route::post('/{slug}/new-comment', 'comment_new');
        Route::post('/{limit}/{offset}', 'blog_posts_data');
    });

Route::controller(ContactController::class)
    ->prefix('contact')
    ->group(function () {
        Route::get('/', 'contact');
        Route::post('/', 'contact_post');
    });

Route::controller(PricingController::class)
    ->prefix('pricing')
    ->group(function () {
        Route::get('/', 'pricing');
    });

Route::controller(PayController::class)
    ->prefix('pay')
    ->middleware('auth.user')
    ->group(function () {
        Route::get('/{planString}', 'pay');
        Route::post('/{planString}/stripe', 'pay_post_stripe');
        Route::post('/{planString}/bank', 'pay_post_bank');
    });

Route::controller(AffiliateController::class)
    ->prefix('affiliate')
    ->group(function () {
        Route::get('/', 'affiliate');
    });

Route::get('/p/{path}', [PagesController::class, 'loadPage']);

Route::controller(LegalController::class)
    ->group(function () {
        Route::get('/terms-of-use', 'terms_of_use');
        Route::get('/privacy-policy', 'privacy_policy');
    });

Route::controller(SubscriberController::class)
    ->prefix('subscribe')
    ->group(function () {
        Route::post('/', 'subscribe');
        Route::get('/{key}', 'subscribe_verify');
    });

Route::get('/logout', [AuthController::class, 'logout']);

Route::post('/color-mode', [HelperController::class, 'theme_mode']);

Route::get('/404', function () {
    abort(404);
});
Route::get('/403', function () {
    abort(403);
});
Route::get('/500', function () {
    abort(500);
});
Route::get('/maintenance', [HelperController::class, 'maintenance']);

Route::get('/sitemap.xml', [SitemapController::class, 'sitemap']);

Route::get('/phpinfo', function () { 
    return phpinfo();
})->middleware('auth.admin');
