<?php

use App\Http\Controllers\AdminFinanceController;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\UserFinanceController;
use Illuminate\Support\Facades\Route;

Route::get('/', fn () => view('welcome'))->name('home');

Route::middleware('guest')->group(function () {
    Route::get('/login', [AuthController::class, 'showLogin'])->name('login');
    Route::post('/login', [AuthController::class, 'login'])->name('login.post');
    Route::get('/register', [AuthController::class, 'showRegister'])->name('register');
    Route::post('/register', [AuthController::class, 'register'])->name('register.post');
    Route::get('/signup', [AuthController::class, 'showRegister'])->name('signup');
    Route::get('/user-register', [AuthController::class, 'showRegister'])->name('user.register.public');
    Route::get('/create-account', [AuthController::class, 'showRegister'])->name('create.account');
});

Route::post('/logout', [AuthController::class, 'logout'])->middleware('auth')->name('logout');

Route::middleware('auth')->group(function () {
    Route::get('/dashboard', function () {
        return auth()->user()->isAdmin()
            ? redirect()->route('admin.dashboard')
            : redirect()->route('user.dashboard');
    })->name('dashboard');

    Route::prefix('admin')->name('admin.')->group(function () {
        Route::get('/dashboard', [AdminFinanceController::class, 'dashboard'])->name('dashboard');
        Route::get('/accounts', [AdminFinanceController::class, 'accounts'])->name('accounts');
        Route::post('/accounts', [AdminFinanceController::class, 'storeAccount'])->name('accounts.store');
        Route::get('/contacts', [AdminFinanceController::class, 'contacts'])->name('contacts');
        Route::post('/contacts', [AdminFinanceController::class, 'storeContact'])->name('contacts.store');
        Route::get('/banks', [AdminFinanceController::class, 'banks'])->name('banks');
        Route::post('/banks', [AdminFinanceController::class, 'storeBank'])->name('banks.store');
        Route::get('/invoices', [AdminFinanceController::class, 'invoices'])->name('invoices');
        Route::post('/invoices', [AdminFinanceController::class, 'storeInvoice'])->name('invoices.store');
        Route::post('/invoices/{invoice}/receive-payment', [AdminFinanceController::class, 'receiveInvoicePayment'])->name('invoices.receive');
        Route::get('/bills', [AdminFinanceController::class, 'bills'])->name('bills');
        Route::post('/bills', [AdminFinanceController::class, 'storeBill'])->name('bills.store');
        Route::post('/bills/{bill}/pay', [AdminFinanceController::class, 'payBill'])->name('bills.pay');
        Route::get('/claims', [AdminFinanceController::class, 'claims'])->name('claims');
        Route::post('/claims/{claim}', [AdminFinanceController::class, 'updateClaim'])->name('claims.update');
        Route::get('/advances', [AdminFinanceController::class, 'advances'])->name('advances');
        Route::post('/advances/{advance}', [AdminFinanceController::class, 'updateAdvance'])->name('advances.update');
        Route::get('/journals', [AdminFinanceController::class, 'journals'])->name('journals');
        Route::post('/journals', [AdminFinanceController::class, 'storeJournal'])->name('journals.store');
        Route::get('/budgets', [AdminFinanceController::class, 'budgets'])->name('budgets');
        Route::post('/budgets', [AdminFinanceController::class, 'storeBudget'])->name('budgets.store');
        Route::get('/payroll', [AdminFinanceController::class, 'payroll'])->name('payroll');
        Route::post('/payroll', [AdminFinanceController::class, 'storePayroll'])->name('payroll.store');
        Route::get('/taxes', [AdminFinanceController::class, 'taxes'])->name('taxes');
        Route::post('/taxes', [AdminFinanceController::class, 'storeTax'])->name('taxes.store');
        Route::get('/reports', [AdminFinanceController::class, 'reports'])->name('reports');
        Route::get('/users', [AdminFinanceController::class, 'users'])->name('users');
        Route::post('/users/{user}', [AdminFinanceController::class, 'updateUser'])->name('users.update');
        Route::get('/audit-logs', [AdminFinanceController::class, 'auditLogs'])->name('audit');
    });

    Route::prefix('user')->name('user.')->group(function () {
        Route::get('/dashboard', [UserFinanceController::class, 'dashboard'])->name('dashboard');
        Route::get('/profile', [UserFinanceController::class, 'profile'])->name('profile');
        Route::post('/profile', [UserFinanceController::class, 'updateProfile'])->name('profile.update');
        Route::get('/claims', [UserFinanceController::class, 'claims'])->name('claims');
        Route::post('/claims', [UserFinanceController::class, 'storeClaim'])->name('claims.store');
        Route::get('/advances', [UserFinanceController::class, 'advances'])->name('advances');
        Route::post('/advances', [UserFinanceController::class, 'storeAdvance'])->name('advances.store');
    });
});
