<?php

use App\Http\Controllers\ErrorLogController;
use App\Http\Controllers\UserLogController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\InvitationController;
use App\Http\Controllers\NotificationController;
use App\Http\Controllers\PlanController;
use App\Http\Controllers\PaymentLedgerController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\RolePermissionController;
use App\Http\Controllers\AdminProfileChangeController;
use App\Http\Controllers\EnquiryController;
use App\Http\Controllers\MaintenanceController;
use App\Http\Controllers\TnCController;
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::get('/__maintenance/{action}/{key}/{command?}', [MaintenanceController::class, 'toggle']);

// Redirect root URL
Route::get('/', function () {
    return redirect()->route(Auth::check() ? 'dashboard' : 'login');
});

Auth::routes(['register' => false, 'reset' => false]);// disable register and reset password

Route::middleware('guest')->group(function() {

    // ----------------------
    // User Invitation
    // RMK:  not use
    // ----------------------
    Route::get('/invite/accept', [InvitationController::class, 'showAcceptForm'])->name('invitation.accept.form');
    Route::post('/invite/accept', [InvitationController::class, 'accept'])->name('invitation.accept');

});

Route::middleware('auth')->group(function() {
    Route::get('/dashboard/{type?}', [DashboardController::class, 'index'])->name('dashboard');

    Route::get('/profile', [ProfileController::class, 'index'])->name('profile');
    Route::put('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::put('/profile/updatecustomerdata', [ProfileController::class, 'updatecustomerdata'])->name('profile.updatecustomerdata');

    // ----------------------
    // User Management
    // ----------------------
    Route::post('/user/{id}/activate', [UserController::class, 'activateAccount'])->name('user.activate');
    Route::post('/user/tnc-agree', [UserController::class, 'agreeTnc'])->name('user.tnc.agree');
    Route::resource('user', UserController::class)->except(['show'])
        ->name('index', 'user.index')
        ->name('create', 'user.create')
        ->name('store', 'user.store')
        ->name('edit', 'user.edit')
        ->name('update', 'user.update')
        ->name('destroy', 'user.destroy');
    Route::get('/user/view/{id}', [UserController::class, 'view'])->name('user.view');

    Route::get('/users/ajax-search', [UserController::class, 'ajaxSearch'])
        ->name('users.ajax.search');

    // ----------------------
    // Role & Permission Management
    // ----------------------
    Route::resource('role', RolePermissionController::class)->except(['show'])
        ->name('index', 'role.index')
        ->name('create', 'role.create')
        ->name('store', 'role.store')
        ->name('edit', 'role.edit')
        ->name('update', 'role.update')
        ->name('destroy', 'role.destroy');

    // ----------------------
    // Plans
    // ----------------------
    Route::resource('plans', PlanController::class)
            ->name('index', 'plan.index')
            ->name('create', 'plan.create')
            ->name('store', 'plan.store')
            ->name('show', 'plan.show')
            ->name('edit', 'plan.edit')
            ->name('update', 'plan.update')
            ->name('destroy', 'plan.destroy');

    Route::get('/plan/participant/{id}', [PlanController::class, 'editParticipant'])->name('participant.edit');
    Route::put('/plan/participant/{id}', [PlanController::class, 'updateParticipant'])->name('participant.update');
    Route::get('/plan/member/{id}/{userId?}', [PlanController::class, 'showPlanForUser'])->name('plan.member.show');
    Route::get('/plan/user/details',[PlanController::class, 'dataTableForUserDetails'])->name('plan.user.details');

    Route::get('/plan/member/{id}/{userId?}/payments',[PaymentLedgerController::class, 'index'])->name('plan.member.payments');
    Route::get('/plan/member/{id}/{userId?}/update-payment',[PaymentLedgerController::class, 'create'])->name('plan.member.updatePayment');
    Route::post('/plan/member/{id}/{userId?}/update-payment',[PaymentLedgerController::class, 'store'])->name('plan.member.updatePayment.store');

    Route::get('/plan/{id}/download-excel', [PlanController::class, 'downloadExcel'])->name('plan.downloadExcel');
    Route::get('/plan/download-pdf', [PlanController::class, 'downloadMasterListPDF'])->name('plan.downloadMasterListPDF');

    // ----------------------
    // Logs
    // ----------------------
    Route::get('/user-logs', [UserLogController::class, 'index'])->name('user-logs.index');
    Route::get('/error-logs', [ErrorLogController::class, 'index'])->name('error-logs.index');


    // ----------------------
    // Notifications
    // ----------------------
    Route::get('/notifications/open/{id}', [NotificationController::class, 'openNotification'])->name('notifications.open');

    // ----------------------
    // Update Customer Data
    // ----------------------
    Route::get('/profile-changes', [AdminProfileChangeController::class, 'index'])->name('profile-changes.index');
    Route::get('/profile-changes/{id}', [AdminProfileChangeController::class, 'show'])->name('profile-changes.show');
    Route::post('/profile-changes/{id}/approve', [AdminProfileChangeController::class, 'approve'])->name('profile-changes.approve');
    Route::post('/profile-changes/{id}/reject', [AdminProfileChangeController::class, 'reject'])->name('profile-changes.reject');

    Route::get('tnc/download', [TnCController::class, 'download'])->name('tnc.download');
    Route::resource('tnc', TnCController::class)->except(['destroy', 'edit', 'store', 'create']);

    // ----------------------
    // Enquiries
    // ----------------------
    Route::get('/enquiries', [EnquiryController::class, 'index'])->name('enquiries.index');
    Route::get('/enquiries/create', [EnquiryController::class, 'create'])->name('enquiries.create');
    Route::post('/enquiries', [EnquiryController::class, 'store'])->name('enquiries.store');
    Route::get('/enquiries/{id}', [EnquiryController::class, 'show'])->name('enquiries.show');
    Route::post('/enquiries/{id}/reply', [EnquiryController::class, 'reply'])->name('enquiries.reply');
    Route::post('/enquiries/{id}/close', [EnquiryController::class, 'close'])->name('enquiries.close');
});

