<?php

use App\Http\Controllers\CompanyController;
use App\Http\Controllers\DocumentController;
use App\Http\Controllers\FileManagerController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\Auth\LoginController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
*/

// ১. গেস্ট রাউট (লগইন করা না থাকলে শুধু লগইন পেজ দেখা যাবে)
Route::middleware('guest')->group(function () {
    Route::get('login', [LoginController::class, 'showLoginForm'])->name('login');
    Route::post('login', [LoginController::class, 'login']);
});

// ২. প্রোটেক্টেড রাউটস (লগইন করা ইউজারদের জন্য)
Route::middleware('auth')->group(function () {

    // হোম রিডাইরেক্ট (লগইন করার পর সরাসরি ড্যাশবোর্ডে যাবে)
    Route::get('/', function () {
        return redirect()->route('dashboard');
    });

    // ড্যাশবোর্ড
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');

    // লগআউট রাউট
    Route::post('logout', [LoginController::class, 'logout'])->name('logout');

    // ডকুমেন্ট ম্যানেজমেন্ট গ্রুপ
    Route::prefix('documents')->name('documents.')->group(function () {
        
        // মেইন লিস্ট ও ফিল্টার
        Route::get('/', [DocumentController::class, 'index'])->name('index');
        Route::get('/company/{company}', [DocumentController::class, 'companyFolders'])->name('company-folders');
        Route::get('/company/{company}/type/{documentType}', [DocumentController::class, 'financialYears'])->name('financial-years');
        Route::get('/company/{company}/type/{documentType}/year/{financialYear}', [DocumentController::class, 'documentsList'])->name('list');

        // অপারেশনস (CRUD)
        Route::post('/store', [DocumentController::class, 'store'])->name('store');
        Route::put('/{id}/update', [DocumentController::class, 'update'])->name('update');
        Route::delete('/{id}/delete', [DocumentController::class, 'destroy'])->name('destroy');
        Route::get('/{document}/download', [DocumentController::class, 'download'])->name('download');
        Route::post('/{document}/copy', [DocumentController::class, 'copy'])->name('copy');

        // ফাইল ম্যানেজার
        Route::get('/manager/{id}', [FileManagerController::class, 'show'])->name('manager.show');
        Route::delete('/manager/doc/delete/{id}', [FileManagerController::class, 'deleteDocument'])->name('doc.delete');
        
        // টাইপ ম্যানেজমেন্ট
        Route::get('/add-type', [DocumentController::class, 'showAddTypeForm'])->name('type.create');
        Route::post('/add-type', [DocumentController::class, 'storeType'])->name('type.store');

        // API Routes (Ajax call এর জন্য)
        Route::get('/api/financial-years', [DocumentController::class, 'getFinancialYears'])->name('api.financial-years');
        Route::get('/api/document-types', [DocumentController::class, 'getDocumentTypes'])->name('api.document-types');
    });

    // কোম্পানি ম্যানেজমেন্ট
    Route::prefix('companies')->name('companies.')->group(function () {
        Route::post('/store', [CompanyController::class, 'store'])->name('store');
    });

});

// ৩. এক্সট্রা অথেনটিকেশন রাউট (যদি Breeze ব্যবহার করেন)
if (file_exists(__DIR__.'/auth.php')) {
    require __DIR__.'/auth.php';
}