<?php

use App\Http\Controllers\ReportController;
use App\Http\Controllers\RoleController;
use App\Http\Controllers\AuditLogController;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\{
    UserController,
    DashboardController,
    LeadController,
    MetaController,
    CrudController,
    AuthController,
    PipelineController,
    FollowUpController,
    WhatsAppController,
    NotificationController,
    QuotationController,
    CustomerController
};

// Guest Authentication Routes
Route::get('/login', [AuthController::class, 'login'])->name('login');
Route::post('/login', [AuthController::class, 'authenticate'])->name('login.submit');
Route::post('/logout', [AuthController::class, 'logout'])->name('logout');

// Authenticated & Permission-Guarded Routes
Route::middleware('session.auth')->group(function () {
    
    // 1. Dashboard
    Route::get('/', fn () => redirect()->route('dashboard'));
    Route::get('/dashboard', [DashboardController::class, 'index'])
        ->middleware('permission:dashboard.view')
        ->name('dashboard');

    // 2. Users Management
    Route::get('/users', [UserController::class, 'index'])->middleware('permission:users.view')->name('users');
    Route::get('/users/create', [UserController::class, 'create'])->middleware('permission:users.create')->name('users.create');
    Route::post('/users', [UserController::class, 'store'])->middleware('permission:users.create')->name('users.store');
    Route::post('/users/bulk-action', [UserController::class, 'bulkAction'])->middleware('permission:users.edit')->name('users.bulk_action');
    Route::get('/users/{user}', [UserController::class, 'show'])->middleware('permission:users.view')->name('users.show');
    Route::get('/users/{user}/edit', [UserController::class, 'edit'])->middleware('permission:users.edit')->name('users.edit');
    Route::put('/users/{user}', [UserController::class, 'update'])->middleware('permission:users.edit')->name('users.update');
    Route::patch('/users/{user}/toggle', [UserController::class, 'toggleStatus'])->middleware('permission:users.edit')->name('users.toggle');
    Route::patch('/users/{user}/lock', [UserController::class, 'toggleLock'])->middleware('permission:users.edit')->name('users.lock');
    Route::post('/users/{user}/reset-password', [UserController::class, 'resetPassword'])->middleware('permission:users.edit')->name('users.reset_password');
    Route::post('/users/{user}/permission-overrides', [UserController::class, 'updatePermissionOverrides'])->middleware('permission:roles.manage_permissions')->name('users.permission_overrides');
    Route::delete('/users/{user}', [UserController::class, 'destroy'])->middleware('permission:users.delete')->name('users.destroy');

    // 3. Roles & Permissions
    Route::get('/roles', [RoleController::class, 'index'])->middleware('permission:roles.view')->name('roles.index');
    Route::get('/roles/create', [RoleController::class, 'create'])->middleware('permission:roles.create')->name('roles.create');
    Route::post('/roles', [RoleController::class, 'store'])->middleware('permission:roles.create')->name('roles.store');
    Route::get('/roles/{role}/edit', [RoleController::class, 'edit'])->middleware('permission:roles.edit')->name('roles.edit');
    Route::put('/roles/{role}', [RoleController::class, 'update'])->middleware('permission:roles.edit')->name('roles.update');
    Route::get('/roles/{role}/permissions', [RoleController::class, 'permissions'])->middleware('permission:roles.manage_permissions')->name('roles.permissions');
    Route::post('/roles/{role}/permissions', [RoleController::class, 'updatePermissions'])->middleware('permission:roles.manage_permissions')->name('roles.permissions.update');
    Route::post('/roles/{role}/duplicate', [RoleController::class, 'duplicate'])->middleware('permission:roles.create')->name('roles.duplicate');
    Route::patch('/roles/{role}/toggle', [RoleController::class, 'toggleStatus'])->middleware('permission:roles.edit')->name('roles.toggle');
    Route::delete('/roles/{role}', [RoleController::class, 'destroy'])->middleware('permission:roles.delete')->name('roles.destroy');

    // 4. Audit Logs
    Route::get('/audit-logs', [AuditLogController::class, 'index'])->middleware('permission:audit_logs.view')->name('audit.index');
    Route::get('/audit-logs/export', [AuditLogController::class, 'export'])->middleware('permission:audit_logs.export')->name('audit.export');

    // 5. Leads
    Route::get('/leads', [LeadController::class, 'index'])->middleware('permission:leads.view')->name('leads.index');
    Route::get('/leads/create', [LeadController::class, 'create'])->middleware('permission:leads.create')->name('leads.create');
    Route::post('/leads', [LeadController::class, 'store'])->middleware('permission:leads.create')->name('leads.store');
    Route::get('/leads/{lead}', [LeadController::class, 'show'])->middleware('permission:leads.view')->name('leads.show');
    Route::get('/leads/{lead}/edit', [LeadController::class, 'edit'])->middleware('permission:leads.edit')->name('leads.edit');
    Route::put('/leads/{lead}', [LeadController::class, 'update'])->middleware('permission:leads.edit')->name('leads.update');
    Route::delete('/leads/{lead}', [LeadController::class, 'destroy'])->middleware('permission:leads.delete')->name('leads.destroy');
    Route::post('/leads/{lead}/convert', [CustomerController::class, 'convertLead'])->middleware('permission:leads.edit')->name('leads.convert');

    // 6. Pipeline
    Route::get('/pipeline', [PipelineController::class, 'index'])->middleware('permission:pipeline.view')->name('pipeline');
    Route::patch('/pipeline/leads/{lead}', [PipelineController::class, 'update'])->middleware('permission:pipeline.edit')->name('pipeline.update');

    // 7. Follow-ups
    Route::get('/followups', [CrudController::class, 'followups'])->middleware('permission:leads.view')->name('followups');
    Route::post('/followups', [FollowUpController::class, 'store'])->middleware('permission:leads.edit')->name('followups.store');
    Route::patch('/followups/{followUp}/complete', [FollowUpController::class, 'complete'])->middleware('permission:leads.edit')->name('followups.complete');

    // 8. Customers
    Route::get('/customers', [CustomerController::class, 'index'])->middleware('permission:customers.view')->name('customers');
    Route::post('/customers', [CustomerController::class, 'store'])->middleware('permission:customers.create')->name('customers.store');
    Route::put('/customers/{customer}', [CustomerController::class, 'update'])->middleware('permission:customers.edit')->name('customers.update');
    Route::delete('/customers/{customer}', [CustomerController::class, 'destroy'])->middleware('permission:customers.delete')->name('customers.destroy');

    // 9. Quotations
    Route::get('/quotations', [QuotationController::class, 'index'])->middleware('permission:quotations.view')->name('quotations.index');
    Route::get('/quotations/create', [QuotationController::class, 'create'])->middleware('permission:quotations.create')->name('quotations.create');
    Route::post('/quotations', [QuotationController::class, 'store'])->middleware('permission:quotations.create')->name('quotations.store');
    Route::get('/quotations/{quotation}', [QuotationController::class, 'show'])->middleware('permission:quotations.view')->name('quotations.show');
    Route::patch('/quotations/{quotation}/status', [QuotationController::class, 'status'])->middleware('permission:quotations.approve')->name('quotations.status');
    Route::get('/quotations/{quotation}/print', [QuotationController::class, 'print'])->middleware('permission:quotations.print')->name('quotations.print');

    // 10. Reports & Analytics
    Route::get('/reports', [ReportController::class, 'index'])->middleware('permission:reports.view')->name('reports.index');
    Route::get('/analytics', [CrudController::class, 'analytics'])->middleware('permission:analytics.view')->name('analytics');
    Route::get('/products', [CrudController::class, 'products'])->middleware('permission:dashboard.view')->name('products');

    // 11. WhatsApp & Meta
    Route::get('/whatsapp', [WhatsAppController::class, 'index'])->middleware('permission:whatsapp.view')->name('whatsapp');
    Route::post('/whatsapp/send', [WhatsAppController::class, 'send'])->middleware('permission:whatsapp.send')->name('whatsapp.send');
    Route::get('/notifications', [NotificationController::class, 'index'])->middleware('permission:dashboard.view')->name('notifications');
    Route::patch('/notifications/{notification}/read', [NotificationController::class, 'read'])->middleware('permission:dashboard.view')->name('notifications.read');
    Route::get('/meta', [MetaController::class, 'index'])->middleware('permission:meta.view')->name('meta.index');
    Route::get('/meta/connect', [MetaController::class, 'redirect'])->middleware('permission:meta.manage')->name('meta.connect');
    Route::get('/meta/callback', [MetaController::class, 'callback'])->middleware('permission:meta.manage')->name('meta.callback');
    Route::post('/meta/pages/{page}/subscribe', [MetaController::class, 'subscribe'])->middleware('permission:meta.manage')->name('meta.subscribe');
    Route::get('/meta/pages/{page}/forms', [MetaController::class, 'forms'])->middleware('permission:meta.view')->name('meta.forms');
});

require __DIR__ . '/v9.php';
require __DIR__ . '/v10.php';
