<?php

use App\Http\Controllers\AuthController;
use App\Http\Controllers\BackupController;
use App\Http\Controllers\CustomerController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\InvoiceController;
use App\Http\Controllers\MasterDataController;
use App\Http\Controllers\PaymentController;
use App\Http\Controllers\ProductController;
use App\Http\Controllers\PurchaseController;
use App\Http\Controllers\ExpenseController;
use App\Http\Controllers\FinanceController;
use App\Http\Controllers\AssetController;
use App\Http\Controllers\ReportController;
use App\Http\Controllers\ReturnController;
use App\Http\Controllers\SettingController;
use App\Http\Controllers\SupplierController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\VoucherController;
use App\Http\Controllers\TranslationController;
use App\Http\Controllers\WizardController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Route::get('/login', [AuthController::class, 'create'])->name('login');
Route::post('/login', [AuthController::class, 'store'])->name('login.store');
Route::post('/locale/{locale}', function (Request $request, string $locale) { abort_unless(in_array($locale, ['en', 'fa', 'ps']), 404); $request->session()->put('locale', $locale); return back(); })->name('locale');

Route::middleware('auth')->group(function () {
    Route::get('/', DashboardController::class)->name('dashboard');
    Route::resource('products', ProductController::class)->except('show');
    Route::resource('customers', CustomerController::class)->except('show');
    Route::resource('suppliers', SupplierController::class)->except('show');
    Route::get('invoices', [InvoiceController::class, 'index'])->name('invoices.index');
    Route::get('invoices/create', [InvoiceController::class, 'create'])->name('invoices.create');
    Route::post('invoices', [InvoiceController::class, 'store'])->name('invoices.store');
    Route::get('invoices/{invoice}', [InvoiceController::class, 'show'])->name('invoices.show');
    Route::post('invoices/{invoice}/payment', [InvoiceController::class, 'payment'])->name('invoices.payment');
    Route::get('payments', [PaymentController::class, 'index'])->name('payments.index');
    Route::post('payments/receive', [PaymentController::class, 'receive'])->name('payments.receive');
    Route::post('payments/supplier', [PaymentController::class, 'paySupplier'])->name('payments.supplier');
    Route::post('payments/general', [PaymentController::class, 'general'])->name('payments.general');
    Route::get('purchases', [PurchaseController::class, 'index'])->name('purchases.index');
    Route::get('purchases/create', [PurchaseController::class, 'create'])->name('purchases.create');
    Route::post('purchases', [PurchaseController::class, 'store'])->name('purchases.store');
    Route::get('purchases/{purchase}', [PurchaseController::class, 'show'])->name('purchases.show');
    Route::post('purchases/{purchase}/payment', [PurchaseController::class, 'payment'])->name('purchases.payment');
    Route::get('expenses', [ExpenseController::class, 'index'])->name('expenses.index');
    Route::post('expenses', [ExpenseController::class, 'store'])->name('expenses.store');
    Route::delete('expenses/{expense}', [ExpenseController::class, 'destroy'])->name('expenses.destroy');
    Route::get('reports', [ReportController::class, 'index'])->name('reports.index');
    Route::get('reports/sales', [ReportController::class, 'sales'])->name('reports.sales');
    Route::get('reports/purchases', [ReportController::class, 'purchases'])->name('reports.purchases');
    Route::get('reports/stock', [ReportController::class, 'stock'])->name('reports.stock');
    Route::get('reports/payments', [ReportController::class, 'payments'])->name('reports.payments');
    Route::get('reports/receipts', [ReportController::class, 'receipts'])->name('reports.receipts');
    Route::get('reports/balance-sheet', [ReportController::class, 'balanceSheet'])->name('reports.balance-sheet');
    Route::get('reports/chart-of-accounts', [ReportController::class, 'chartOfAccounts'])->name('reports.chart-of-accounts');
    Route::get('reports/income-statement', [ReportController::class, 'incomeStatement'])->name('reports.income-statement');
    Route::get('reports/customer-balances', [ReportController::class, 'customerBalances'])->name('reports.customer-balances');
    Route::get('reports/supplier-balances', [ReportController::class, 'supplierBalances'])->name('reports.supplier-balances');
    Route::get('returns', [ReturnController::class, 'index'])->name('returns.index');
    Route::post('returns', [ReturnController::class, 'store'])->name('returns.store');
    Route::delete('returns/{return}', [ReturnController::class, 'destroy'])->name('returns.destroy');
    Route::get('finance', [FinanceController::class, 'index'])->middleware('role:manager,accountant')->name('finance.index');
    Route::post('finance/accounts', [FinanceController::class, 'storeAccount'])->middleware('role:manager,accountant')->name('finance.accounts.store');
    Route::post('finance/exchange', [FinanceController::class, 'exchange'])->middleware('role:manager,accountant')->name('finance.exchange');
    Route::get('assets', [AssetController::class, 'index'])->middleware('role:manager,accountant')->name('assets.index');
    Route::post('assets', [AssetController::class, 'store'])->middleware('role:manager,accountant')->name('assets.store');
    Route::get('vouchers', [VoucherController::class, 'index'])->middleware('role:manager,accountant')->name('vouchers.index');
    Route::post('vouchers', [VoucherController::class, 'store'])->middleware('role:manager,accountant')->name('vouchers.store');
    Route::get('users', [UserController::class, 'index'])->middleware('role:manager')->name('users.index');
    Route::post('users', [UserController::class, 'store'])->middleware('role:manager')->name('users.store');
    Route::put('users/{user}/role', [UserController::class, 'updateRole'])->middleware('role:manager')->name('users.role');
    Route::get('reports/customers/{customer}', [ReportController::class, 'customerLedger'])->name('reports.customer-ledger');
    Route::get('reports/suppliers/{supplier}', [ReportController::class, 'supplierLedger'])->name('reports.supplier-ledger');
    Route::get('settings', [SettingController::class, 'index'])->name('settings.index');
    Route::put('settings', [SettingController::class, 'update'])->name('settings.update');
    Route::post('settings/currencies', [SettingController::class, 'storeCurrency'])->name('currencies.store');
    Route::put('settings/currencies/{currency}/default', [SettingController::class, 'defaultCurrency'])->name('currencies.default');
    Route::post('settings/categories', [MasterDataController::class, 'storeCategory'])->name('categories.store');
    Route::delete('settings/categories/{category}', [MasterDataController::class, 'destroyCategory'])->name('categories.destroy');
    Route::post('settings/units', [MasterDataController::class, 'storeUnit'])->name('units.store');
    Route::delete('settings/units/{unit}', [MasterDataController::class, 'destroyUnit'])->name('units.destroy');
    Route::get('translations', [TranslationController::class, 'index'])->name('translations.index');
    Route::post('translations', [TranslationController::class, 'store'])->name('translations.store');
    Route::delete('translations/{translation}', [TranslationController::class, 'destroy'])->name('translations.destroy');
    Route::get('wizards', [WizardController::class, 'index'])->name('wizards.index');
    Route::get('backup', [BackupController::class, 'index'])->middleware('role:manager')->name('backup.index');
    Route::post('backup/download', [BackupController::class, 'download'])->middleware('role:manager')->name('backup.download');
    Route::post('backup/restore', [BackupController::class, 'restore'])->middleware('role:manager')->name('backup.restore');
    Route::view('desktop', 'desktop.index')->name('desktop.index');
    Route::post('/logout', [AuthController::class, 'destroy'])->name('logout');
});
