<?php

use App\Http\Controllers\Admin\AccountStatementController;
use App\Http\Controllers\Admin\ActivityLogController;
use App\Http\Controllers\Admin\AiAssistantController;
use App\Http\Controllers\Admin\BackupController;
use App\Http\Controllers\Admin\BillingController;
use App\Http\Controllers\Admin\BillingPaymentController;
use App\Http\Controllers\Admin\BtrcReportController;
use App\Http\Controllers\Admin\CustomerController;
use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\EmailMessageController;
use App\Http\Controllers\Admin\EmailTemplateController;
use App\Http\Controllers\Admin\HomepageCmsController;
use App\Http\Controllers\Admin\InventoryController;
use App\Http\Controllers\Admin\MikrotikController;
use App\Http\Controllers\Admin\MikrotikSecretController;
use App\Http\Controllers\Admin\OltController;
use App\Http\Controllers\Admin\OnlineStatusController;
use App\Http\Controllers\Admin\PackageController;
use App\Http\Controllers\Admin\PluginController;
use App\Http\Controllers\Admin\QueueController;
use App\Http\Controllers\Admin\ReportsController;
use App\Http\Controllers\Admin\ResellerController;
use App\Http\Controllers\Admin\RoleController;
use App\Http\Controllers\Admin\SchedulerController;
use App\Http\Controllers\Admin\SettingsController;
use App\Http\Controllers\Admin\SmsGatewayController;
use App\Http\Controllers\Admin\SmsMessageController;
use App\Http\Controllers\Admin\SmsTemplateController;
use App\Http\Controllers\Admin\StaffController;
use App\Http\Controllers\Admin\ZoneController;
use App\Http\Controllers\CustomerPortalController;
use App\Http\Controllers\CustomerPortalLoginController;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\LocaleController;
use App\Http\Controllers\ProfileController;
use Illuminate\Support\Facades\Route;

Route::get('/', HomeController::class)->name('home');
Route::get('/language/{locale}', LocaleController::class)->whereIn('locale', ['en', 'bn'])->name('language.switch');

Route::middleware('guest')->group(function () {
    Route::get('/customer/login', [CustomerPortalLoginController::class, 'show'])->name('customer.login');
    Route::post('/customer/login', [CustomerPortalLoginController::class, 'identify'])->name('customer.login.identify');
    Route::get('/customer/verify', [CustomerPortalLoginController::class, 'showVerify'])->name('customer.login.verify');
    Route::post('/customer/verify', [CustomerPortalLoginController::class, 'verify'])->name('customer.login.verify.submit');
});
Route::post('/customer/logout', [CustomerPortalLoginController::class, 'logout'])->name('customer.logout');

Route::prefix('portal')->name('portal.')->middleware('customer.portal')->group(function () {
    Route::get('/', [CustomerPortalController::class, 'dashboard'])->name('dashboard');
    Route::post('/payment/{billing}', [CustomerPortalController::class, 'startPayment'])->name('payment.start');
    Route::get('/payment/callback/uddoktapay', [CustomerPortalController::class, 'paymentCallback'])->name('payment.callback');
});

Route::middleware(['auth', 'verified', 'plugin.active'])->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');

    Route::prefix('admin')->name('admin.')->group(function () {
        Route::get('dashboard', [DashboardController::class, 'index'])->name('dashboard');
        Route::get('ai-assistant', [AiAssistantController::class, 'index'])->name('ai-assistant.index');
        Route::post('ai-assistant/chat', [AiAssistantController::class, 'chat'])->name('ai-assistant.chat');
        Route::post('ai-assistant/confirm', [AiAssistantController::class, 'confirm'])->name('ai-assistant.confirm');

        // Plugin platform (Super Admin / plugin.manage only)
        Route::get('plugins', [PluginController::class, 'index'])->name('plugins.index');
        Route::post('plugins/install', [PluginController::class, 'install'])->name('plugins.install');
        Route::patch('plugins/{plugin}/toggle', [PluginController::class, 'toggle'])->name('plugins.toggle');
        Route::get('customers/data', [CustomerController::class, 'data'])->name('customers.data');
        Route::get('customers/delete-log', [CustomerController::class, 'deleted'])->name('customers.deleted');
        Route::patch('customers/delete-log/{customer}/restore', [CustomerController::class, 'restore'])->withTrashed()->name('customers.restore');
        Route::delete('customers/delete-log/{customer}/permanent', [CustomerController::class, 'forceDelete'])->withTrashed()->name('customers.force-delete');
        Route::get('customers/quick-search', [CustomerController::class, 'quickSearch'])->name('customers.quick-search');
        Route::get('customers/import/template', [CustomerController::class, 'importTemplate'])->name('customers.import.template');
        Route::post('customers/import', [CustomerController::class, 'import'])->name('customers.import');
        Route::delete('customers/bulk-delete', [CustomerController::class, 'bulkDestroy'])->name('customers.bulk-destroy');
        Route::get('customers/create', [CustomerController::class, 'create'])->name('customers.create');
        Route::get('customers/{customer}/ledger', [CustomerController::class, 'ledger'])->name('customers.ledger');
        Route::get('customers/{customer}/ledger/data', [CustomerController::class, 'ledgerData'])->name('customers.ledger.data');
        Route::get('customers/{customer}/ledger/{account}/receipt', [CustomerController::class, 'ledgerReceipt'])->name('customers.ledger.receipt');
        Route::post('customers/{customer}/mikrotik-disconnect', [CustomerController::class, 'updateDisconnect'])->name('customers.mikrotik_disconnect.update');
        Route::post('customers/{customer}/grace-extension', [CustomerController::class, 'extendDisconnectGrace'])->name('customers.grace-extension');
        Route::get('customers/{customer}/live-traffic', [CustomerController::class, 'liveTraffic'])->name('customers.live-traffic');
        Route::resource('customers', CustomerController::class)->except(['show']);

        Route::get('packages/data', [PackageController::class, 'data'])->name('packages.data');
        Route::get('packages/mikrotik-profiles', [PackageController::class, 'mikrotikProfiles'])->name('packages.mikrotik-profiles');
        Route::resource('packages', PackageController::class)->except(['show', 'create']);

        Route::get('mikrotiks/data', [MikrotikController::class, 'data'])->name('mikrotiks.data');
        Route::post('mikrotiks/test-connection', [MikrotikController::class, 'testConnection'])->name('mikrotiks.test_connection');
        // Keep connection testing on a non-resource URL so a browser cache or a
        // malformed request can never treat "test-connection" as a router ID.
        Route::post('mikrotiks/connection-check', [MikrotikController::class, 'testConnection'])->name('mikrotiks.connection_check');
        Route::patch('mikrotiks/{mikrotik}/status', [MikrotikController::class, 'toggleStatus'])->name('mikrotiks.status');
        Route::get('mikrotiks/profiles', [MikrotikController::class, 'profiles'])->name('mikrotiks.profiles');
        Route::get('mikrotik/disconnect-due', [MikrotikController::class, 'disconnectByDue'])->name('mikrotik.disconnect-due');
        Route::get('mikrotik/disconnect-due/data', [MikrotikController::class, 'disconnectByDueData'])->name('mikrotik.disconnect-due.data');
        Route::get('mikrotik/online', [MikrotikController::class, 'online'])->name('mikrotik.online');
        Route::get('mikrotik/online/data', [MikrotikController::class, 'onlineData'])->name('mikrotik.online.data');
        Route::get('mikrotik/pppoe-offline', [MikrotikController::class, 'pppoeOffline'])->name('mikrotik.pppoe-offline');
        Route::get('mikrotik/pppoe-offline/data', [MikrotikController::class, 'pppoeOfflineData'])->name('mikrotik.pppoe-offline.data');
        Route::get('mikrotik/online-static', [MikrotikController::class, 'onlineStatic'])->name('mikrotik.online-static');
        Route::get('mikrotik/online-static/data', [MikrotikController::class, 'onlineStaticData'])->name('mikrotik.online-static.data');
        Route::get('mikrotik/pppoe-unmatching', [MikrotikController::class, 'pppoeUnmatching'])->name('mikrotik.pppoe-unmatching');
        Route::get('mikrotik/pppoe-software-unmatching', [MikrotikController::class, 'pppoeSoftwareUnmatching'])->name('mikrotik.pppoe-software-unmatching');
        Route::get('mikrotik/pppoe-mikrotik-unmatching', [MikrotikController::class, 'pppoeMikrotikUnmatching'])->name('mikrotik.pppoe-mikrotik-unmatching');
        Route::get('mikrotik/pppoe-unmatching/data', [MikrotikController::class, 'pppoeUnmatchingData'])->name('mikrotik.pppoe-unmatching.data');
        Route::post('mikrotik/pppoe-unmatching/import-secrets', [MikrotikController::class, 'importSecrets'])->name('mikrotik.pppoe-unmatching.import-secrets');
        Route::post('mikrotik/pppoe-unmatching/upload-customers', [MikrotikController::class, 'uploadCustomers'])->name('mikrotik.pppoe-unmatching.upload-customers');
        Route::post('mikrotik/pppoe-unmatching/upload-one', [MikrotikController::class, 'uploadOneCustomer'])->name('mikrotik.pppoe-unmatching.upload-one');
        Route::post('mikrotik/pppoe-unmatching/import-one', [MikrotikController::class, 'importOneSecret'])->name('mikrotik.pppoe-unmatching.import-one');
        Route::get('mikrotik/static-unmatching', [MikrotikController::class, 'staticUnmatching'])->name('mikrotik.static-unmatching');
        Route::get('mikrotik/static-unmatching/data', [MikrotikController::class, 'staticUnmatchingData'])->name('mikrotik.static-unmatching.data');
        Route::resource('mikrotiks', MikrotikController::class)->except(['show', 'create'])->whereNumber('mikrotik');

        // OLT management
        Route::get('olts/data', [OltController::class, 'data'])->name('olts.data');
        Route::post('olts/{olt}/test-connection', [OltController::class, 'testConnection'])->name('olts.test_connection');
        Route::get('olts/{olt}/snapshot', [OltController::class, 'refreshSnapshot'])->name('olts.snapshot');
        Route::get('olts/{olt}/interfaces', [OltController::class, 'interfaces'])->name('olts.interfaces');
        Route::get('olts/{olt}/onus', [OltController::class, 'onus'])->name('olts.onus');
        Route::resource('olts', OltController::class);

        Route::post('inventory/transaction', [InventoryController::class, 'transaction'])->name('inventory.transaction');
        Route::get('inventory/export', [InventoryController::class, 'export'])->name('inventory.export');
        Route::resource('inventory', InventoryController::class)->only(['index', 'store', 'update']);

        Route::get('accounts/statement/export', [AccountStatementController::class, 'export'])->name('accounts.statement.export');
        Route::get('accounts/statement', [AccountStatementController::class, 'index'])->name('accounts.statement');
        Route::post('accounts/transactions', [AccountStatementController::class, 'store'])->name('accounts.transactions.store');
        Route::delete('accounts/transactions/{transaction}', [AccountStatementController::class, 'destroy'])->name('accounts.transactions.destroy');

        Route::get('mikrotik-secrets/data', [MikrotikSecretController::class, 'data'])->name('mikrotik-secrets.data');
        Route::post('mikrotik-secrets/sync', [MikrotikSecretController::class, 'syncNow'])->name('mikrotik-secrets.sync');
        Route::patch('mikrotik-secrets/{mikrotik_secret}/status', [MikrotikSecretController::class, 'toggleStatus'])->name('mikrotik-secrets.status');
        Route::resource('mikrotik-secrets', MikrotikSecretController::class)->except(['show', 'create']);

        Route::get('online-statuses/data', [OnlineStatusController::class, 'data'])->name('online-statuses.data');
        Route::resource('online-statuses', OnlineStatusController::class)->except(['show', 'create']);

        Route::get('zones/data', [ZoneController::class, 'data'])->name('zones.data');
        Route::resource('zones', ZoneController::class)->except(['show', 'create']);

        Route::get('resellers/data', [ResellerController::class, 'data'])->name('resellers.data');
        Route::resource('resellers', ResellerController::class)->except(['show', 'create']);

        Route::get('staff/data', [StaffController::class, 'data'])->name('staff.data');
        Route::post('roles/custom-permissions', [RoleController::class, 'storePermission'])->name('roles.permissions.store');
        Route::delete('roles/custom-permissions/{permission}', [RoleController::class, 'destroyPermission'])->name('roles.permissions.destroy');
        Route::resource('roles', RoleController::class)->except(['show']);
        Route::resource('staff', StaffController::class)->except(['show']);

        Route::get('cms/homepage', [HomepageCmsController::class, 'index'])->name('cms.homepage.index');
        Route::put('cms/homepage/{homepageSection}', [HomepageCmsController::class, 'update'])->name('cms.homepage.update');

        Route::get('sms-gateways/data', [SmsGatewayController::class, 'data'])->name('sms-gateways.data');
        Route::resource('sms-gateways', SmsGatewayController::class)->except(['show', 'create']);

        Route::get('sms-messages/data', [SmsMessageController::class, 'data'])->name('sms-messages.data');
        Route::resource('sms-messages', SmsMessageController::class)->only(['index', 'store']);
        Route::get('sms-templates', [SmsTemplateController::class, 'index'])->name('sms-templates.index');
        Route::put('sms-templates/{smsTemplate}', [SmsTemplateController::class, 'update'])->name('sms-templates.update');

        Route::get('activity-logs/data', [ActivityLogController::class, 'data'])->name('activity-logs.data');
        Route::get('activity-logs/mikrotik-automation', [ActivityLogController::class, 'automation'])->name('activity-logs.automation');
        Route::resource('activity-logs', ActivityLogController::class)->only(['index', 'destroy']);

        // Reports
        Route::get('reports', [ReportsController::class, 'index'])->name('reports.index');
        Route::get('reports/revenue/data', [ReportsController::class, 'revenueData'])->name('reports.revenue.data');
        Route::post('reports/revenue/export', [ReportsController::class, 'exportRevenue'])->name('reports.revenue.export');
        Route::get('reports/btrc', [BtrcReportController::class, 'index'])->name('reports.btrc');
        Route::get('reports/btrc/export', [BtrcReportController::class, 'export'])->name('reports.btrc.export');

        // Settings
        Route::get('settings/data', [SettingsController::class, 'data'])->name('settings.data');
        Route::post('settings/control-center', [SettingsController::class, 'savePanel'])->name('settings.control-center');
        Route::post('settings/clear-cache', [SettingsController::class, 'clearCache'])->name('settings.clear-cache');
        Route::post('settings/apply-payment-redirect', [SettingsController::class, 'applyPaymentRedirect'])->name('settings.apply-payment-redirect');
        Route::resource('settings', SettingsController::class)->only(['index', 'show', 'update']);

        // Backups
        Route::get('backups/data', [BackupController::class, 'data'])->name('backups.data');
        Route::resource('backups', BackupController::class)->only(['index', 'store', 'destroy']);
        Route::get('backups/{id}/download', [BackupController::class, 'download'])->name('backups.download');

        // Scheduler
        Route::get('scheduler', [SchedulerController::class, 'index'])->name('scheduler.index');
        Route::post('scheduler/run-daily', [SchedulerController::class, 'runDaily'])->name('scheduler.run-daily');

        // Queue Monitoring
        Route::get('queue', [QueueController::class, 'index'])->name('queue.index');
        Route::get('queue/jobs/data', [QueueController::class, 'jobsData'])->name('queue.jobs.data');
        Route::get('queue/failed/data', [QueueController::class, 'failedData'])->name('queue.failed.data');
        Route::post('queue/failed/{id}/retry', [QueueController::class, 'retryFailed'])->name('queue.failed.retry');
        Route::delete('queue/failed/{id}', [QueueController::class, 'deleteFailed'])->name('queue.failed.delete');
        Route::post('queue/flush', [QueueController::class, 'flushJobs'])->name('queue.flush');

        // Customer Billing
        Route::get('billings/data', [BillingController::class, 'data'])->name('billings.data');
        Route::get('billings/print', [BillingController::class, 'print'])->name('billings.print');
        Route::get('billings/print-paid', [BillingController::class, 'printPaid'])->name('billings.print-paid');
        Route::get('billings/print-unpaid', [BillingController::class, 'printUnpaid'])->name('billings.print-unpaid');
        Route::get('billings/generate', [BillingController::class, 'generate'])->name('billings.generate');
        Route::get('billings/full-paid', [BillingController::class, 'fullPaid'])->name('billings.full_paid');
        Route::post('billings/{id}/generate', [BillingController::class, 'generateInvoice'])->name('billings.generateInvoice');
        Route::post('billings/pay', [BillingPaymentController::class, 'pay'])->name('billings.pay');
        Route::delete('customers/{customer}/ledger/{account}', [BillingPaymentController::class, 'destroy'])->name('customers.ledger.destroy');
        Route::resource('billings', BillingController::class)->only(['index', 'store', 'update', 'destroy', 'show']);

        Route::get('email-templates/data', [EmailTemplateController::class, 'data'])->name('email-templates.data');
        Route::resource('email-templates', EmailTemplateController::class)->except(['show', 'create']);

        Route::get('email-messages/data', [EmailMessageController::class, 'data'])->name('email-messages.data');
        Route::resource('email-messages', EmailMessageController::class)->only(['index', 'store']);
    });

    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';
