<?php

use App\Http\Controllers\AccountController;
use App\Http\Controllers\BudgetController;
use App\Http\Controllers\CategoryController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\GoalController;
use App\Http\Controllers\ImportController;
use App\Http\Controllers\NotificationController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\RecurringTransactionController;
use App\Http\Controllers\ReportController;
use App\Http\Controllers\SettingsController;
use App\Http\Controllers\TagController;
use App\Http\Controllers\TransactionController;
use App\Http\Controllers\TransferController;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Artisan;

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

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

// Storage file access (for cPanel hosting where symlinks don't work)
// Route::get('/storage/{path}', [StorageController::class, 'show'])->where('path', '.*');

/*
|--------------------------------------------------------------------------
| Authenticated Routes
|--------------------------------------------------------------------------
*/

Route::middleware(['auth', 'verified'])->group(function () {

    // Dashboard
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
    Route::get('/dashboard/chart-data', [DashboardController::class, 'getChartData'])->name('dashboard.chart-data');
    Route::get('/dashboard/summary', [DashboardController::class, 'getSummary'])->name('dashboard.summary');

    // Accounts
    Route::resource('accounts', AccountController::class);
    Route::post('/accounts/{account}/restore', [AccountController::class, 'restore'])->name('accounts.restore')->withTrashed();
    Route::post('/accounts/{account}/update-balance', [AccountController::class, 'updateBalance'])->name('accounts.update-balance');
    Route::post('/accounts/reorder', [AccountController::class, 'reorder'])->name('accounts.reorder');

    // Transactions
    Route::resource('transactions', TransactionController::class);
    Route::post('/transactions/bulk-destroy', [TransactionController::class, 'bulkDestroy'])->name('transactions.bulk-destroy');
    Route::post('/transactions/{transaction}/duplicate', [TransactionController::class, 'duplicate'])->name('transactions.duplicate');
    Route::post('/transactions/quick', [TransactionController::class, 'quickStore'])->name('transactions.quick-store');

    // Transfers
    Route::resource('transfers', TransferController::class)->except(['edit', 'update']);
    Route::get('/transfers/exchange-rate', [TransferController::class, 'getExchangeRate'])->name('transfers.exchange-rate');

    // Categories
    Route::resource('categories', CategoryController::class);
    Route::post('/categories/reorder', [CategoryController::class, 'reorder'])->name('categories.reorder');
    Route::post('/categories/{category}/toggle-active', [CategoryController::class, 'toggleActive'])->name('categories.toggle-active');

    // Budgets
    Route::resource('budgets', BudgetController::class);
    Route::get('/budgets/{budget}/progress', [BudgetController::class, 'getProgress'])->name('budgets.progress');
    Route::post('/budgets/{budget}/toggle-active', [BudgetController::class, 'toggleActive'])->name('budgets.toggle-active');

    // Goals
    Route::resource('goals', GoalController::class);
    Route::post('/goals/{goal}/contribute', [GoalController::class, 'contribute'])->name('goals.contribute');
    Route::delete('/goals/{goal}/contributions/{contribution}', [GoalController::class, 'removeContribution'])->name('goals.remove-contribution');
    Route::post('/goals/{goal}/achieve', [GoalController::class, 'achieve'])->name('goals.achieve');
    Route::post('/goals/{goal}/cancel', [GoalController::class, 'cancel'])->name('goals.cancel');
    Route::post('/goals/{goal}/reactivate', [GoalController::class, 'reactivate'])->name('goals.reactivate');

    // Recurring Transactions
    Route::resource('recurring-transactions', RecurringTransactionController::class);
    Route::post('/recurring-transactions/{recurring_transaction}/skip', [RecurringTransactionController::class, 'skip'])->name('recurring-transactions.skip');
    Route::post('/recurring-transactions/{recurring_transaction}/process', [RecurringTransactionController::class, 'process'])->name('recurring-transactions.process');
    Route::post('/recurring-transactions/{recurring_transaction}/toggle-active', [RecurringTransactionController::class, 'toggleActive'])->name('recurring-transactions.toggle-active');

    // Tags
    Route::resource('tags', TagController::class)->except(['show', 'create', 'edit']);
    Route::get('/tags/search', [TagController::class, 'search'])->name('tags.search');

    // Reports
    Route::prefix('reports')->name('reports.')->group(function () {
        Route::get('/', [ReportController::class, 'index'])->name('index');
        Route::get('/income-expense', [ReportController::class, 'incomeExpense'])->name('income-expense');
        Route::get('/category-breakdown', [ReportController::class, 'categoryBreakdown'])->name('category-breakdown');
        Route::get('/trend', [ReportController::class, 'trend'])->name('trend');
        Route::get('/net-worth', [ReportController::class, 'netWorth'])->name('net-worth');
        Route::get('/export', [ReportController::class, 'export'])->name('export');
    });

    // Import
    Route::get('/imports', [ImportController::class, 'index'])->name('imports.index');
    Route::get('/imports/create', [ImportController::class, 'create'])->name('imports.create');
    Route::post('/imports/preview', [ImportController::class, 'preview'])->name('imports.preview');
    Route::post('/imports/process', [ImportController::class, 'process'])->name('imports.process');
    Route::get('/imports/{import}', [ImportController::class, 'show'])->name('imports.show');
    Route::delete('/imports/{import}', [ImportController::class, 'destroy'])->name('imports.destroy');

    // Notifications
    Route::prefix('notifications')->name('notifications.')->group(function () {
        Route::get('/', [NotificationController::class, 'index'])->name('index');
        Route::get('/latest', [NotificationController::class, 'latest'])->name('latest');
        Route::post('/{notification}/read', [NotificationController::class, 'markAsRead'])->name('mark-read');
        Route::post('/mark-all-read', [NotificationController::class, 'markAllAsRead'])->name('mark-all-read');
        Route::delete('/{notification}', [NotificationController::class, 'destroy'])->name('destroy');
        Route::delete('/read/destroy', [NotificationController::class, 'destroyRead'])->name('destroy-read');
    });

    // Settings
    Route::get('/settings', [SettingsController::class, 'index'])->name('settings.index');
    Route::patch('/settings', [SettingsController::class, 'update'])->name('settings.update');
    Route::get('/settings/export-data', [SettingsController::class, 'exportData'])->name('settings.export-data');
    Route::delete('/settings/delete-data', [SettingsController::class, 'deleteData'])->name('settings.delete-data');

    // Profile (from Breeze)
    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');
});

require __DIR__.'/auth.php';
