<?php

use App\Http\Controllers\AccountController;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Auth\RegisterController;
use App\Http\Controllers\BusinessController;
use App\Http\Controllers\CampaignController;
use App\Http\Controllers\CompanyController;
use App\Http\Controllers\ContactController;
use App\Http\Controllers\DealController;
use App\Http\Controllers\LeadController;
use App\Http\Controllers\ProductCategoryController;
use App\Http\Controllers\ProductController;
use App\Http\Controllers\ProductUnitController;
use App\Http\Controllers\ScheduleActivityController;
use App\Http\Controllers\TicketController;
use App\Http\Controllers\WonLostController;
use App\Http\Controllers\RoleController;
use App\Http\Controllers\PermissionController;
use App\Http\Controllers\UserRoleController;
use App\Http\Controllers\MyActivitiesController;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\TeamController;
use App\Http\Controllers\DepartmentController;
use App\Http\Controllers\EmployeeController;
use App\Http\Controllers\AuditLogController;
use App\Http\Controllers\IntegrationController;
use App\Http\Controllers\WorkflowModuleController;
use App\Http\Controllers\PriceBookController;
use App\Http\Controllers\RecycleBinController;
use App\Http\Controllers\TradeDocumentController;
use App\Http\Controllers\ProjectController;
use App\Http\Controllers\CaseController;
use App\Http\Controllers\SurveyController;
use App\Http\Controllers\ReportingController;
use App\Http\Controllers\CrmFileController;
use App\Http\Controllers\IntegrationWebhookController;
use App\Http\Controllers\NotificationPreferenceController;
use App\Http\Controllers\ReminderEscalationSettingController;
use App\Http\Controllers\ActivityWatcherController;
use App\Http\Controllers\PaymentController;
use App\Http\Controllers\NotificationCenterController;
use App\Http\Controllers\ActivityDeliveryController;
use App\Http\Controllers\RelatedActivityController;
use App\Http\Controllers\OperationalDocumentController;
use App\Http\Controllers\OperationsFoundationController;
use App\Http\Controllers\CampaignAudienceController;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;


Route::get('/login', [LoginController::class, 'showLoginForm'])->name('login');
Route::post('/login', [LoginController::class, 'login']);
Route::post('/logout', [LoginController::class, 'logout'])->name('logout');

Route::get('/user-register', [RegisterController::class, 'showRegisterForm'])->name('userRegister');
Route::post('/user-register', [RegisterController::class, 'register'])->name('userRegisterPost');


// Email verification routes
Route::get('/email/verify', [RegisterController::class, 'showVerificationNotice'])->name('verification.notice');
// Route::get('/email/verify/{id}/{hash}', [RegisterController::class, 'verify'])
//     ->middleware(['signed', 'throttle:6,1'])
//     ->name('verification.verify');

// Add this route to web.php for TESTING ONLY
Route::get('/email/verify-test/{id}/{hash}', [RegisterController::class, 'verify'])
    ->name('verification.verify');

Route::post('/email/verification-notification', [RegisterController::class, 'resendVerificationEmail'])
    ->middleware(['throttle:6,1'])
    ->name('verification.send');

Route::middleware(['auth', 'verified'])->group(function () {
    Route::get('business', [BusinessController::class, 'index'])->name('business.index');
    Route::put('business/{id}', [BusinessController::class, 'update'])->name('business.update');
    Route::post('business', [BusinessController::class, 'store'])->name('business.store');
});


Route::middleware(['auth', 'verified', 'business.complete'])->group(function () {
    Route::get('/settings/integrations', [IntegrationController::class, 'index'])
        ->name('integrations.index');
    Route::redirect('/integrations', '/settings/integrations');
    Route::put('/settings/integrations/{provider}', [IntegrationController::class, 'update'])
        ->name('integrations.update');
    Route::post('/settings/integrations/{provider}/test', [IntegrationController::class, 'test'])
        ->name('integrations.test');
    Route::post('/settings/integrations/mappings', [IntegrationController::class, 'mappings'])
        ->name('integrations.mappings.store');
    Route::delete('/settings/integrations/mappings/{mapping}', [IntegrationController::class, 'destroyMapping'])
        ->name('integrations.mappings.destroy');
    Route::post('/settings/integrations/logs/{log}/retry', [IntegrationController::class, 'retry'])
        ->name('integrations.logs.retry');
    Route::post('/integrations/facebook/sync', [IntegrationController::class, 'syncFacebook'])
        ->name('integrations.facebook.sync');
    Route::post('/integrations/mailchimp/sync', [IntegrationController::class, 'syncMailchimp'])
        ->name('integrations.mailchimp.sync');
    Route::post('/integrations/whatsapp/send', [IntegrationController::class, 'sendWhatsApp'])
        ->name('integrations.whatsapp.send');
    Route::post('/integrations/jira/issues', [IntegrationController::class, 'createJiraIssue'])
        ->name('integrations.jira.issue');

    Route::get('/audit-logs', [AuditLogController::class, 'index'])->name('audit-logs.index');
    Route::get('/audit-logs/export', [AuditLogController::class, 'export'])->name('audit-logs.export');
    // dashboard routes
    // Route::get('/', function () {
    //     return view('index');
    // })->name('index');

    Route::get('/', [HomeController::class, 'index'])->name('index');
    Route::get('/my-dashboard', [HomeController::class, 'myDashboard'])->name('my-dashboard');

    // Role Management Routes
    Route::resource('roles', RoleController::class);
    
    // Permission Management Routes
    Route::resource('permissions', PermissionController::class);
    Route::resource('teams', TeamController::class)->except('show');
    Route::get('/teams/{team}/activity', [TeamController::class, 'activity'])->name('teams.activity');
    Route::resource('departments', DepartmentController::class)->except('show');
    Route::resource('employees', EmployeeController::class)->except('show');
    
    // User Role Assignment Routes
    Route::get('user-roles', [UserRoleController::class, 'index'])->name('user-roles.index');
    Route::get('/user/create', [UserRoleController::class, 'create'])->name('user.create');
    Route::post('/user/store', [UserRoleController::class, 'store'])->name('user.store');
    Route::get('user-roles/{user}/edit', [UserRoleController::class, 'edit'])->name('user-roles.edit');
    Route::put('user-roles/{user}', [UserRoleController::class, 'update'])->name('user-roles.update');
    Route::delete('user-roles/{user}/destroy', [UserRoleController::class, 'destroy'])->name('user-roles.destroy');

    //contacts
    Route::get('/contacts', [ContactController::class, 'index'])->name('contacts');
    Route::get('/contacts/export/{format}', [ContactController::class, 'export'])->name('contacts.export');
    Route::get('/contacts/import/template', [ContactController::class, 'importTemplate'])->name('contacts.import-template');
    Route::post('/contacts/import', [ContactController::class, 'import'])->name('contacts.import');
    Route::get('/contacts-create', [ContactController::class, 'create'])->name('contacts-create');
    Route::get('/contacts-view', [ContactController::class, 'show'])->name('contacts.view');
    Route::post('/contacts/store', [ContactController::class, 'store'])->name('contacts.store');
    Route::get('/contacts-edit', [ContactController::class, 'edit'])->name('contacts.edit');
    Route::put('/contacts/update/{id}', [ContactController::class, 'update'])->name('contacts.update');
    Route::delete('/contacts/{id}', [ContactController::class, 'destroy'])->name('contacts.destroy');
    Route::post('/assign-lead', [ContactController::class, 'assignLead'])->name('assign-lead');
    Route::post('/assign-deal', [ContactController::class, 'assignDeal'])->name('assign-deal');
    Route::post('/assign-campaign', [ContactController::class, 'assignCampaign'])->name('assign-campaign');
    Route::post('/notes/store', [ContactController::class, 'noteStore'])->name('notes.store');
    // Route::post('/contacts/{id}/update-stage', [ContactController::class, 'updateStage']);
    Route::post('/contacts/update-stage', [ContactController::class, 'updateStage'])->name('contacts.update-stage');

    //leads
    Route::get('/leads', [LeadController::class, 'index'])->name('leads');
    Route::get('/leads/options', [LeadController::class, 'options'])->name('leads.options');
    Route::get('/leads/export/{format}', [LeadController::class, 'export'])->name('leads.export');
    Route::get('/leads-dashboard', [LeadController::class, 'dashboard'])->name('leads-dashboard');
    Route::get('/leads-create', [LeadController::class, 'create'])->name('leads-create');
    Route::get('/leads-view', [LeadController::class, 'show'])->name('leads.view');
    Route::post('/leads/store', [LeadController::class, 'store'])->name('leads.store');
    Route::get('/leads-edit', [LeadController::class, 'edit'])->name('leads.edit');
    Route::put('/leads/update/{id}', [LeadController::class, 'update'])->name('leads.update');
    Route::patch('/leads/{id}/profile-field', [LeadController::class, 'updateProfileField'])->name('leads.profile-field.update');
    Route::post('/leads/{lead}/products', [LeadController::class, 'addProduct'])->name('leads.products.store');
    Route::delete('/leads/{lead}/products/{product}', [LeadController::class, 'removeProduct'])->name('leads.products.destroy');
    Route::post('/leads/{id}/assign-employee', [LeadController::class, 'assignEmployee'])->name('leads.assign-employee');
    Route::delete('/leads/{id}', [LeadController::class, 'destroy'])->name('leads.destroy');
    Route::post('/update-lead-stage/{leadId}', [LeadController::class, 'updateStage'])->name('update-lead-stage');
    Route::post('/leads/{id}/update-lead-stage', [LeadController::class, 'updateLeadStage']);
    Route::post('/leads/{lead}/reopen', [LeadController::class, 'reopen'])->name('leads.reopen');
    Route::post('/update-stage-status', [LeadController::class, 'updateStageStatus'])->name('update.stage.status');
    Route::get('/leads/{id}/conversion-review', [LeadController::class, 'conversionReview'])
        ->name('leads.conversion-review');

    //Deals
    Route::get('/deals', [DealController::class, 'index'])->name('deals');
    Route::get('/deals-dashboard', [DealController::class, 'dashboard'])->name('deals-dashboard');
    Route::get('/deals-loss-analysis', [DealController::class, 'lossAnalysis'])->name('deals.loss-analysis');
    Route::get('/deals-create', [DealController::class, 'create'])->name('deals-create');
    Route::get('/deals-view', [DealController::class, 'show'])->name('deals.view');
    Route::post('/deals/store', [DealController::class, 'store'])->name('deals.store');
    Route::get('/deals-edit', [DealController::class, 'edit'])->name('deals.edit');
    Route::put('/deals/update/{id}', [DealController::class, 'update'])->name('deals.update');
    Route::delete('/deals/{id}', [DealController::class, 'destroy'])->name('deals.destroy');
    Route::post('/update-deal-stage/{dealId}', [DealController::class, 'updateStage'])->name('update-deal-stage');
    Route::post('/deals/{id}/update-deal-stage', [DealController::class, 'updateDealStage']);
    Route::post('/deals/{deal}/close-won', [DealController::class, 'storeWonClosureDetails'])
        ->name('deals.close-won');
    Route::get('/deals/{deal}/closure-approval', [DealController::class, 'closureApprovalPage'])
        ->name('deals.closure-approval.page');
    Route::post('/deals/{deal}/closure-approval', [DealController::class, 'closureApproval'])
        ->name('deals.closure-approval');
    Route::post('/deals/{deal}/reopen', [DealController::class, 'reopen'])->name('deals.reopen');
    Route::post('/deals/{deal}/competitors', [DealController::class, 'storeCompetitor'])->name('deals.competitors.store');
    Route::delete('/deals/{deal}/competitors/{competitor}', [DealController::class, 'destroyCompetitor'])
        ->name('deals.competitors.destroy');
    Route::post('/deals/{deal}/products', [DealController::class, 'addProduct'])
        ->name('deals.products.store');
    Route::delete('/deals/{deal}/products/{line}', [DealController::class, 'removeProduct'])
        ->name('deals.products.destroy');
    Route::post('/deals/{deal}/discount-approvals/{approval}', [DealController::class, 'decideDiscount'])
        ->name('deals.discount-approvals.decide');
    Route::post('/deals/{deal}/files', [DealController::class, 'uploadFile'])
        ->name('deals.files.store');
    Route::get('/deals/{deal}/files/{file}', [DealController::class, 'viewFile'])
        ->name('deals.files.view');
    Route::get('/deals/{deal}/files/{file}/download', [DealController::class, 'downloadFile'])
        ->name('deals.files.download');
    Route::post('/update-deal-stage-status', [DealController::class, 'updateStageStatus'])
        ->name('update.deal.stage.status');


    //won or loss
    Route::post('/won-lost/store-or-update', [WonLostController::class, 'storeOrUpdate'])->name('won-lost.storeOrUpdate');


    //Account
    Route::get('/account', [AccountController::class, 'index'])->name('account');
    Route::get('/account-create', [AccountController::class, 'create'])->name('account-create');
    Route::get('/account-view', [AccountController::class, 'show'])->name('account.view');
    Route::post('/account/store', [AccountController::class, 'store'])->name('account.store');
    Route::get('/account-edit', [AccountController::class, 'edit'])->name('account.edit');
    Route::put('/account/update/{id}', [AccountController::class, 'update'])->name('account.update');
    Route::delete('/account/{id}', [AccountController::class, 'destroy'])->name('account.destroy');

    //Application Schedule Activity

    Route::get('/activity', [ScheduleActivityController::class, 'index'])->name('schedule-activity');
    Route::get('/activity-create', [ScheduleActivityController::class, 'create'])->name('schedule-activity-create');
    Route::get('/activity/schedules', [ScheduleActivityController::class, 'getSchedules'])->name('activities.schedules');
    Route::post('/activity/store', [ScheduleActivityController::class, 'store'])->name('activities.store');
    Route::post('/activity/attachment-requirements', [ScheduleActivityController::class, 'saveAttachmentRequirement'])->name('activities.attachment-requirements.store');
    Route::get('/activity-edit', [ScheduleActivityController::class, 'edit'])->name('activities.edit');
    Route::post('/activities/{activity}/watchers', [ActivityWatcherController::class, 'store'])->name('activities.watchers.store');
    Route::delete('/activities/{activity}/watchers', [ActivityWatcherController::class, 'destroy'])->name('activities.watchers.destroy');
    Route::put('/activity/update/{id}', [ScheduleActivityController::class, 'update'])->name('activities.update');
    Route::delete('/activities/{id}', [ScheduleActivityController::class, 'destroy'])->name('activities.destroy');

    // Route::get('/activity-view', [ScheduleActivityController::class, 'show'])->name('activity-view');

    //Campaign

    Route::get('/campaigns', [CampaignController::class, 'index'])->name('campaigns');
    Route::get('/campaign-template-types', [CampaignController::class, 'templateTypes'])->name('campaign-template-types');
    Route::post('/campaign-template-types', [CampaignController::class, 'storeTemplateType'])->name('campaign-template-types.store');
    Route::put('/campaign-template-types/{campaignType}', [CampaignController::class, 'updateTemplateType'])->name('campaign-template-types.update');
    Route::delete('/campaign-template-types/{campaignType}', [CampaignController::class, 'destroyTemplateType'])->name('campaign-template-types.destroy');
    Route::get('/campaigns-create', [CampaignController::class, 'create'])->name('campaigns-create');
    Route::get('/campaigns-view', [CampaignController::class, 'show'])->name('campaigns.view');
    Route::post('/campaigns/store', [CampaignController::class, 'store'])->name('campaigns.store');
    Route::get('/campaigns-edit', [CampaignController::class, 'edit'])->name('campaigns.edit');
    Route::put('/campaigns/update/{id}', [CampaignController::class, 'update'])->name('campaigns.update');
    Route::delete('/campaigns/{id}', [CampaignController::class, 'destroy'])->name('campaigns.destroy');
    Route::get('/campaigns-email', [CampaignController::class, 'bulkmail'])->name('campaigns.email');
    Route::get('/campaigns/{campaign}/audience', [CampaignAudienceController::class, 'index'])->name('campaigns.audience');
    Route::get('/marketing-dashboard', [CampaignController::class, 'dashboard'])->name('campaigns.dashboard');
    Route::post('/campaigns/{campaign}/audience', [CampaignAudienceController::class, 'add'])->name('campaigns.audience.add');
    Route::delete('/campaigns/{campaign}/audience/{target}', [CampaignAudienceController::class, 'remove'])->name('campaigns.audience.remove');
    Route::post('/campaigns/{campaign}/audience/{target}/convert', [CampaignAudienceController::class, 'convert'])->name('campaigns.audience.convert');
    Route::post('/campaigns/{campaign}/sync/{provider}', [CampaignAudienceController::class, 'sync'])->name('campaigns.audience.sync');
    Route::post('/campaigns/{campaign}/sync-logs/{log}/retry', [CampaignAudienceController::class, 'retry'])->name('campaigns.audience.retry');


    //products

    Route::get('/products', [ProductController::class, 'index'])->name('products');
    Route::get('/products-create', [ProductController::class, 'create'])->name('products-create');
    Route::get('/products-view', [ProductController::class, 'show'])->name('products.view');
    Route::post('/products/store', [ProductController::class, 'store'])->name('products.store');
    Route::get('/products-edit', [ProductController::class, 'edit'])->name('products.edit');
    Route::put('/products/update/{id}', [ProductController::class, 'update'])->name('products.update');
    Route::delete('/products/{id}', [ProductController::class, 'destroy'])->name('products.destroy');

    //product category
    Route::get('/category-create', [ProductCategoryController::class, 'create'])->name('category-create');
    Route::post('/products/category/store', [ProductCategoryController::class, 'store'])->name('productCategory.store');
    Route::get('/category-edit', [ProductCategoryController::class, 'edit'])->name('productCategory.edit');
    Route::put('/products/category/update/{id}', [ProductCategoryController::class, 'update'])->name('productCategory.update');


    //product Unit
    Route::get('/unit-create', [ProductUnitController::class, 'create'])->name('unit-create');
    Route::post('/products/unit/store', [ProductUnitController::class, 'store'])->name('productUnit.store');
    Route::get('/unit-edit', [ProductUnitController::class, 'edit'])->name('productUnit.edit');
    Route::put('/products/unit/update/{id}', [ProductUnitController::class, 'update'])->name('productUnit.update');

    //comapny
    Route::post('/companies/store', [CompanyController::class, 'store'])->name('companies.store');


    // //business
    // Route::get('business', [BusinessController::class, 'index'])->name('business.index');
    // // Route::get('business/{id}/edit', [BusinessController::class, 'edit'])->name('business.edit');
    // Route::put('business/{id}', [BusinessController::class, 'update'])->name('business.update');
    // Route::post('business', [BusinessController::class, 'store'])->name('business.store');
    // // Route::post('/business/save', [BusinessController::class, 'save'])->name('business.save');


    //ticket
    Route::get('/tickets', [TicketController::class, 'index'])->name('tickets');
    Route::get('/tickets-dashboard', [TicketController::class, 'dashboard'])->name('tickets-dashboard');
    Route::post('/update-ticket-stage/{ticketId}', [TicketController::class, 'updateStage'])->name('update-ticket-stage');
    Route::get('/ticket-create', [TicketController::class, 'create'])->name('ticket-create');
    Route::get('/tickets-view', [TicketController::class, 'show'])->name('ticket.view');
    Route::post('/tickets/store', [TicketController::class, 'store'])->name('ticket.store');
    Route::get('/tickets-edit', [TicketController::class, 'edit'])->name('ticket.edit');
    Route::put('/tickets/update/{id}', [TicketController::class, 'update'])->name('ticket.update');
    Route::delete('/tickets/{id}', [TicketController::class, 'destroy'])->name('ticket.destroy');
    Route::get('/lists', [TicketController::class, 'activitiesKList'])->name('activities-list');
    Route::post('/tickets/{id}/update-ticket-stage', [TicketController::class, 'updateTicketStage']);
    Route::post('/update-ticket-stage-status', [TicketController::class, 'updateStageStatus'])
        ->name('update.ticket.stage.status');

    //call
    Route::get('/call-create', [TicketController::class, 'callCreate'])->name('call-create');
    Route::post('/calls/store', [TicketController::class, 'callStore'])->name('call.store');
    Route::get('/calls-edit', [TicketController::class, 'callEdit'])->name('call.edit');
    Route::put('/calls/update/{id}', [TicketController::class, 'callUpdate'])->name('call.update');
    Route::delete('/calls/{id}', [TicketController::class, 'callDestroy'])->name('call.destroy');


    //task
    Route::get('/task-create', [TicketController::class, 'taskCreate'])->name('task-create');
    Route::post('/tasks/store', [TicketController::class, 'taskStore'])->name('task.store');
    Route::get('/tasks-edit', [TicketController::class, 'taskEdit'])->name('task.edit');
    Route::put('/tasks/update/{id}', [TicketController::class, 'taskUpdate'])->name('task.update');
    Route::delete('/tasks/{id}', [TicketController::class, 'taskDestroy'])->name('task.destroy');

    //event
    Route::get('/event-create', [TicketController::class, 'eventCreate'])->name('event-create');
    Route::post('/events/store', [TicketController::class, 'eventStore'])->name('event.store');
    Route::get('/events-edit', [TicketController::class, 'eventEdit'])->name('event.edit');
    Route::put('/events/update/{id}', [TicketController::class, 'eventUpdate'])->name('event.update');
    Route::delete('/events/{id}', [TicketController::class, 'eventDestroy'])->name('event.destroy');

    Route::get('/myactivities', [MyActivitiesController::class, 'index'])->name('myactivities');
    Route::get('/salesactivities', [MyActivitiesController::class, 'saleindex'])->name('salesactivities');

    Route::get('/workflow-related-records', [WorkflowModuleController::class, 'relatedRecords'])
        ->name('workflow.related-records');
    Route::get('/workflow/{module}', [WorkflowModuleController::class, 'index'])
        ->whereIn('module', ['email-templates', 'messages', 'notes', 'follow-ups', 'todos', 'projects', 'cases', 'surveys', 'trade-documents'])
        ->name('workflow.index');
    Route::post('/workflow/{module}', [WorkflowModuleController::class, 'store'])->name('workflow.store');
    Route::put('/workflow/todos/{todo}/comments', [WorkflowModuleController::class, 'updateTodoComments'])
        ->name('workflow.todos.comments.update');
    Route::put('/workflow/{module}/{id}', [WorkflowModuleController::class, 'update'])->name('workflow.update');
    Route::delete('/workflow/{module}/{id}', [WorkflowModuleController::class, 'destroy'])->name('workflow.destroy');
    Route::post('/workflow/{module}/{id}/restore', [WorkflowModuleController::class, 'restore'])->name('workflow.restore');
    Route::get('/operations/{type}', [OperationalDocumentController::class, 'index'])
        ->whereIn('type', ['sales-orders','contracts','handovers','deliveries','invoices'])
        ->name('operational-documents.index');
    Route::post('/operations/{type}', [OperationalDocumentController::class, 'store'])
        ->whereIn('type', ['sales-orders','contracts','handovers','deliveries','invoices'])
        ->name('operational-documents.store');
    Route::put('/operations/{document}/details', [OperationalDocumentController::class, 'updateDetails'])
        ->name('operational-documents.details.update');
    Route::post('/operations/{document}/transition', [OperationalDocumentController::class, 'transition'])
        ->name('operational-documents.transition');
    Route::post('/operations/{document}/reject-delivery', [OperationalDocumentController::class, 'rejectDelivery'])
        ->name('operational-documents.deliveries.reject');
    Route::post('/delivery-corrective-actions/{action}/complete', [OperationalDocumentController::class, 'completeCorrectiveAction'])
        ->name('delivery-corrective-actions.complete');
    Route::post('/operations/{document}/next', [OperationalDocumentController::class, 'createNext'])
        ->name('operational-documents.next');
    Route::get('/operations/{document}/next-preview', [OperationalDocumentController::class, 'previewNext'])
        ->name('operational-documents.next.preview');
    Route::post('/operations/{document}/sign', [OperationalDocumentController::class, 'signContract'])
        ->name('operational-documents.contracts.sign');
    Route::post('/operations/sales-orders/from-quotation', [OperationalDocumentController::class, 'createFromQuotation'])
        ->name('operational-documents.sales-orders.from-quotation');
    Route::get('/operations-dashboard', [OperationsFoundationController::class, 'dashboard'])->name('operations.dashboard');
    Route::get('/customer-success', [OperationsFoundationController::class, 'customerSuccess'])->name('operations.customer-success');
    Route::post('/operations/accounts/{account}/hierarchy', [OperationsFoundationController::class, 'hierarchy'])->name('operations.accounts.hierarchy');
    Route::post('/operations/accounts/{account}/assign', [OperationsFoundationController::class, 'assignAccount'])->name('operations.accounts.assign');
    Route::post('/operations/accounts/{account}/merge', [OperationsFoundationController::class, 'merge'])->name('operations.accounts.merge');
    Route::post('/operations/customer-health', [OperationsFoundationController::class, 'health'])->name('operations.health.store');
    Route::post('/operations/renewals', [OperationsFoundationController::class, 'renewal'])->name('operations.renewals.store');
    Route::post('/operations/renewals/{renewal}/status', [OperationsFoundationController::class, 'renewalStatus'])->name('operations.renewals.status');
    Route::post('/operations/invoice-schedules', [OperationsFoundationController::class, 'invoiceSchedule'])->name('operations.invoice-schedules.store');
    Route::post('/operations/sla-policies', [OperationsFoundationController::class, 'sla'])->name('operations.sla.store');
    Route::post('/operations/sla-refresh', [OperationsFoundationController::class, 'refreshSla'])->name('operations.sla.refresh');
    Route::post('/operations/assignment-rules', [OperationsFoundationController::class, 'assignmentRule'])->name('operations.assignment-rules.store');
    Route::get('/assignment-rules', [OperationsFoundationController::class, 'assignmentRules'])->name('operations.assignment-rules.index');
    Route::post('/assignment-failures/{failure}/resolve', [OperationsFoundationController::class, 'resolveAssignmentFailure'])->name('operations.assignment-failures.resolve');
    Route::get('/price-books', [PriceBookController::class, 'index'])->name('price-books.index');
    Route::get('/payments', [PaymentController::class, 'index'])->name('payments.index');
    Route::post('/payments', [PaymentController::class, 'store'])->name('payments.store');
    Route::post('/payments/{payment}/verify', [PaymentController::class, 'verify'])->name('payments.verify');
    Route::post('/price-books', [PriceBookController::class, 'store'])->name('price-books.store');
    Route::put('/price-books/{priceBook}', [PriceBookController::class, 'update'])->name('price-books.update');
    Route::delete('/price-books/{priceBook}', [PriceBookController::class, 'destroy'])->name('price-books.destroy');
    Route::post('/price-books/{priceBook}/items', [PriceBookController::class, 'storeItem'])->name('price-books.items.store');
    Route::delete('/price-books/{priceBook}/items/{item}', [PriceBookController::class, 'destroyItem'])->name('price-books.items.destroy');
    Route::get('/recycle-bin', [RecycleBinController::class, 'index'])->name('recycle-bin.index');
    Route::post('/recycle-bin/{module}/{id}/restore', [RecycleBinController::class, 'restore'])->name('recycle-bin.restore');
    Route::post('/trade-documents/{document}/lines', [TradeDocumentController::class, 'addLine'])->name('trade-documents.lines.store');
    Route::delete('/trade-documents/{document}/lines/{line}', [TradeDocumentController::class, 'removeLine'])->name('trade-documents.lines.destroy');
    Route::post('/trade-documents/{document}/transition', [TradeDocumentController::class, 'transition'])->name('trade-documents.transition');
    Route::post('/trade-documents/{document}/convert', [TradeDocumentController::class, 'convert'])->name('trade-documents.convert');
    Route::post('/trade-documents/{document}/share', [TradeDocumentController::class, 'share'])->name('trade-documents.share');
    Route::get('/trade-documents/{document}/pdf', [TradeDocumentController::class, 'pdf'])->name('trade-documents.pdf');
    Route::get('/trade-documents/{document}/word', [TradeDocumentController::class, 'word'])->name('trade-documents.word');
    Route::get('/trade-documents/{document}/csv', [TradeDocumentController::class, 'csv'])->name('trade-documents.csv');
    Route::post('/trade-documents/{document}/email', [TradeDocumentController::class, 'email'])->name('trade-documents.email');
    Route::post('/trade-documents/{document}/payments', [TradeDocumentController::class, 'payment'])->name('trade-documents.payments.store');
    Route::get('/trade-documents/{document}/payments', [TradeDocumentController::class, 'paymentHistory'])->name('trade-documents.payments.index');
    Route::get('/trade-documents/{document}/payments/{payment}/receipt', [TradeDocumentController::class, 'receipt'])->name('trade-documents.payments.receipt');
    Route::put('/trade-documents/{document}/payments/{payment}', [TradeDocumentController::class, 'correctPayment'])->name('trade-documents.payments.correct');
    Route::post('/trade-documents/{document}/payments/{payment}/void', [TradeDocumentController::class, 'voidPayment'])->name('trade-documents.payments.void');
    Route::post('/trade-documents/{document}/payments/{payment}/refund', [TradeDocumentController::class, 'refundPayment'])->name('trade-documents.payments.refund');
    Route::post('/trade-documents/{document}/payments/{payment}/reverse', [TradeDocumentController::class, 'reversePayment'])->name('trade-documents.payments.reverse');
    Route::post('/trade-documents/{document}/payments/{payment}/reconcile', [TradeDocumentController::class, 'reconcilePayment'])->name('trade-documents.payments.reconcile');
    Route::post('/trade-documents/configure-numbering', [TradeDocumentController::class, 'configure'])->name('trade-documents.configure');
    Route::post('/trade-documents/templates', [TradeDocumentController::class, 'template'])->name('trade-documents.templates.store');
    Route::post('/trade-documents/sync-legacy', [TradeDocumentController::class, 'syncLegacy'])->name('trade-documents.sync-legacy');
    Route::get('/reports/crm', [ReportingController::class, 'index'])->name('reports.crm');
    Route::get('/reports/crm/export', [ReportingController::class, 'export'])->name('reports.crm.export');
    Route::post('/reports/crm/schedules', [ReportingController::class, 'schedule'])->name('reports.crm.schedules.store');
    Route::post('/crm-files/{file}/replace', [CrmFileController::class, 'replace'])->name('crm-files.replace');
    Route::get('/crm-files/{file}/compare/{other}', [CrmFileController::class, 'compare'])->name('crm-files.compare');
    Route::post('/crm-files/{file}/permissions', [CrmFileController::class, 'grant'])->name('crm-files.permissions');
    Route::get('/crm-files/{file}/view', [CrmFileController::class, 'view'])->name('crm-files.view');
    Route::get('/crm-files/{file}/download', [CrmFileController::class, 'download'])->name('crm-files.download');
    Route::delete('/crm-files/{file}', [CrmFileController::class, 'destroy'])->name('crm-files.destroy');
    Route::post('/crm-files/{id}/restore', [CrmFileController::class, 'restore'])->name('crm-files.restore');
    Route::delete('/crm-files/{id}/permanent', [CrmFileController::class, 'forceDestroy'])->name('crm-files.force-destroy');
    Route::put('/crm-files/settings', [CrmFileController::class, 'updateSettings'])->name('crm-files.settings');
    Route::get('/crm-files/{module}/{id}', [CrmFileController::class, 'index'])->name('crm-files.index');
    Route::post('/crm-files/{module}/{id}', [CrmFileController::class, 'store'])->name('crm-files.store');
    Route::get('/settings/notifications', [NotificationPreferenceController::class, 'edit'])->name('notification-preferences.edit');
    Route::put('/settings/notifications', [NotificationPreferenceController::class, 'update'])->name('notification-preferences.update');
    Route::get('/settings/reminder-escalations', [ReminderEscalationSettingController::class, 'edit'])->name('reminder-escalations.edit');
    Route::put('/settings/reminder-escalations', [ReminderEscalationSettingController::class, 'update'])->name('reminder-escalations.update');
    Route::get('/notifications', [NotificationCenterController::class, 'index'])->name('notifications.index');
    Route::get('/notifications/{id}/open', [NotificationCenterController::class, 'open'])->name('notifications.open');
    Route::post('/notifications/read-all', [NotificationCenterController::class, 'markAllRead'])->name('notifications.read-all');
    Route::post('/notification-attempts/{attempt}/retry', [NotificationCenterController::class, 'retry'])->name('notification-attempts.retry');
    Route::get('/meeting-invitations/{activity}', [ActivityDeliveryController::class, 'meeting'])->name('meeting-invitations.show');
    Route::post('/meeting-invitations/{activity}', [ActivityDeliveryController::class, 'respond'])->name('meeting-invitations.respond');
    Route::get('/related-activity/{module}/{id}', [RelatedActivityController::class, 'index'])->name('related-activity.index');
});

Route::get('/shared/trade-document/{token}', [TradeDocumentController::class, 'publicView'])->name('trade-documents.public');
Route::post('/shared/trade-document/{token}/decision', [TradeDocumentController::class, 'customerDecision'])->name('trade-documents.customer-decision');
Route::post('/webhooks/{provider}', [IntegrationWebhookController::class, 'receive'])
    ->middleware('throttle:120,1')->name('integrations.webhook');
Route::get('/email-track/open/{token}.gif', [ActivityDeliveryController::class, 'open'])->name('email-tracking.open');
Route::get('/survey/respond/{token}', [SurveyController::class,'responseForm'])->name('surveys.respond');
Route::post('/survey/respond/{token}', [SurveyController::class,'submit'])->name('surveys.submit');
Route::get('/survey/thanks/{token}', [SurveyController::class,'thanks'])->name('surveys.thanks');
Route::post('/email-track/reply/{token}', [ActivityDeliveryController::class, 'reply'])
    ->middleware('throttle:60,1')->name('email-tracking.reply');


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

// Route::get('/general-setting', function () {
//     return view('settings.settings-general');
// })->name('general-setting');

Route::get('/email-setting', function () {
    return view('settings.settings-email');
})->name('email-setting');

// reports routes
Route::middleware(['auth','verified','business.complete'])->group(function(){
    Route::get('/leads-reports', [ReportingController::class,'legacy'])->defaults('section','leads')->name('leads-report');
    Route::get('/project-reports', [ReportingController::class,'legacy'])->defaults('section','projects')->name('project-report');
    Route::get('/sales-reports', [ReportingController::class,'legacy'])->defaults('section','sales')->name('sales-report');
    Route::get('/timesheets-reports', [ReportingController::class,'legacy'])->defaults('section','timesheets')->name('timesheets-report');
});


// applications routes

Route::get('/apps-chat', function () {
    return redirect()->route('workflow.index', ['module' => 'messages']);
})->name('apps-chat');

// Route::get('/apps-email', function () {
//     return view('applications.apps-email');
// })->name('apps-email');

Route::get('/apps-mail', function () {
    return redirect()->route('schedule-activity', ['activity_type' => 'email']);
})->name('apps-mail');

Route::get('/apps-notes', function () {
    return redirect()->route('workflow.index', ['module' => 'notes']);
})->name('apps-notes');

Route::get('/apps-storage', function () {
    return view('applications.apps-storage');
})->name('apps-storage');

Route::get('/apps-tasks', function () {
    return redirect()->route('workflow.index', ['module' => 'todos']);
})->name('apps-tasks');


//customers
Route::get('/customers-create', function () {
    return redirect()->route('contacts-create', ['type' => 'Customer']);
})->name('customers-create');

Route::get('/customers-view', function () {
    $id=request('id'); return $id ? redirect()->route('contacts.view',['id'=>$id]) : redirect()->route('contacts',['type'=>'Customer']);
})->name('customers-view');

Route::get('/customers', function () {
    return redirect()->route('contacts', ['type' => 'Customer']);
})->name('customers');



//projects
Route::middleware(['auth','verified','business.complete'])->group(function () {
    Route::get('/projects', [ProjectController::class,'index'])->name('projects');
    Route::get('/projects-create', [ProjectController::class,'create'])->name('projects-create');
    Route::post('/projects', [ProjectController::class,'store'])->name('projects.store');
    Route::get('/projects-view', [ProjectController::class,'show'])->name('projects-view');
    Route::put('/projects/{project}', [ProjectController::class,'update'])->name('projects.update');
    Route::delete('/projects/{project}', [ProjectController::class,'destroy'])->name('projects.destroy');
    Route::post('/deals/{deal}/convert-project', [ProjectController::class,'convertDeal'])->name('projects.convert-deal');
    Route::post('/projects/{project}/milestones', [ProjectController::class,'milestoneStore'])->name('projects.milestones.store');
    Route::post('/projects/{project}/risks-issues', [ProjectController::class,'riskIssueStore'])->name('projects.risks-issues.store');
    Route::put('/projects/{project}/risks-issues/{item}', [ProjectController::class,'riskIssueUpdate'])->name('projects.risks-issues.update');
    Route::put('/projects/{project}/milestones/{milestone}', [ProjectController::class,'milestoneUpdate'])->name('projects.milestones.update');
    Route::delete('/projects/{project}/milestones/{milestone}', [ProjectController::class,'milestoneDestroy'])->name('projects.milestones.destroy');
    Route::post('/projects/{project}/tasks', [ProjectController::class,'taskStore'])->name('projects.tasks.store');
    Route::put('/projects/{project}/tasks/{task}', [ProjectController::class,'taskUpdate'])->name('projects.tasks.update');
    Route::delete('/projects/{project}/tasks/{task}', [ProjectController::class,'taskDestroy'])->name('projects.tasks.destroy');
    Route::post('/projects/{project}/products', [ProjectController::class,'productStore'])->name('projects.products.store');
    Route::delete('/projects/{project}/products/{product}', [ProjectController::class,'productDestroy'])->name('projects.products.destroy');
    Route::post('/projects/{project}/assign', [ProjectController::class,'assign'])->name('projects.assign');
    Route::post('/projects/{project}/meetings', [ProjectController::class,'meetingStore'])->name('projects.meetings.store');
    Route::get('/cases', [CaseController::class,'index'])->name('cases.index');
    Route::post('/cases', [CaseController::class,'store'])->name('cases.store');
    Route::get('/cases/{case}', [CaseController::class,'show'])->name('cases.show');
    Route::put('/cases/{case}', [CaseController::class,'update'])->name('cases.update');
    Route::delete('/cases/{case}', [CaseController::class,'destroy'])->name('cases.destroy');
    Route::post('/cases/{case}/comments', [CaseController::class,'comment'])->name('cases.comments.store');
    Route::get('/surveys', [SurveyController::class,'index'])->name('surveys.index');
    Route::post('/surveys', [SurveyController::class,'store'])->name('surveys.store');
    Route::get('/surveys/{survey}', [SurveyController::class,'show'])->name('surveys.show');
    Route::put('/surveys/{survey}', [SurveyController::class,'update'])->name('surveys.update');
    Route::post('/surveys/{survey}/questions', [SurveyController::class,'questionStore'])->name('surveys.questions.store');
    Route::put('/surveys/{survey}/questions/{question}', [SurveyController::class,'questionUpdate'])->name('surveys.questions.update');
    Route::delete('/surveys/{survey}/questions/{question}', [SurveyController::class,'questionDestroy'])->name('surveys.questions.destroy');
    Route::post('/surveys/{survey}/distribute', [SurveyController::class,'distribute'])->name('surveys.distribute');
    Route::post('/surveys/{survey}/recipients/{recipient}/resend', [SurveyController::class,'resend'])->name('surveys.recipients.resend');
    Route::post('/surveys/{survey}/recipients/{recipient}/remind', [SurveyController::class,'remind'])->name('surveys.recipients.remind');
});

Auth::routes();

    // Route::get('/login', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
// });
