<?php

use App\Http\Controllers\AppController;
use App\Http\Middleware\CheckPageSlug;
use App\Livewire\Blog;
use App\Livewire\Dashboard\Bulk;
use App\Livewire\Dashboard\BulkGmail;
use App\Livewire\Dashboard\Dashboard;
use App\Livewire\Dashboard\Mailbox\Inbox;
use App\Livewire\Frontend\Mailbox;
use App\Livewire\Home;
use App\Livewire\ListBlog;
use App\Livewire\Page;

use App\Livewire\Settings\Appearance;
use App\Livewire\Settings\Password;
use App\Livewire\Settings\Profile;
use App\Models\Email;
use Illuminate\Support\Facades\Route;

Route::get('/', Home::class)->name('home');
Route::get('/mailbox', Mailbox::class)->name('mailbox');
Route::get('/mailbox/{email?}', [AppController::class, 'mailbox'])->name('mailboxFromURL');

Route::get('/switch/{email}', [AppController::class, 'switch'])->name('switch');
Route::get('/switchP/{email}', [AppController::class, 'switchP'])->name('switchP');
Route::get('/delete/{email?}', [AppController::class, 'delete'])->name('delete');
Route::get('/deleteP/{email?}', [AppController::class, 'deleteP'])->name('deleteP');

Route::get('locale/{locale}', [AppController::class, 'locale'])->name('locale');
Route::get('/blog', ListBlog::class)->name('list-blog');
Route::get('/blog/{slug}', Blog::class)->name('blog');

Route::post('/sync', function (Request $request) {
    try {

        if (config('app.auto_fetch_mail')) {
            Email::fetchProcessStoreEmail();
        }

    } catch (\Exception $e) {
        \Log::error($e->getMessage());
    }
    return response()->noContent();
});


Route::middleware(['auth', 'verified'])->group(function () {
    Route::get('dashboard', Dashboard::class)->name('dashboard');
    Route::get('dashboard/generate-premium-email', Inbox::class)->name('dashboard.premium');
    Route::get('dashboard/generate-10minute-email', Dashboard::class)->name('dashboard.10minute');
    Route::get('dashboard/bulk-email-generator', Bulk::class)->name('dashboard.bulk');
    Route::get('dashboard/bulk-gmail-generator', BulkGmail::class)->name('dashboard.bulkGmail');
    Route::get('dashboard/compose-email', Dashboard::class)->name('dashboard.compose');

    // Checkout Routes
    Route::get('checkout/{plan}', function ($pricing_id) {
        $plans = config('app.plans');
        $pricingData = [];
        foreach ($plans as $plan) {
            $pricingData[] = $plan['pricing_id'];
        }

        if (in_array($pricing_id, $pricingData)) {
            return auth()->user()
                        ->newSubscription('default', $pricing_id)
                        ->allowPromotionCodes()
                        ->checkout([
                            'success_url' => route('checkout.success'),
                            'cancel_url' => route('checkout.cancel'),
                        ]);

        }
        abort(404);
    })->name('checkout');

    Route::get('dashboard/success', [Dashboard::class, 'paymentStatus'])->name('checkout.success')->defaults('status', 'success');
    Route::get('dashboard/cancel', [Dashboard::class, 'paymentStatus'])->name('checkout.cancel')->defaults('status', 'cancel');

    Route::get('dashboard/billing', function () {
        return auth()->user()->redirectToBillingPortal(route('dashboard'));
    })->name('billing');
});

Route::middleware(['auth'])->group(function () {
    Route::redirect('settings', 'settings/profile');

    Route::get('settings/profile', Profile::class)->name('settings.profile');
    Route::get('settings/password', Password::class)->name('settings.password');
    Route::get('settings/appearance', Appearance::class)->name('settings.appearance');
});

require __DIR__.'/auth.php';

Route::get('{slug}', Page::class)->where('slug', '.*')->name('page')->middleware(CheckPageSlug::class);
