<?php

use Illuminate\Support\Facades\Route;

Route::namespace('Auth')->group(function () {
    Route::middleware('admin.guest')->group(function () {
        Route::controller('LoginController')->group(function () {
            Route::get('/', 'showLoginForm')->name('login');
            Route::post('/', 'login')->name('login');
            Route::get('logout', 'logout')->middleware('admin')->withoutMiddleware('admin.guest')->name('logout');
        });

        // Admin Password Reset
        Route::controller('ForgotPasswordController')->prefix('password')->name('password.')->group(function () {
            Route::get('reset', 'showLinkRequestForm')->name('reset');
            Route::post('reset', 'sendResetCodeEmail');
            Route::get('code-verify', 'codeVerify')->name('code.verify');
            Route::post('verify-code', 'verifyCode')->name('verify.code');
        });

        Route::controller('ResetPasswordController')->group(function () {
            Route::get('password/reset/{token}', 'showResetForm')->name('password.reset.form');
            Route::post('password/reset/change', 'reset')->name('password.change');
        });
    });
});

Route::middleware('admin')->group(function () {

    Route::controller('AdminController')->group(function () {
        Route::get('dashboard', 'dashboard')->name('dashboard');

        Route::get('ticket-insight', 'ticketInsight')->name('dashboard.ticket.insight');
        Route::get('customer-happiness-rating', 'customerHappinessRating')->name('dashboard.customer.happiness.rating');
        Route::get('department-insight', 'departmentInsight')->name('dashboard.department.insight');

        Route::get('profile', 'profile')->name('profile');
        Route::post('profile', 'profileUpdate')->name('profile.update');
        Route::get('password', 'password')->name('password');
        Route::post('password', 'passwordUpdate')->name('password.update');

        //Notification
        Route::get('notifications', 'notifications')->name('notifications');
        Route::get('notification/read/{id}', 'notificationRead')->name('notification.read');
        Route::get('notifications/read-all', 'readAllNotification')->name('notifications.read.all');
        Route::post('notifications/delete-all', 'deleteAllNotification')->name('notifications.delete.all');
        Route::post('notifications/delete-single/{id}', 'deleteSingleNotification')->name('notifications.delete.single');

        //Report Bugs
        Route::get('request-report', 'requestReport')->name('request.report');
        Route::post('request-report', 'reportSubmit');

        Route::get('download-attachments/{file_hash}', 'downloadAttachment')->name('download.attachment');
    });

    Route::controller('SupportTicketController')->prefix('support-ticket')->name('ticket.')->group(function () {
        Route::get('add', 'add')->name('add');
        Route::post('store', 'store')->name('store');
        Route::post('reply/{ticket_number}', 'reply')->name('reply');
        Route::post('ai-generate', 'aiGenerate')->name('ai.generate');
        Route::post('reply/delete/{reply_id}', 'deleteReply')->name('reply.delete');

        Route::get('list', 'list')->name('list');
        Route::get('details/{ticket_number}', 'details')->name('details');

        Route::get('attachment/{ticket_number}', 'attachment')->name('attachment');
        Route::post('attachment/delete/{file_hash}', 'deleteAttachment')->name('attachment.delete');

        Route::get('other/{ticket_number}', 'otherTicket')->name('other');
        Route::get('history/{ticket_number}', 'history')->name('history');

        Route::post('feedback/{ticket_number}', 'saveFeedback')->name('feedback');

        Route::get('attachment/download/{file_hash}', 'downloadAttachment')->name('attachment.download');
        Route::post('update/{ticket_number}', 'update')->name('update');
    });

    Route::controller('SupportTicketController')->prefix('support-ticket')->group(function () {
        Route::get('pin/list', 'pinList')->name('pin.ticket.list');
        Route::get('pin/{ticket_number}', 'pin')->name('pin.ticket');
    });

    Route::controller('PredefinedReplyController')->prefix('predefined/reply')->name('predefined.reply.')->group(function () {
        Route::get('list', 'list')->name('list');
        Route::post('save/{id?}', 'save')->name('save');
        Route::post('status/{id}', 'status')->name('status');
        Route::post('delete/{id}', 'delete')->name('delete');
    });

    Route::controller('ManageAgentController')->middleware('super.admin')->prefix('agent')->name('agent.')->group(function () {
        Route::get('list', 'list')->name('list');
        Route::get('overview/{id}', 'overview')->name('overview');
        Route::get('analytics/{id}', 'analytics')->name('analytics');

        Route::get('ticket/{id}', 'ticket')->name('ticket');
        Route::get('happiness-rating/{id}', 'happinessRating')->name('happiness.rating');

        Route::get('add', 'addAgent')->name('add');
        Route::get('edit/{id}', 'editAgent')->name('edit');
        Route::post('save/{id?}', 'save')->name('save');
    });

    Route::controller('ManageAgentController')->middleware('super.admin')->prefix('department/agent')->name('department.agent.')->group(function () {
        Route::get('list/{id}', 'listByDepartment')->name('list');
        Route::post('add', 'add')->name('add');
        Route::post('remove/{agent_id}/{department_id}', 'remove')->name('remove');
    });

    Route::controller('CustomerController')->prefix('customer')->name('customer.')->group(function () {
        Route::get('list', 'list')->name('list');
        Route::get('add', 'add')->name('add');
        Route::get('edit/{id}', 'edit')->name('edit');
        Route::post('save/{id?}', 'save')->name('save');

        Route::get('overview/{id}', 'overview')->name('overview');
        Route::get('analytics/{id}', 'analytics')->name('analytics');

        Route::get('ticket/{id}', 'ticket')->name('ticket');
        Route::get('happiness-rating/{id}', 'happinessRating')->name('happiness.rating');
        Route::get('attachment/{id}', 'attachment')->name('attachment');

        Route::post('info', 'info')->name('info');
    });

    Route::controller('KnowledgeController')->prefix('knowledge')->name('knowledge.')->group(function () {
        Route::get('list', 'list')->name('list');
        Route::get('details/{id}', 'details')->name('details');
        Route::post('comment/store/{id}', 'storeComment')->name('comment.store');
        Route::get('comment/{id}', 'comment')->name('comment');
        Route::get('comment/load/{id}', 'loadComment')->name('comment.load');
        Route::get('add', 'add')->name('add');
        Route::get('edit/{id}', 'edit')->name('edit');
        Route::post('save/{id?}', 'save')->name('save');
        Route::post('status/{id}', 'status')->name('status');
        Route::post('delete/{id}', 'delete')->name('delete');

        Route::post('ai-generate', 'aiGenerate')->name('ai.generate');

        Route::get('analytics/load', 'loadAnalytics')->name('analytics.load');
        Route::get('analytics/{id?}', 'analytics')->name('analytics');
    });

    Route::controller('KnowledgeCategoryController')->prefix('knowledge-category')->name('knowledge.category.')->group(function () {
        Route::get('list', 'list')->name('list');
        Route::get('add', 'add')->name('add');
        Route::get('edit/{id}', 'edit')->name('edit');
        Route::post('save/{id?}', 'save')->name('save');
        Route::post('status/{id}', 'status')->name('status');
        Route::post('delete/{id}', 'delete')->name('delete');
    });

    Route::controller('AnalyticsController')->middleware('super.admin')->prefix('analytics')->name('analytics.')->group(function () {
        Route::get('ticket', 'ticket')->name('ticket');
        Route::get('ticket/data', 'ticketData')->name('ticket.data');
        Route::get('ticket/source/data', 'ticketSourceData')->name('ticket.source.data');

        Route::get('ticket/days/recent', 'ticketSevenDays')->name('ticket.days.seven');
        Route::get('ticket/data/department/days/recent', 'ticketDataByDepartment')->name('ticket.data.department.days.seven');
        Route::get('ticket/data/channel/days/recent', 'ticketDataByChannel')->name('ticket.data.channel.days.seven');
        Route::get('ticket/data/rating/days/recent', 'ticketDataByRating')->name('ticket.data.rating.days.seven');
        Route::get('agent/first/response/days/recent', 'agentFirstResponseTime')->name('agent.first.response.seven.days');
        Route::get('channel/wise/response/days/recent', 'channelWiseResponseTime')->name('ticket.data.response.time.seven.days');

        Route::get('satisfaction', 'satisfaction')->name('satisfaction');
        Route::get('satisfaction/data/{type}', 'satisfactionData')->name('satisfaction.data');

        Route::get('response/time', 'responseTime')->name('response.time');
        Route::get('agent/response/time', 'agentResponseTime')->name('agent.response.time');
        Route::get('agent/bar/response/time', 'agentBarResponseTime')->name('agent.bar.response.time');
        Route::get('user/response/time', 'userResponseTime')->name('user.response.time');

        Route::get('resolution/time', 'resolutionTime')->name('resolution.time');
        Route::get('resolution/time/data', 'resolutionTimeData')->name('resolution.time.data');

        Route::get('opened-and-closed', 'openedAndClosed')->name('opened.and.closed');
        Route::get('opened-and-closed/data', 'openedAndClosedData')->name('opened.and.closed.data');
        Route::get('status/data', 'statusData')->name('status.data');
    });

    Route::controller('DepartmentController')->middleware('super.admin')->prefix('department')->name('department.')->group(function () {
        Route::get('list', 'list')->name('list');
        Route::get('add', 'add')->name('add');
        Route::get('edit/{id}', 'edit')->name('edit');
        Route::post('save/{id?}', 'save')->name('save');
        Route::post('status/{id}', 'status')->name('status');

        Route::get('form/builder/{id}', 'formBuilder')->name('form.builder');
        Route::post('form/builder/{id}', 'saveFormBuilder');
    });

    Route::controller('SpamFilterController')->prefix('spam-filter')->name('spam.filter.')->group(function () {
        Route::get('list', 'list')->name('list');
        Route::post('save/{id?}', 'save')->name('save');
        Route::post('status/{id}', 'status')->name('status');
        Route::post('delete/{id}', 'delete')->name('delete');
    });

    Route::controller('NotificationController')->middleware('super.admin')->prefix('notification/setting')->name('notification.setting.')->group(function () {
        Route::get('config', 'config')->name('config');
        Route::post('config/save', 'saveConfig')->name('config.save');
        Route::post('email/test', 'emailTest')->name('email.test');

        Route::get('sms/config', 'smsConfig')->name('sms.config');
        Route::post('sms/config/save', 'saveSmsConfig')->name('sms.config.save');
        Route::post('sms/test', 'smsTest')->name('sms.test');

        Route::get('global/template', 'globalTemplate')->name('global.template');
        Route::post('global/template/save', 'saveGlobalTemplate')->name('global.template.save');

        Route::get('templates', 'templates')->name('templates');
        Route::get('template/email/edit/{id}', 'editEmailTemplate')->name('template.email.edit');
        Route::get('template/sms/edit/{id}', 'editSmsTemplate')->name('template.sms.edit');
        Route::post('template/save/{id}', 'saveTemplate')->name('template.save');
    });

    Route::prefix('ticket/setting')->middleware('super.admin')->name('setting.ticket.')->group(function () {
        Route::controller('TagController')->prefix('tag')->name('tag.')->group(function () {
            Route::get('list', 'list')->name('list');
            Route::post('save/{id?}', 'save')->name('save');
            Route::post('status/{id}', 'status')->name('status');
        });

        Route::controller('PriorityController')->prefix('priority')->name('priority.')->group(function () {
            Route::get('list', 'list')->name('list');
            Route::post('save/{id?}', 'save')->name('save');
            Route::post('status/{id}', 'status')->name('status');
        });

        Route::controller('TicketStatusController')->prefix('ticket-status')->name('status.')->group(function () {
            Route::get('list', 'list')->name('list');
            Route::post('save/{id?}', 'save')->name('save');
            Route::post('status/{id}', 'status')->name('status');
            Route::post('awaiting/{id}', 'awaiting')->name('awaiting');
        });

    });

    // Language Manager
    Route::controller('LanguageController')->prefix('language')->name('language.')->group(function () {
        Route::get('/', 'langManage')->name('manage');
        Route::post('/', 'langStore')->name('manage.store');
        Route::post('delete/{id}', 'langDelete')->name('manage.delete');
        Route::post('update/{id}', 'langUpdate')->name('manage.update');
        Route::get('edit/{id}', 'langEdit')->name('key');
        Route::post('import', 'langImport')->name('import.lang');
        Route::post('store/key/{id}', 'storeLanguageJson')->name('store.key');
        Route::post('delete/key/{id}', 'deleteLanguageJson')->name('delete.key');
        Route::post('update/key/{id}', 'updateLanguageJson')->name('update.key');
        Route::get('get-keys', 'getKeys')->name('get.key');
    });

    Route::controller('GeneralSettingController')->middleware('super.admin')->name('setting.')->group(function () {
        Route::get('general-setting', 'general')->name('general');
        Route::post('general-setting', 'generalUpdate');

        Route::get('auto-response', 'autoResponse')->name('response.auto');
        Route::post('auto-response', 'autoResponseUpdate');
        Route::get('ai-config', 'AiConfig')->name('ai.config');
        Route::post('ai-config', 'aiConfigUpdate')->name('ai.config.update');

        Route::get('logo', 'logo')->name('logo');
        Route::post('logo', 'logoUpdate')->name('logo.update');

        Route::get('maintenance-mode', 'maintenanceMode')->name('maintenance.mode');
        Route::post('maintenance-mode', 'maintenanceModeSubmit');

        Route::get('info', 'systemInfo')->name('info');
        Route::get('optimize', 'optimize')->name('optimize');
        Route::get('optimize-clear', 'optimizeClear')->name('optimize.clear');
    });

    Route::controller('CronConfigurationController')->middleware('super.admin')->name('cron.')->prefix('cron')->group(function () {
        Route::get('index', 'cronJobs')->name('index');
        Route::post('store', 'cronJobStore')->name('store');
        Route::post('update', 'cronJobUpdate')->name('update');
        Route::post('delete/{id}', 'cronJobDelete')->name('delete');
        Route::get('schedule', 'schedule')->name('schedule');
        Route::post('schedule/store', 'scheduleStore')->name('schedule.store');
        Route::post('schedule/status/{id}', 'scheduleStatus')->name('schedule.status');
        Route::get('schedule/pause/{id}', 'schedulePause')->name('schedule.pause');
        Route::get('schedule/logs/{id}', 'scheduleLogs')->name('schedule.logs');
        Route::post('schedule/log/resolved/{id}', 'scheduleLogResolved')->name('schedule.log.resolved');
        Route::post('schedule/log/flush/{id}', 'logFlush')->name('log.flush');
    });

    Route::controller('FrontendController')->middleware('super.admin')->group(function () {
        Route::prefix('policy/pages')->name('policy.pages.')->group(function () {
            Route::get('/', 'pagesList')->name('list');
            Route::get('add', 'addPage')->name('add');
            Route::get('edit/{id}', 'editPage')->name('edit');
            Route::post('save/{id?}', 'savePage')->name('save');
            Route::post('remove/{id}', 'remove')->name('remove');
            Route::get('frontend-slug-check/{id?}', 'frontendElementSlugCheck')->name('slug.check');
        });

        Route::get('frontend-content', 'frontendContent')->name('frontend.content');
        Route::post('frontend-content/save', 'frontendContentSave')->name('frontend.content.save');

        Route::get('faq', 'faq')->name('frontend.faq');
        Route::post('faq/save/{id?}', 'saveFaq')->name('frontend.faq.save');
        Route::post('faq/delete/{id}', 'deleteFaq')->name('frontend.faq.delete');

        Route::get('seo', 'seo')->name('frontend.seo');
        Route::post('seo/save', 'saveSeo')->name('frontend.seo.save');
    });

    // Plugin
    Route::controller('ExtensionController')->middleware('super.admin')->prefix('extensions')->name('extensions.')->group(function () {
        Route::get('/', 'index')->name('index');
        Route::post('update/{id}', 'update')->name('update');
        Route::post('status/{id}', 'status')->name('status');
    });

    //System Information
    Route::controller('SystemController')->middleware('super.admin')->name('system.')->prefix('system')->group(function () {
        Route::get('system-update', 'systemUpdate')->name('update');
        Route::post('system-update', 'systemUpdateProcess')->name('update.process');
        Route::get('system-update/log', 'systemUpdateLog')->name('update.log');
    });
});
