<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\BranchAdminController;
use App\Http\Controllers\UserAdminController;
use App\Http\Controllers\Auth\PasswordResetLinkController;
use App\Http\Controllers\Auth\NewPasswordController;
use App\Http\Controllers\DbMaintenanceController;



Route::get('/', function () {
    return auth()->check()
        ? redirect()->route('dashboard')   // إذا عندك route اسمها dashboard
        : redirect()->route('login');      // route الافتراضي للّوجين
});

// =====================
// Login / Logout
// =====================
Route::get('/login', [AuthController::class, 'showLogin'])->name('login');
Route::post('/login', [AuthController::class, 'login']);

Route::post('/logout', [AuthController::class, 'logout'])->middleware('auth')->name('logout');
// ✅ Logout GET احتياطي لتفادي 419 (Page Expired)
Route::get('/logout', [AuthController::class, 'logout'])->middleware('auth');


// =====================
// Forgot / Reset Password (SMTP) - Laravel Default
// =====================
   Route::middleware('guest')->group(function () {
    Route::get('/forgot-password', [PasswordResetLinkController::class, 'create'])->name('password.request');
    Route::post('/forgot-password', [PasswordResetLinkController::class, 'store'])->name('password.email');

    Route::get('/reset-password/{token}', [NewPasswordController::class, 'create'])->name('password.reset');
    Route::post('/reset-password', [NewPasswordController::class, 'store'])->name('password.update');

});





Route::middleware(['auth', 'admin_or_super'])->group(function () {
    // Backups
    Route::get('/dashboard/settings/db-backups', [DbMaintenanceController::class, 'index'])->name('db.backups.index');
    Route::post('/dashboard/settings/db-backups/create', [DbMaintenanceController::class, 'create'])->name('db.backups.create');
    Route::get('/dashboard/settings/db-backups/download/{file}', [DbMaintenanceController::class, 'download'])->name('db.backups.download');
    Route::delete('/dashboard/settings/db-backups/delete/{file}', [DbMaintenanceController::class, 'delete'])->name('db.backups.delete');

    // Restore (مهم)
    Route::post('/dashboard/settings/db-backups/restore/{file}', [DbMaintenanceController::class, 'restore'])->name('db.backups.restore');

    // Purge company data
    Route::post('/dashboard/settings/purge-company-data', [DbMaintenanceController::class, 'purge'])->name('company.data.purge');
});











// =====================
// Dashboard Root Redirect
// =====================
Route::get('/dashboard', function () {
    return redirect()->route('dashboard.sales');
})->middleware(['auth'])->name('dashboard');

// =====================
// Dashboard (AUTH)
// =====================
Route::prefix('dashboard')->middleware(['auth'])->group(function () {

    // =====================
    // Settings (ADMIN ONLY) - Company Settings
    // =====================
    Route::get('/settings/company', [DashboardController::class, 'companySettingsIndex'])
        ->middleware('admin')
        ->name('settings.company');

    Route::post('/settings/company', [DashboardController::class, 'companySettingsUpdate'])
        ->middleware('admin')
        ->name('settings.company.update');

    // =====================
    // Sales Summary
    // =====================
    Route::get('/sales', [DashboardController::class, 'sales'])->name('dashboard.sales');
    Route::get('/sales/export', [DashboardController::class, 'exportSales'])->name('dashboard.sales.export');
    Route::get('/sales/export/pdf', [DashboardController::class, 'exportSalesPdf'])->name('dashboard.sales.export.pdf');

    // =====================
    // Sales Report (Daily)
    // =====================
    Route::get('/sales/report', [DashboardController::class, 'report'])->name('dashboard.sales.report');
    Route::get('/sales/report/export', [DashboardController::class, 'exportReport'])->name('dashboard.sales.report.export');
    Route::get('/sales/report/export/pdf', [DashboardController::class, 'exportReportPdf'])->name('dashboard.sales.report.export.pdf');

    // =====================
    // Branch Details
    // =====================
    Route::get('/branch/{branchId}', [DashboardController::class, 'branchDetails'])->name('dashboard.branch.details');

    // حذف فواتير مشتريات محددة
    Route::post('dashboard/purchases/invoices/delete', [DashboardController::class, 'purchasesInvoicesDelete'])
        ->name('purchases.invoices.delete');

    // =====================
    // Reports (All-in-one)
    // =====================
    Route::get('/reports', [DashboardController::class, 'reportsIndex'])->name('dashboard.reports');
    Route::get('/reports/export', [DashboardController::class, 'exportReports'])->name('dashboard.reports.export');
    Route::get('/reports/export/pdf', [DashboardController::class, 'exportReportsPdf'])->name('dashboard.reports.export.pdf');
    Route::get('/reports/print', [DashboardController::class, 'reportsPrint'])->name('dashboard.reports.print');

    // =========================
    // Reports Tabs (Sales / Purchases / Expenses)
    // =========================

    // SALES TAB
    Route::get('/reports/sales', [DashboardController::class, 'reportsSalesIndex'])->name('dashboard.reports.sales');
    Route::get('/reports/sales/export', [DashboardController::class, 'exportReportsSales'])->name('dashboard.reports.sales.export');
    Route::get('/reports/sales/export/pdf', [DashboardController::class, 'exportReportsSalesPdf'])->name('dashboard.reports.sales.export.pdf');
    Route::get('/reports/sales/print', [DashboardController::class, 'reportsSalesPrint'])->name('dashboard.reports.sales.print');

    // PURCHASES TAB
    Route::get('/reports/purchases', [DashboardController::class, 'reportsPurchasesIndex'])->name('dashboard.reports.purchases');
    Route::get('/reports/purchases/export', [DashboardController::class, 'exportReportsPurchases'])->name('dashboard.reports.purchases.export');
    Route::get('/reports/purchases/export/pdf', [DashboardController::class, 'exportReportsPurchasesPdf'])->name('dashboard.reports.purchases.export.pdf');
    Route::get('/reports/purchases/print', [DashboardController::class, 'reportsPurchasesPrint'])->name('dashboard.reports.purchases.print');

    // EXPENSES TAB
    Route::get('/reports/expenses', [DashboardController::class, 'reportsExpensesIndex'])->name('dashboard.reports.expenses');
    Route::get('/reports/expenses/export', [DashboardController::class, 'exportReportsExpenses'])->name('dashboard.reports.expenses.export');
    Route::get('/reports/expenses/export/pdf', [DashboardController::class, 'exportReportsExpensesPdf'])->name('dashboard.reports.expenses.export.pdf');
    Route::get('/reports/expenses/print', [DashboardController::class, 'reportsExpensesPrint'])->name('dashboard.reports.expenses.print');

    // =====================
    // Expenses Settings
    // =====================
    Route::get('/expenses/settings', [DashboardController::class, 'expensesSettings'])->name('expenses.settings');

    Route::post('/expenses/settings/categories', [DashboardController::class, 'expenseCatsStore'])->name('expenses.cats.store');
    Route::post('/expenses/settings/categories/{id}', [DashboardController::class, 'expenseCatsUpdate'])->name('expenses.cats.update');
    Route::post('/expenses/settings/categories/{id}/toggle', [DashboardController::class, 'expenseCatsToggle'])->name('expenses.cats.toggle');

    Route::post('/expenses/settings/subcats', [DashboardController::class, 'expenseSubcatsStore'])->name('expenses.subcats.store');
    Route::post('/expenses/settings/subcats/{id}', [DashboardController::class, 'expenseSubcatsUpdate'])->name('expenses.subcats.update');
    Route::post('/expenses/settings/subcats/{id}/toggle', [DashboardController::class, 'expenseSubcatsToggle'])->name('expenses.subcats.toggle');

    // =====================
    // Settings
    // =====================
    Route::get('/settings', [DashboardController::class, 'settingsIndex'])->name('settings.index');

    Route::post('/settings/suppliers', [DashboardController::class, 'suppliersStore'])->name('settings.suppliers.store');
    Route::post('/settings/suppliers/{id}', [DashboardController::class, 'suppliersUpdate'])->name('settings.suppliers.update');
    Route::post('/settings/suppliers/{id}/toggle', [DashboardController::class, 'suppliersToggleActive'])->name('settings.suppliers.toggle');

    Route::post('/settings/items', [DashboardController::class, 'itemsStore'])->name('settings.items.store');
    Route::post('/settings/items/{id}', [DashboardController::class, 'itemsUpdate'])->name('settings.items.update');
    Route::post('/settings/items/{id}/toggle', [DashboardController::class, 'itemsToggleActive'])->name('settings.items.toggle');

    // =====================
    // Purchases
    // =====================
    Route::get('/purchases', [DashboardController::class, 'purchasesIndex'])->name('purchases.index');
    Route::post('/purchases', [DashboardController::class, 'purchasesStore'])->name('purchases.store');

    Route::get('/purchases/invoices', [DashboardController::class, 'purchasesInvoices'])->name('purchases.invoices');
    Route::get('/purchases/invoices/export', [DashboardController::class, 'exportPurchasesInvoices'])->name('purchases.invoices.export');
    Route::get('/purchases/invoices/export/pdf', [DashboardController::class, 'exportPurchasesInvoicesPdf'])->name('purchases.invoices.export.pdf');

    // =====================
    // Expenses
    // =====================
    Route::get('/expenses', [DashboardController::class, 'expensesIndex'])->name('expenses.index');
    Route::post('/expenses', [DashboardController::class, 'expensesStore'])->name('expenses.store');

    Route::get('/expenses/invoices', [DashboardController::class, 'expensesInvoices'])->name('expenses.invoices');
    Route::get('/expenses/invoices/export', [DashboardController::class, 'exportExpensesInvoices'])->name('expenses.invoices.export');
    Route::get('/expenses/invoices/export/pdf', [DashboardController::class, 'exportExpensesInvoicesPdf'])->name('expenses.invoices.export.pdf');

    Route::post('/expenses/invoices/delete', [DashboardController::class, 'expensesInvoicesDelete'])->name('expenses.invoices.delete');
});

// =====================
// Users (VIEW: ADMIN + SUPER) / (CREATE+ROLE: ADMIN ONLY) / (RESET: ADMIN + SUPER)
// =====================

// ✅ عرض شاشة المستخدمين للسوبر + الأدمن
Route::get('/dashboard/users', [UserAdminController::class, 'index'])
    ->middleware(['auth', 'admin_or_super'])
    ->name('users.index');

// ✅ إنشاء مستخدم (Admin فقط)
Route::post('/dashboard/users', [UserAdminController::class, 'store'])
    ->middleware(['auth', 'admin'])
    ->name('users.store');

// ✅ تعديل دور المستخدم (Admin فقط)
Route::post('/dashboard/users/{userId}/role', [UserAdminController::class, 'updateRole'])
    ->middleware(['auth', 'admin'])
    ->name('users.updateRole');

// ✅ Reset Password (Admin + Super)
Route::post('/dashboard/users/{userId}/reset-password', [UserAdminController::class, 'resetPassword'])
    ->middleware(['auth', 'admin_or_super'])
    ->name('users.resetPassword');


// ✅ تفعيل/إيقاف مستخدم (Admin + Super)
Route::post('/dashboard/users/{userId}/toggle-active', [UserAdminController::class, 'toggleActive'])
    ->middleware(['auth', 'admin_or_super'])
    ->name('users.toggleActive');

// ✅ حذف مستخدم (Admin فقط)
Route::post('/dashboard/users/{userId}/delete', [UserAdminController::class, 'destroy'])
    ->middleware(['auth', 'admin'])
    ->name('users.destroy');



// =====================
// Branch Admin (ADMIN ONLY)
// =====================
Route::middleware(['auth', 'admin'])->group(function () {
    Route::get('/dashboard/branches', [BranchAdminController::class, 'index'])->name('branches.index');
    Route::post('/dashboard/branches', [BranchAdminController::class, 'store'])->name('branches.store');

    Route::post('/dashboard/branches/{id}/toggle-active', [BranchAdminController::class, 'toggleActive'])->name('branches.toggleActive');

    Route::get('/dashboard/admin/invoices/purge', [BranchAdminController::class, 'showPurgeInvoices'])->name('admin.invoices.purge.form');
    Route::get('/dashboard/admin/invoices/backup', [BranchAdminController::class, 'backupInvoices'])->name('admin.invoices.backup');
    Route::post('/dashboard/admin/invoices/purge', [BranchAdminController::class, 'purgeInvoices'])->name('admin.invoices.purge.do');
    Route::post('/dashboard/admin/invoices/restore', [BranchAdminController::class, 'restoreInvoices'])->name('admin.invoices.restore');
});

// =====================
// Profile
// =====================
Route::middleware('auth')->group(function () {
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
});
