<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AccountController;
use App\Http\Controllers\AgentController;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\BorrowController;
use App\Http\Controllers\BuyCurrencyController;
use App\Http\Controllers\CurrencyController;
use App\Http\Controllers\PendingTransactionController;
use App\Http\Controllers\ProfitController;
use App\Http\Controllers\ReportsController;
use App\Http\Controllers\SellCurrencyController;
use App\Http\Controllers\SetupController;
use App\Http\Controllers\TransactionController;
use App\Http\Controllers\AdjustBalanceController;
use App\Http\Controllers\AgentBalanceController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::get('/login', [AuthController::class, 'showLogin'])->name('login');
Route::post('/login', [AuthController::class, 'login'])->name('login.submit');
Route::post('/logout', [AuthController::class, 'logout'])->name('logout');

Route::middleware(['admin.login'])->group(function () {
    Route::get('/', [SetupController::class, 'dashboard'])->name('dashboard');
    Route::get('/stock', [SetupController::class, 'stockDashboard'])->name('stock.dashboard');
    
    Route::get('/setup/opening-balances', [SetupController::class, 'openingBalances'])->name('setup.opening-balances');
    Route::post('/setup/opening-balances', [SetupController::class, 'updateOpeningBalances'])->name('setup.opening-balances.update');

    Route::resource('currencies', CurrencyController::class)->only(['index', 'store', 'edit', 'update']);
    Route::resource('agents', AgentController::class)->only(['index', 'store', 'edit', 'update', 'destroy']);
    Route::resource('accounts', AccountController::class)->only(['index', 'store', 'edit', 'update', 'destroy']);
    Route::get('/accounts/summary', [AccountController::class, 'summary'])->name('accounts.summary');

    // Borrows
    Route::resource('borrows', BorrowController::class)->only(['index', 'create', 'store', 'edit', 'update']);
    Route::get('/borrows/{borrow}/settle', [BorrowController::class, 'settle'])->name('borrows.settle');
    Route::post('/borrows/{borrow}/settle', [BorrowController::class, 'processSettlement'])->name('borrows.process-settlement');

    // General Transaction actions
    Route::get('/transactions/buy', [BuyCurrencyController::class, 'create'])->name('transactions.buy.create');
    Route::post('/transactions/buy', [BuyCurrencyController::class, 'store'])->name('transactions.buy.store');
    Route::get('/transactions/buy/{transaction}/edit', [BuyCurrencyController::class, 'edit'])->name('transactions.buy.edit');
    Route::put('/transactions/buy/{transaction}', [BuyCurrencyController::class, 'update'])->name('transactions.buy.update');

    Route::get('/transactions/sell', [SellCurrencyController::class, 'create'])->name('transactions.sell.create');
    Route::post('/transactions/sell', [SellCurrencyController::class, 'store'])->name('transactions.sell.store');
    Route::get('/transactions/sell/{transaction}/edit', [SellCurrencyController::class, 'edit'])->name('transactions.sell.edit');
    Route::put('/transactions/sell/{transaction}', [SellCurrencyController::class, 'update'])->name('transactions.sell.update');

    Route::delete('/transactions/{transaction}', [TransactionController::class, 'destroy'])->name('transactions.destroy');

    // Pending Transactions
    Route::get('/transactions/pending', [PendingTransactionController::class, 'index'])->name('transactions.pending.index');
    Route::post('/transactions/pending/{transaction}/received', [PendingTransactionController::class, 'markReceived'])->name('transactions.pending.received');

    // Agent Balances
    Route::post('/agent-balances/{agent_balance}/received', [AgentBalanceController::class, 'markReceived'])->name('agent-balances.received');
    Route::resource('agent-balances', AgentBalanceController::class)->except(['create', 'show']);

    // Adjust Balance
    Route::get('/accounts/adjust', [AdjustBalanceController::class, 'index'])->name('accounts.adjust.index');
    Route::post('/accounts/adjust', [AdjustBalanceController::class, 'store'])->name('accounts.adjust.store');

    // Profit Summary
    Route::get('/profits', [ProfitController::class, 'index'])->name('profits.index');

    // Reports
    Route::get('/reports/currencies', [ReportsController::class, 'currencies'])->name('reports.currencies');
    Route::get('/reports/profits', [ReportsController::class, 'profits'])->name('reports.profits');
    Route::get('/reports/handling-fees', [ReportsController::class, 'handlingFees'])->name('reports.handling-fees');
    Route::get('/reports/accounts', [ReportsController::class, 'accounts'])->name('reports.accounts');
    Route::get('/reports/transactions', [ReportsController::class, 'transactions'])->name('reports.transactions');
    Route::get('/reports/transactions/{transaction}', [ReportsController::class, 'showTransaction'])->name('reports.transactions.show');
});