<?php

use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\MenuController;
use App\Http\Controllers\Admin\ModuleController;
use App\Http\Controllers\Auth\LoginController;
use Illuminate\Support\Facades\Route;

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

// Authentication Routes
Route::get('/login', [LoginController::class, 'showLoginForm'])->name('login');
Route::post('/login', [LoginController::class, 'login']);
Route::post('/logout', [LoginController::class, 'logout'])->name('logout');
Route::get('/otp', [LoginController::class, 'showOtpForm'])->name('otp.show');
Route::post('/otp', [LoginController::class, 'verifyOtp'])->name('otp.verify');
Route::post('/otp/resend', [LoginController::class, 'resendOtp'])->name('otp.resend');
Route::get('/change-password',  [LoginController::class, 'showChangePasswordForm'])->name('password.change')->middleware('auth');
Route::post('/change-password', [LoginController::class, 'updatePassword'])->name('password.update')->middleware('auth');

// Admin Routes (Protected)
Route::prefix('admin')->name('admin.')->middleware(['auth', 'admin', 'password.changed'])->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
    
    Route::resource('menus', MenuController::class);
    Route::resource('modules', ModuleController::class);
    Route::resource('users', App\Http\Controllers\Admin\UserController::class);
    Route::get('users/{user}/permissions',  [App\Http\Controllers\Admin\UserController::class, 'getPermissions'])->name('users.permissions.get');
    Route::post('users/{user}/permissions', [App\Http\Controllers\Admin\UserController::class, 'savePermissions'])->name('users.permissions.save');
    Route::post('modules/{module}/toggle-status', [ModuleController::class, 'toggleStatus'])->name('modules.toggle-status');

    // Account Lawyer Mapping Routes
    Route::prefix('account-lawyer')->name('account-lawyer.')->group(function () {
        Route::get('/', [App\Http\Controllers\Admin\AccountLawyerMappingController::class, 'index'])->name('index');
        Route::get('/search-account', [App\Http\Controllers\Admin\AccountLawyerMappingController::class, 'searchAccount'])->name('search-account');
        Route::get('/search-lawyer', [App\Http\Controllers\Admin\AccountLawyerMappingController::class, 'searchLawyer'])->name('search-lawyer');
        Route::get('/create', [App\Http\Controllers\Admin\AccountLawyerMappingController::class, 'create'])->name('create');
        Route::post('/store', [App\Http\Controllers\Admin\AccountLawyerMappingController::class, 'store'])->name('store');
        Route::get('/{mapping}/edit', [App\Http\Controllers\Admin\AccountLawyerMappingController::class, 'edit'])->name('edit');
        Route::put('/{mapping}', [App\Http\Controllers\Admin\AccountLawyerMappingController::class, 'update'])->name('update');
        Route::delete('/{mapping}', [App\Http\Controllers\Admin\AccountLawyerMappingController::class, 'destroy'])->name('destroy');
        Route::get('/success', [App\Http\Controllers\Admin\AccountLawyerMappingController::class, 'success'])->name('success');
    });
    // General Documents
    Route::get('/general-documents', [App\Http\Controllers\Admin\GeneralDocumentController::class, 'index'])->name('general-documents.index');
    Route::post('/general-documents', [App\Http\Controllers\Admin\GeneralDocumentController::class, 'store'])->name('general-documents.store');
    Route::get('/general-documents/search-users', [App\Http\Controllers\Admin\GeneralDocumentController::class, 'searchUsers'])->name('general-documents.search-users');
    Route::get('/general-documents/{document}/files', [App\Http\Controllers\Admin\GeneralDocumentController::class, 'files'])->name('general-documents.files');
    Route::post('/general-documents/{document}/share', [App\Http\Controllers\Admin\GeneralDocumentController::class, 'share'])->name('general-documents.share');
    Route::get('/general-documents/{document}/download/{attachment}', [App\Http\Controllers\Admin\GeneralDocumentController::class, 'download'])->name('general-documents.download');
    Route::delete('/general-documents/{document}', [App\Http\Controllers\Admin\GeneralDocumentController::class, 'destroy'])->name('general-documents.destroy');

    // Profile
    Route::get('/profile', [App\Http\Controllers\Admin\ProfileController::class, 'show'])->name('profile.show');
    Route::put('/profile', [App\Http\Controllers\Admin\ProfileController::class, 'update'])->name('profile.update');
    Route::put('/profile/change-password', [App\Http\Controllers\Admin\ProfileController::class, 'changePassword'])->name('profile.change-password');

    // Reports
    Route::get('/reports', [App\Http\Controllers\Admin\ReportController::class, 'index'])->name('reports.index');

    // Case File Route
    Route::get('/case-file', [App\Http\Controllers\Admin\CaseFileController::class, 'index'])->name('case-file.index');

    // Oracle API
    Route::get('/oracle/law-firms', [App\Http\Controllers\Admin\OracleApiController::class, 'searchLawFirms'])->name('oracle.law-firms');
    Route::get('/oracle/loan-accounts', [App\Http\Controllers\Admin\OracleApiController::class, 'searchLoanAccount'])->name('oracle.loan-accounts');
    Route::get('/oracle/check-lawyer-user', [App\Http\Controllers\Admin\OracleApiController::class, 'checkLawyerUser'])->name('oracle.check-lawyer-user');
});
