<?php

use Illuminate\Support\Facades\Route;
use App\Livewire\Auth\Login;
use App\Livewire\Dashboard;

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

Route::get('/dev/migrate-pages', function() {
    \Illuminate\Support\Facades\Schema::dropIfExists('pages');
    \Illuminate\Support\Facades\Artisan::call('migrate');
    return 'تمت تهيئة قاعدة بيانات الصفحات بنجاح!';
});

Route::get('/login', Login::class)->name('login');

Route::get('/dashboard', Dashboard::class)->middleware('auth')->name('dashboard');
Route::get('/settings', \App\Livewire\Settings::class)->middleware(['auth', 'can:manage settings'])->name('settings');
Route::get('/profile', \App\Livewire\Profile::class)->middleware('auth')->name('profile');
Route::get('/users', \App\Livewire\Users::class)->middleware(['auth', 'can:manage users'])->name('users');

// Data screens requiring 'view records' permission
Route::middleware(['auth', 'can:view records'])->group(function () {
    Route::get('/entities', \App\Livewire\Entities::class)->name('entities');
    Route::get('/branches', \App\Livewire\Branches::class)->name('branches');
    Route::get('/centers', \App\Livewire\Centers::class)->name('centers');
    Route::get('/projects', \App\Livewire\Projects::class)->name('projects');
    Route::get('/parties', \App\Livewire\Parties::class)->name('parties');
    Route::get('/accounts', \App\Livewire\Accounts::class)->name('accounts');
    Route::get('/ledgers', \App\Livewire\Ledgers::class)->name('ledgers');
    Route::get('/pages', \App\Livewire\Pages::class)->name('pages');
    Route::get('/pages/{page}/transactions', \App\Livewire\PageTransactions::class)->name('pages.transactions');
    Route::get('/pages/{page}/report', [\App\Http\Controllers\PageReportController::class, 'show'])->name('pages.report');
    
    // Reports
    Route::get('/reports', \App\Livewire\Reports\Index::class)->name('reports.index');
    Route::get('/reports/entities', \App\Livewire\Reports\EntitiesReport::class)->name('reports.entities');
    Route::get('/reports/entities/print', [\App\Http\Controllers\PrintReportController::class, 'entities'])->name('reports.entities.print');
    Route::get('/reports/branches', \App\Livewire\Reports\BranchesReport::class)->name('reports.branches');
    Route::get('/reports/branches/print', [\App\Http\Controllers\PrintReportController::class, 'branches'])->name('reports.branches.print');
    Route::get('/reports/centers', \App\Livewire\Reports\CentersReport::class)->name('reports.centers');
    Route::get('/reports/centers/print', [\App\Http\Controllers\PrintReportController::class, 'centers'])->name('reports.centers.print');
    Route::get('/reports/projects', \App\Livewire\Reports\ProjectsReport::class)->name('reports.projects');
    Route::get('/reports/projects/print', [\App\Http\Controllers\PrintReportController::class, 'projects'])->name('reports.projects.print');
    Route::get('/reports/parties', \App\Livewire\Reports\PartiesReport::class)->name('reports.parties');
    Route::get('/reports/parties/print', [\App\Http\Controllers\PrintReportController::class, 'parties'])->name('reports.parties.print');
    Route::get('/reports/accounts', \App\Livewire\Reports\AccountsReport::class)->name('reports.accounts');
    Route::get('/reports/accounts/print', [\App\Http\Controllers\PrintReportController::class, 'accounts'])->name('reports.accounts.print');
    Route::get('/reports/ledgers', \App\Livewire\Reports\LedgersReport::class)->name('reports.ledgers');
    Route::get('/reports/ledgers/print', [\App\Http\Controllers\PrintReportController::class, 'ledgers'])->name('reports.ledgers.print');
    Route::get('/reports/pages', \App\Livewire\Reports\PagesReport::class)->name('reports.pages');
    Route::get('/reports/pages/print', [\App\Http\Controllers\PrintReportController::class, 'pages'])->name('reports.pages.print');
    Route::get('/reports/balances', \App\Livewire\Reports\BalancesReport::class)->name('reports.balances');
    Route::get('/reports/balances/print', [\App\Http\Controllers\PrintReportController::class, 'balances'])->name('reports.balances.print');
    Route::get('/reports/account-statement', \App\Livewire\Reports\AccountStatementReport::class)->name('reports.account_statement');
    Route::get('/reports/aggregated-statement', \App\Livewire\Reports\AggregatedStatementReport::class)->name('reports.aggregated_statement');
    Route::get('/reports/aggregated-statement/print', [\App\Http\Controllers\AggregatedReportController::class, 'show'])->name('reports.aggregated_statement.print');
});
// System Optimization Route (Useful for cPanel Deployment)
Route::get('/setup/optimize', function () {
    try {
        \Illuminate\Support\Facades\Artisan::call('optimize:clear');
        \Illuminate\Support\Facades\Artisan::call('config:cache');
        \Illuminate\Support\Facades\Artisan::call('route:cache');
        \Illuminate\Support\Facades\Artisan::call('view:cache');
        return 'تم تنظيف وتهيئة النظام بنجاح للعمل بأقصى سرعة في بيئة الإنتاج! 🚀';
    } catch (\Exception $e) {
        return 'حدث خطأ أثناء التهيئة: ' . $e->getMessage();
    }
});

Route::post('/logout', function () {
    auth()->logout();
    request()->session()->invalidate();
    request()->session()->regenerateToken();
    return redirect('/');
})->name('logout');
