<?php

use App\Http\Controllers\App\DashboardController;
use App\Http\Controllers\App\AdminDashboardPageController;
use App\Http\Controllers\App\AdminBillingPageController;
use App\Http\Controllers\App\AdminPlansPageController;
use App\Http\Controllers\App\AdminTenantDetailPageController;
use App\Http\Controllers\App\AdminTenantsPageController;
use App\Http\Controllers\App\BillingSettingsPageController;
use App\Http\Controllers\App\ChannelsSettingsPageController;
use App\Http\Controllers\App\ContactsController;
use App\Http\Controllers\App\InboxController;
use App\Http\Controllers\App\OrderDetailController;
use App\Http\Controllers\App\OrderPdfController;
use App\Http\Controllers\App\OrderPrintController;
use App\Http\Controllers\App\OrdersController;
use App\Http\Controllers\App\PipelineController;
use App\Http\Controllers\App\ProductsController;
use App\Http\Controllers\App\QuotesController;
use App\Http\Controllers\App\ReportsController;
use App\Http\Controllers\App\TasksController;
use App\Http\Controllers\App\FollowUpQueueController;
use App\Http\Controllers\App\AiSettingsPageController;
use App\Http\Controllers\App\UsersSettingsPageController;
use App\Http\Controllers\App\RolesSettingsPageController;
use App\Http\Controllers\Auth\AuthenticatedSessionController;
use App\Http\Controllers\Auth\RegisteredUserController;
use App\Http\Controllers\Install\InstallerController;
use App\Http\Controllers\Onboarding\OnboardingController;
use App\Http\Controllers\Web\WhishCallbackController;
use Illuminate\Support\Facades\Route;

Route::middleware('install.guest')->group(function () {
    Route::get('/install', [InstallerController::class, 'show'])->name('install.show');
    Route::post('/install', [InstallerController::class, 'install'])->name('install.run');
});

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

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

    Route::get('/api/payment/callbacks/whish', WhishCallbackController::class)->name('payments.whish.callback');

    Route::middleware('guest')->group(function () {
        Route::get('/signup', [RegisteredUserController::class, 'create'])->name('register');
        Route::post('/signup', [RegisteredUserController::class, 'store'])->name('register.store');

        Route::get('/login', [AuthenticatedSessionController::class, 'create'])->name('login');
        Route::post('/login', [AuthenticatedSessionController::class, 'store'])->name('login.store');
    });

    Route::middleware('auth')->group(function () {
        Route::post('/logout', [AuthenticatedSessionController::class, 'destroy'])->name('logout');

        Route::middleware('onboarding.pending')->group(function () {
            Route::get('/onboarding', [OnboardingController::class, 'show'])->name('onboarding.show');
            Route::post('/onboarding', [OnboardingController::class, 'update'])->name('onboarding.update');
        });

        Route::middleware(['onboarded', 'subscription.active'])->group(function () {
            Route::get('/app/dashboard', DashboardController::class)->name('dashboard');
            Route::get('/app/inbox', InboxController::class)->middleware('permission:conversations.view_all|conversations.view_assigned')->name('inbox');
            Route::get('/app/contacts', ContactsController::class)->middleware('permission:contacts.view')->name('contacts');
            Route::get('/app/pipeline', PipelineController::class)->middleware('permission:leads.view')->name('pipeline');
            Route::get('/app/products', ProductsController::class)->middleware('permission:products.manage')->name('products');
            Route::get('/app/quotes', QuotesController::class)->middleware('permission:quotes.manage')->name('quotes');
            Route::get('/app/orders', OrdersController::class)->middleware('permission:orders.manage')->name('orders');
            Route::get('/app/orders/{order}', OrderDetailController::class)->middleware('permission:orders.manage')->name('orders.show');
            Route::get('/app/orders/{order}/print', OrderPrintController::class)->middleware('permission:orders.manage')->name('orders.print');
            Route::get('/app/orders/{order}/pdf', OrderPdfController::class)->middleware('permission:orders.manage')->name('orders.pdf');
            Route::get('/app/tasks', TasksController::class)->name('tasks');
            Route::get('/app/follow-up-queue', FollowUpQueueController::class)->name('follow-up-queue');
            Route::get('/app/reports/{report}', ReportsController::class)->middleware('permission:reports.view|reports.view_revenue')->name('reports.show');
            Route::get('/app/settings/channels', ChannelsSettingsPageController::class)->middleware('permission:channels.manage')->name('settings.channels');
            Route::get('/app/settings/billing', BillingSettingsPageController::class)->middleware('permission:billing.manage')->name('settings.billing');
            Route::get('/app/settings/users', UsersSettingsPageController::class)->middleware('permission:users.manage')->name('settings.users');
            Route::get('/app/settings/roles', RolesSettingsPageController::class)->middleware('permission:roles.manage')->name('settings.roles');
            Route::get('/app/settings/ai', AiSettingsPageController::class)->middleware('permission:ai.manage_settings|ai.view_ai_usage')->name('settings.ai');
        });

        Route::middleware('operator')->group(function () {
            Route::get('/admin', AdminDashboardPageController::class)->name('admin.dashboard');
            Route::get('/admin/tenants', AdminTenantsPageController::class)->name('admin.tenants');
            Route::get('/admin/tenants/{tenant}', AdminTenantDetailPageController::class)->name('admin.tenants.show');
            Route::get('/admin/plans', AdminPlansPageController::class)->name('admin.plans');
            Route::get('/admin/billing', AdminBillingPageController::class)->name('admin.billing');
        });
    });
});
