<?php

use Illuminate\Support\Facades\Route;
use App\Modules\Finance\Controllers\UtilityFeeController;
use App\Modules\Finance\Controllers\AccountingReportController;

use App\Modules\Finance\Controllers\AccountPayableController;

Route::middleware('auth')->group(function () {
    // 應付帳款
    Route::group(['prefix' => 'finance'], function () {
        Route::middleware('permission:account_payables.view')->group(function () {
            Route::get('/account-payables', [AccountPayableController::class, 'index'])->name('account-payables.index');
            Route::get('/account-payables/{accountPayable}', [AccountPayableController::class, 'show'])->name('account-payables.show');
        });
        Route::middleware('permission:account_payables.edit')->group(function () {
            Route::post('/account-payables/{accountPayable}/invoice', [AccountPayableController::class, 'updateInvoice'])->name('account-payables.invoice');
        });
        Route::middleware('permission:account_payables.pay')->group(function () {
            Route::post('/account-payables/{accountPayable}/pay', [AccountPayableController::class, 'pay'])->name('account-payables.pay');
        });
    });

    // 公共事業費管理
    Route::middleware('permission:utility_fees.view')->group(function () {
        Route::get('/utility-fees', [UtilityFeeController::class, 'index'])->name('utility-fees.index');
    });
    Route::middleware('permission:utility_fees.create')->group(function () {
        Route::post('/utility-fees', [UtilityFeeController::class, 'store'])->name('utility-fees.store');
    });
    Route::middleware('permission:utility_fees.edit')->group(function () {
        Route::put('/utility-fees/{utility_fee}', [UtilityFeeController::class, 'update'])->name('utility-fees.update');
        
        // 附件管理 (Ajax)
        Route::get('/utility-fees/{utility_fee}/attachments', [UtilityFeeController::class, 'attachments'])->name('utility-fees.attachments');
        Route::post('/utility-fees/{utility_fee}/attachments', [UtilityFeeController::class, 'uploadAttachment'])->name('utility-fees.upload-attachment');
        Route::delete('/utility-fees/{utility_fee}/attachments/{attachment}', [UtilityFeeController::class, 'deleteAttachment'])->name('utility-fees.delete-attachment');
    });
    Route::middleware('permission:utility_fees.delete')->group(function () {
        Route::delete('/utility-fees/{utility_fee}', [UtilityFeeController::class, 'destroy'])->name('utility-fees.destroy');
    });

    // 會計報表
    Route::middleware('permission:accounting.view')->prefix('accounting-report')->group(function () {
        Route::get('/', [AccountingReportController::class, 'index'])->name('accounting.report');
        Route::get('/export', [AccountingReportController::class, 'export'])
            ->middleware('permission:accounting.export')
            ->name('accounting.export');
    });
});
