<?php

use App\Http\Controllers\ProfileController;
use App\Http\Controllers\BusinessController;
use App\Http\Controllers\CustomerController;
use App\Http\Controllers\FbrReferenceController;
use App\Http\Controllers\InvoiceController;
use App\Http\Controllers\ItemController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\SettingsController;
use App\Http\Controllers\InvoiceImportController;
use App\Http\Controllers\CreditDebitNoteController;
use App\Http\Controllers\CustomerImportController;
use App\Http\Controllers\ItemImportController;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('welcome');
});

Route::middleware(['auth', 'business.context'])->group(function () {

    Route::get('/dashboard', DashboardController::class)->name('dashboard');

    // Businesses
    Route::get('/businesses/{business}/test-connection', [BusinessController::class, 'testConnection'])->name('businesses.testConnection');
    Route::resource('businesses', BusinessController::class);

    // Customers
    Route::get('customers/import', [CustomerImportController::class, 'show'])->name('customers.import');
    Route::get('customers/import/template', [CustomerImportController::class, 'downloadTemplate'])->name('customers.import.template');
    Route::post('customers/import/preview', [CustomerImportController::class, 'preview'])->name('customers.import.preview');
    Route::post('customers/import/store', [CustomerImportController::class, 'store'])->name('customers.import.store');

    Route::get('customers', [CustomerController::class, 'page'])->name('customers.index');
    Route::get('customers/data', [CustomerController::class, 'index'])->name('customers.data');
    Route::get('customers/create', [CustomerController::class, 'create'])->name('customers.create');
    Route::post('customers', [CustomerController::class, 'store'])->name('customers.store');
    Route::get('customers/{customer}', [CustomerController::class, 'show'])->name('customers.show');
    Route::get('customers/{customer}/edit', [CustomerController::class, 'edit'])->name('customers.edit');
    Route::put('customers/{customer}', [CustomerController::class, 'update'])->name('customers.update');
    Route::delete('customers/{customer}', [CustomerController::class, 'destroy'])->name('customers.destroy');
    Route::post('customers/bulk-delete', [CustomerController::class, 'bulkDelete'])->name('customers.bulk-delete');
   
   
    // Items (product master)
    Route::get('items/import', [ItemImportController::class, 'show'])->name('items.import');
    Route::get('items/import/template', [ItemImportController::class, 'downloadTemplate'])->name('items.import.template');
    Route::post('items/import/preview', [ItemImportController::class, 'preview'])->name('items.import.preview');
    Route::post('items/import/store', [ItemImportController::class, 'store'])->name('items.import.store');

    Route::get('items', [ItemController::class, 'page'])->name('items.index');
    Route::get('items/data', [ItemController::class, 'index'])->name('items.data');
    Route::get('items/create', [ItemController::class, 'create'])->name('items.create');
    Route::post('items', [ItemController::class, 'store'])->name('items.store');
    Route::get('items/{item}/edit', [ItemController::class, 'edit'])->name('items.edit');
    Route::get('items/{item}', [ItemController::class, 'show'])->name('items.show');
    Route::put('items/{item}', [ItemController::class, 'update'])->name('items.update');
    Route::delete('items/{item}', [ItemController::class, 'destroy'])->name('items.destroy');
    Route::post('items/bulk-delete', [ItemController::class, 'bulkDelete'])->name('items.bulk-delete');

    // Invoices — draft/sync lifecycle
    Route::get('invoices', [InvoiceController::class, 'page'])->name('invoices.index');
    Route::get('invoices/data', [InvoiceController::class, 'index'])->name('invoices.data');
    Route::get('invoices/metrics', [InvoiceController::class, 'metrics'])->name('invoices.metrics');
    Route::get('invoices/create', [InvoiceController::class, 'create'])->name('invoices.create');
    Route::get('invoices/pdf', [InvoiceController::class, 'downloadPdf'])->name('invoices.download-pdf');
    Route::post('invoices', [InvoiceController::class, 'store'])->name('invoices.store');
    // Invoice Import Module
    Route::get('invoices/import', [InvoiceImportController::class, 'show'])->name('invoices.import');
    Route::get('invoices/import/template', [InvoiceImportController::class, 'downloadTemplate'])->name('invoices.import.template');
    Route::post('invoices/import/preview', [InvoiceImportController::class, 'preview'])->name('invoices.import.preview');
    Route::post('invoices/import/store', [InvoiceImportController::class, 'store'])->name('invoices.import.store');

    Route::post('invoices/bulk-delete', [InvoiceController::class, 'bulkDelete'])->name('invoices.bulk-delete');
    Route::post('invoices/bulk-sync', [InvoiceController::class, 'bulkSync'])->name('invoices.bulk-sync');
    Route::get('invoices/bulk-pdf', [InvoiceController::class, 'bulkPdf'])->name('invoices.bulk-pdf');
    Route::get('invoices/export-excel', [InvoiceController::class, 'exportExcel'])->name('invoices.export-excel');

    Route::get('invoices/{invoice}/email-preview', [InvoiceController::class, 'emailPreview'])->name('invoices.email-preview');
    Route::post('invoices/{invoice}/send-email', [InvoiceController::class, 'sendEmail'])->name('invoices.send-email');
    Route::get('invoices/{invoice}', [InvoiceController::class, 'show'])->name('invoices.show');
    Route::get('invoices/{invoice}/edit', [InvoiceController::class, 'edit'])->name('invoices.edit');
    Route::put('invoices/{invoice}', [InvoiceController::class, 'update'])->name('invoices.update');
    Route::delete('invoices/{invoice}', [InvoiceController::class, 'destroy'])->name('invoices.destroy');
     Route::post('invoices/{id}/restore', [InvoiceController::class, 'restore'])->name('invoices.restore');
    Route::post('invoices/{invoice}/validate', [InvoiceController::class, 'validateNow'])->name('invoices.validate');
    Route::post('invoices/{invoice}/schedule', [InvoiceController::class, 'scheduleSync'])->name('invoices.schedule-sync');
    Route::post('invoices/{invoice}/sync', [InvoiceController::class, 'syncNow'])->name('invoices.sync');
    Route::post('invoices/{invoice}/queue-sync', [InvoiceController::class, 'queueSync']);
    Route::post('invoices/bulk-queue-sync', [InvoiceController::class, 'bulkQueueSync']);
    
    

    // FBR Integration Logs Page
    Route::get('fbr-logs', [InvoiceController::class, 'fbrLogsPage'])->name('fbr-logs.index');
    Route::get('fbr-logs/data', [InvoiceController::class, 'fbrLogsData'])->name('fbr-logs.data');

    // Debit / Credit Notes (created against an already-synced invoice)
    Route::get('notes', [CreditDebitNoteController::class, 'page'])->name('notes.index');
    Route::get('notes/data', [CreditDebitNoteController::class, 'index'])->name('notes.data');
    Route::get('notes/metrics', [CreditDebitNoteController::class, 'metrics'])->name('notes.metrics');
    Route::get('notes/create', [CreditDebitNoteController::class, 'create'])->name('notes.create');
    Route::post('notes', [CreditDebitNoteController::class, 'store'])->name('notes.store');
    Route::get('notes/{note}', [CreditDebitNoteController::class, 'show'])->name('notes.show');
    Route::get('notes/{note}/edit', [CreditDebitNoteController::class, 'edit'])->name('notes.edit');
    Route::put('notes/{note}', [CreditDebitNoteController::class, 'update'])->name('notes.update');
    Route::delete('notes/{note}', [CreditDebitNoteController::class, 'destroy'])->name('notes.destroy');
    Route::post('notes/{note}/sync', [CreditDebitNoteController::class, 'sync'])->name('notes.sync');
    Route::post('invoices/debit-note', [InvoiceController::class, 'storeDebitNote']);
    Route::post('invoices/credit-note', [InvoiceController::class, 'storeCreditNote']);

    // FBR reference/lookup data for building invoice forms
    Route::prefix('fbr')->group(function () {
        Route::get('provinces', [FbrReferenceController::class, 'provinces'])->name('fbr.provinces');
        Route::get('doc-types', [FbrReferenceController::class, 'docTypes'])->name('fbr.doc-types');
        Route::get('item-codes', [FbrReferenceController::class, 'itemCodes'])->name('fbr.item-codes');
        Route::get('transaction-types', [FbrReferenceController::class, 'transactionTypes'])->name('fbr.transaction-types');
        Route::get('uoms', [FbrReferenceController::class, 'uoms'])->name('fbr.uoms');
        Route::get('sale-type-to-rate', [FbrReferenceController::class, 'saleTypeToRate'])->name('fbr.sale-type-to-rate');
        Route::get('sro-schedule', [FbrReferenceController::class, 'sroSchedule'])->name('fbr.sro-schedule');
        Route::get('hs-uom', [FbrReferenceController::class, 'hsUom'])->name('fbr.hs-uom');
        Route::get('atl-status', [FbrReferenceController::class, 'atlStatus'])->name('fbr.atl-status');
        Route::get('registration-type', [FbrReferenceController::class, 'registrationType'])->name('fbr.registration-type');
        Route::get('customer-lookup', [FbrReferenceController::class, 'customerLookup'])->name('fbr.customer-lookup');
    });

    // Reports module
    Route::get('reports/customer', [\App\Http\Controllers\ReportsController::class, 'customerReport'])->name('reports.customer');
    Route::get('reports/customer/data', [\App\Http\Controllers\ReportsController::class, 'customerData'])->name('reports.customer.data');
    Route::get('reports/customer/print', [\App\Http\Controllers\ReportsController::class, 'printCustomerReport'])->name('reports.customer.print');

    Route::get('reports/tax', [\App\Http\Controllers\ReportsController::class, 'taxReport'])->name('reports.tax');
    Route::get('reports/tax/data', [\App\Http\Controllers\ReportsController::class, 'taxData'])->name('reports.tax.data');
    Route::get('reports/tax/print', [\App\Http\Controllers\ReportsController::class, 'printTaxReport'])->name('reports.tax.print');

    Route::get('/settings', [SettingsController::class, 'edit'])->name('settings.edit');
    Route::put('/settings', [SettingsController::class, 'update'])->name('settings.update');
    Route::post('/settings/test-smtp', [SettingsController::class, 'testSmtp'])->name('settings.test-smtp');

    Route::get('/settings/templates', [SettingsController::class, 'templatesIndex'])->name('settings.templates.index');
    Route::get('/settings/templates/{template}/edit', [SettingsController::class, 'templateEdit'])->name('settings.templates.edit');
    Route::put('/settings/templates/{template}', [SettingsController::class, 'templateUpdate'])->name('settings.templates.update');
    Route::post('/settings/templates/{template}/default', [SettingsController::class, 'templateSetDefault'])->name('settings.templates.setDefault');
    Route::get('/settings/templates/{template}/preview', [SettingsController::class, 'preview'])
        ->name('settings.templates.preview');

    Route::get('/settings/templates/{template}/preview-thumbnail', [SettingsController::class, 'previewThumbnail'])
        ->name('settings.templates.previewThumbnail');

    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');
    Route::post('/profile/picture', [ProfileController::class, 'updatePicture'])->name('profile.picture.update');
    Route::post('/profile/2fa/setup', [ProfileController::class, 'setup2fa'])->name('profile.2fa.setup');
    Route::post('/profile/2fa/enable', [ProfileController::class, 'enable2fa'])->name('profile.2fa.enable');
    Route::post('/profile/2fa/disable', [ProfileController::class, 'disable2fa'])->name('profile.2fa.disable');

    // Notifications Lifecycle
    Route::get('/notifications', [App\Http\Controllers\NotificationController::class, 'index'])->name('notifications.index');
    Route::post('/notifications/{id}/read', [App\Http\Controllers\NotificationController::class, 'markRead'])->name('notifications.read');
    Route::post('/notifications/read-all', [App\Http\Controllers\NotificationController::class, 'markAllRead'])->name('notifications.read-all');
    Route::delete('/notifications/{id}', [App\Http\Controllers\NotificationController::class, 'destroy'])->name('notifications.destroy');

    // Business Users & Custom Roles Management
    Route::resource('business-users', \App\Http\Controllers\BusinessUserController::class);
    Route::resource('business-roles', \App\Http\Controllers\BusinessRoleController::class)->parameters([
        'business-roles' => 'role'
    ]);

    // Super Admin Routes
    Route::get('/super-admin/impersonate/leave', [App\Http\Controllers\SuperAdminController::class, 'leaveImpersonate'])->name('super-admin.impersonate.leave');
    Route::get('/super-admin/businesses/{business}/manage', [App\Http\Controllers\SuperAdminController::class, 'manageBusiness'])->name('super-admin.businesses.manage');
    Route::get('/super-admin', [App\Http\Controllers\SuperAdminController::class, 'dashboard'])->name('super-admin.dashboard');
    Route::post('/super-admin/plans', [App\Http\Controllers\SuperAdminController::class, 'storePlan'])->name('super-admin.plans.store');
    Route::put('/super-admin/businesses/{business}', [App\Http\Controllers\SuperAdminController::class, 'updateBusiness'])->name('super-admin.businesses.update');
    Route::post('/super-admin/role-permissions', [App\Http\Controllers\SuperAdminController::class, 'updateRolePermissions'])->name('super-admin.role-permissions.update');
    Route::get('/super-admin/impersonate/{user}', [App\Http\Controllers\SuperAdminController::class, 'impersonate'])->name('super-admin.impersonate');
    Route::post('/super-admin/settings', [App\Http\Controllers\SuperAdminController::class, 'updateSettings'])->name('super-admin.settings.update');
    Route::get('/super-admin/backup', [App\Http\Controllers\SuperAdminController::class, 'backupDb'])->name('super-admin.backup');
    Route::post('/super-admin/clear', [App\Http\Controllers\SuperAdminController::class, 'clearDb'])->name('super-admin.clear');
});

Route::middleware('guest')->group(function () {
    Route::get('/login/2fa', [App\Http\Controllers\Auth\AuthenticatedSessionController::class, 'create2fa'])->name('auth.2fa');
    Route::post('/login/2fa', [App\Http\Controllers\Auth\AuthenticatedSessionController::class, 'store2fa'])->name('auth.2fa.store');
});

require __DIR__ . '/auth.php';
