<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\DashboardController;

Route::get('/', function () {
    return redirect()->route('login');
});

Route::middleware('guest')->group(function () {
    Route::get('/login', [AuthController::class, 'showLoginForm'])->name('login');
    Route::post('/login', [AuthController::class, 'login']);
    
    // OTP Password Reset Routes
    Route::get('forgot-password', [\App\Http\Controllers\OtpController::class, 'showForgotForm'])->name('password.forgot');
    Route::post('forgot-password', [\App\Http\Controllers\OtpController::class, 'requestOtp'])->name('password.otp.request');
    Route::get('verify-otp', [\App\Http\Controllers\OtpController::class, 'showVerifyForm'])->name('password.verify');
    Route::post('verify-otp', [\App\Http\Controllers\OtpController::class, 'verifyOtp'])->name('password.otp.verify');
    Route::get('reset-password', [\App\Http\Controllers\OtpController::class, 'showResetForm'])->name('password.reset');
    Route::post('reset-password', [\App\Http\Controllers\OtpController::class, 'resetPassword'])->name('password.reset.submit');
});

// Public links
Route::get('/public/installment-plan/{id}', [\App\Http\Controllers\PublicController::class, 'showInstallmentPlan'])->name('public.installment-plan.show');
Route::post('/public/installment-plan/{id}', [\App\Http\Controllers\PublicController::class, 'verifyInstallmentPlan'])->name('public.installment-plan.verify');

Route::middleware('auth')->group(function () {
    Route::post('/logout', [AuthController::class, 'logout'])->name('logout');
    
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
    
    // User Guide
    Route::get('/guide', function() { return view('guide'); })->name('guide');
    
    // Notifications
    Route::get('/notifications', [\App\Http\Controllers\NotificationController::class, 'index'])->name('notifications.index');
    Route::post('/notifications/{id}/read', [\App\Http\Controllers\NotificationController::class, 'markAsRead'])->name('notifications.read');
    
    // Admin Notifications
    Route::get('/system/notifications/create', [\App\Http\Controllers\NotificationController::class, 'create'])->name('notifications.create');
    Route::post('/system/notifications', [\App\Http\Controllers\NotificationController::class, 'store'])->name('notifications.store');
    
    // Customer Portal
    Route::get('/portal/dashboard', [\App\Http\Controllers\CustomerPortalController::class, 'dashboard'])->name('portal.dashboard');
    Route::get('/portal/installments', [\App\Http\Controllers\CustomerPortalController::class, 'installments'])->name('portal.installments');
    Route::post('/portal/installment/{installment}/upload-proof', [\App\Http\Controllers\CustomerPortalController::class, 'uploadProof'])->name('portal.upload_proof');
    Route::get('/portal/documents', [\App\Http\Controllers\CustomerPortalController::class, 'documents'])->name('portal.documents');
    Route::get('/portal/invoices', [\App\Http\Controllers\CustomerPortalController::class, 'invoices'])->name('portal.invoices');
    Route::get('/portal/invoices/{invoice}', [\App\Http\Controllers\CustomerPortalController::class, 'showInvoice'])->name('portal.invoices.show');
    Route::get('/portal/quotations', [\App\Http\Controllers\CustomerPortalController::class, 'quotations'])->name('portal.quotations');
    Route::get('/portal/quotations/{quotation}', [\App\Http\Controllers\CustomerPortalController::class, 'showQuotation'])->name('portal.quotations.show');
    Route::get('/portal/profile', [\App\Http\Controllers\CustomerPortalController::class, 'profile'])->name('portal.profile');
    Route::post('/portal/profile', [\App\Http\Controllers\CustomerPortalController::class, 'updateProfile'])->name('portal.profile.update');
    
    // Staff Global Routes (Accessible to all non-customers)
    Route::middleware(['not.customer'])->group(function () {
        Route::resource('attendance', \App\Http\Controllers\AttendanceController::class)->only(['index', 'store']);
        Route::post('attendance/self-log', [\App\Http\Controllers\AttendanceController::class, 'selfLog'])->name('attendance.self-log');
    });

    // CRM and Admin Routes
    Route::middleware(['not.customer', 'permission.dynamic'])->group(function () {
        // Export Routes
        Route::get('/leads/pipeline', [\App\Http\Controllers\LeadPipelineController::class, 'index'])->name('leads.pipeline');
        Route::put('/leads/{lead}/status', [\App\Http\Controllers\LeadPipelineController::class, 'updateStatus'])->name('leads.update_status');
        Route::get('/leads/export', [\App\Http\Controllers\LeadController::class, 'export'])->name('leads.export');
        Route::get('customers/export', [\App\Http\Controllers\CustomerController::class, 'export'])->name('customers.export');

        // Payment Slip
        Route::get('payment-slip/{transaction}', [\App\Http\Controllers\PaymentSlipController::class, 'generate'])->name('payment.slip.generate');

        // Leads & Customers
        Route::resource('leads', \App\Http\Controllers\LeadController::class);
        Route::post('leads/{lead}/convert', [\App\Http\Controllers\LeadController::class, 'convertToCustomer'])->name('leads.convert');
        Route::resource('customers', \App\Http\Controllers\CustomerController::class);
        Route::post('customers/{customer}/generate-password', [\App\Http\Controllers\CustomerController::class, 'generatePassword'])->name('customers.password.generate');
        Route::post('customers/{customer}/documents', [\App\Http\Controllers\CustomerController::class, 'uploadDocument'])->name('customers.documents.store');
        Route::patch('customer-documents/{document}/verify', [\App\Http\Controllers\CustomerController::class, 'verifyDocument'])->name('customer.documents.verify');
        Route::patch('customer-documents/{document}/reject', [\App\Http\Controllers\CustomerController::class, 'rejectDocument'])->name('customer.documents.reject');
        
        // Inventory & Properties
        Route::resource('projects', \App\Http\Controllers\ProjectController::class);
        Route::resource('blocks', \App\Http\Controllers\BlockController::class);
        Route::resource('properties', \App\Http\Controllers\PropertyController::class);
        Route::post('properties/{property}/documents', [\App\Http\Controllers\PropertyDocumentController::class, 'store'])->name('properties.documents.store');
        Route::delete('properties/{property}/documents/{document}', [\App\Http\Controllers\PropertyDocumentController::class, 'destroy'])->name('properties.documents.destroy');
        Route::patch('property-documents/{document}/verify', [\App\Http\Controllers\PropertyDocumentController::class, 'verify'])->name('properties.documents.verify');
        Route::patch('property-documents/{document}/reject', [\App\Http\Controllers\PropertyDocumentController::class, 'reject'])->name('properties.documents.reject');
        
        // Customer Property Interests
        Route::post('customers/{customer}/property-interests', [\App\Http\Controllers\CustomerPropertyInterestController::class, 'store'])->name('customers.property-interests.store');
        
        // Agents & HR
        Route::resource('agents', \App\Http\Controllers\AgentProfileController::class);
        Route::get('/agents/{agent}/dashboard', [\App\Http\Controllers\AgentProfileController::class, 'dashboard'])->name('agents.dashboard');
        Route::resource('follow-ups', \App\Http\Controllers\FollowUpController::class)->only(['index', 'store']);
        Route::patch('follow-ups/{followUp}/status', [\App\Http\Controllers\FollowUpController::class, 'updateStatus'])->name('follow-ups.status');
        
        // Reports
        Route::get('reports/export/{report}', [\App\Http\Controllers\ReportController::class, 'export'])->name('reports.export');
        Route::resource('reports', \App\Http\Controllers\ReportController::class);
        
        // Sales & Finance (Phase 10 & 11)
        Route::resource('quotations', \App\Http\Controllers\QuotationController::class);
        Route::resource('expenses', \App\Http\Controllers\ExpenseController::class);
        Route::resource('bookings', \App\Http\Controllers\BookingController::class);
        Route::resource('rentals', \App\Http\Controllers\RentalController::class);
        Route::post('bookings/{booking}/approve', [\App\Http\Controllers\BookingController::class, 'approve'])->name('bookings.approve');
        
        // Agreements
        Route::get('agreements', [\App\Http\Controllers\AgreementController::class, 'index'])->name('agreements.index');
        Route::get('agreements/{booking}', [\App\Http\Controllers\AgreementController::class, 'show'])->name('agreements.show');
        Route::post('bookings/{booking}/documents', [\App\Http\Controllers\BookingController::class, 'uploadDocument'])->name('bookings.documents.upload');
        Route::delete('booking-documents/{document}', [\App\Http\Controllers\BookingController::class, 'deleteDocument'])->name('bookings.documents.destroy');
        Route::patch('booking-documents/{document}/verify', [\App\Http\Controllers\BookingController::class, 'verifyDocument'])->name('bookings.documents.verify');
        Route::patch('booking-documents/{document}/reject', [\App\Http\Controllers\BookingController::class, 'rejectDocument'])->name('bookings.documents.reject');
        
        // Dealers
        Route::resource('dealers', \App\Http\Controllers\DealerController::class);
        
        // Vendors
        Route::resource('vendors', \App\Http\Controllers\VendorController::class);
        
        // Site Surveys
        Route::resource('site-surveys', \App\Http\Controllers\SiteSurveyController::class);
        
        // Transactions
        Route::get('transactions', [\App\Http\Controllers\TransactionController::class, 'index'])->name('transactions.index');
        Route::get('transactions/create', [\App\Http\Controllers\TransactionController::class, 'create'])->name('transactions.create');
        Route::post('transactions', [\App\Http\Controllers\TransactionController::class, 'store'])->name('transactions.store');
        Route::post('transactions/{transaction}/approve', [\App\Http\Controllers\TransactionController::class, 'approve'])->name('transactions.approve');
        Route::post('transactions/{transaction}/reject', [\App\Http\Controllers\TransactionController::class, 'reject'])->name('transactions.reject');
        Route::delete('transactions/{transaction}', [\App\Http\Controllers\TransactionController::class, 'destroy'])->name('transactions.destroy');
        
        Route::resource('tokens', \App\Http\Controllers\TokenController::class);
        Route::resource('installment-plans', \App\Http\Controllers\InstallmentPlanController::class);
        Route::resource('invoices', \App\Http\Controllers\InvoiceController::class);

        // User Preferences & Customization API
        Route::post('/preferences/columns', [\App\Http\Controllers\UserPreferenceController::class, 'updateColumns'])->name('preferences.columns');
        Route::post('/preferences/filters', [\App\Http\Controllers\UserPreferenceController::class, 'saveFilter'])->name('preferences.filters');
        
        // Imports
        Route::post('/import/{type}', [\App\Http\Controllers\ImportController::class, 'import'])->name('import');
        Route::get('/import/sample/{type}', [\App\Http\Controllers\ImportController::class, 'downloadSample'])->name('import.sample');

        // Reports
        Route::get('reports/standard/{preset}', [\App\Http\Controllers\ReportController::class, 'generateStandard'])->name('reports.standard');
        Route::resource('reports', \App\Http\Controllers\ReportController::class);

        // Global Search API
        Route::get('/search', [\App\Http\Controllers\SearchController::class, 'globalSearch'])->name('search.global');

        // Settings & Integrations
        Route::get('/settings', [App\Http\Controllers\SettingController::class, 'index'])->name('settings.index');
        Route::post('/settings', [App\Http\Controllers\SettingController::class, 'store'])->name('settings.store');
        
        // System Tools
        Route::post('/system/reset', [\App\Http\Controllers\SystemController::class, 'resetDatabase'])->name('system.reset');
        Route::post('/system/backup', [\App\Http\Controllers\SystemController::class, 'backupDatabase'])->name('system.backup');
        
        // Activity Logs
        Route::get('/activity-logs', [App\Http\Controllers\ActivityLogController::class, 'index'])->name('activity-logs.index');

        // AI Recommendations
        Route::get('/ai-recommendations', [\App\Http\Controllers\AiRecommendationController::class, 'index'])->name('ai.recommendations');

        // Master Data
        Route::resource('master-data', \App\Http\Controllers\MasterDataController::class)->except(['create', 'show', 'edit'])->parameters(['master-data' => 'masterData']);

        // OTP Admin
        Route::get('otp-requests', [\App\Http\Controllers\OtpController::class, 'adminIndex'])->name('admin.otp.index');
        Route::post('otp-requests/{otpRequest}/generate', [\App\Http\Controllers\OtpController::class, 'adminGenerate'])->name('admin.otp.generate');

        // Installments
        Route::delete('/installments/{installment}', [\App\Http\Controllers\InstallmentController::class, 'destroy'])->name('installments.destroy');

        // Property Transfers
        Route::resource('property-transfers', \App\Http\Controllers\PropertyTransferController::class);
        Route::delete('/installments/{installment}', [\App\Http\Controllers\InstallmentController::class, 'destroy'])->name('installments.destroy');

        // Roles and Users
        Route::resource('users', \App\Http\Controllers\UserController::class);
        Route::resource('roles', \App\Http\Controllers\RoleController::class);
    });
});
