<?php

use Illuminate\Support\Facades\Route;

Route::get('/', fn () => redirect()->route('dashboard'));

Route::middleware('auth')->group(function () {

    // Dashboard
    Route::get('/dashboard', [App\Http\Controllers\DashboardController::class, 'index'])->name('dashboard');

    // Purchase
    Route::get('purchase/print/{purchase}', [App\Http\Controllers\PrintController::class, 'purchaseInvoice'])->name('purchase.print');
    Route::get('purchase/history', [App\Http\Controllers\ReportController::class, 'purchase'])->name('purchase.history');
    Route::get('purchase/summary', [App\Http\Controllers\ReportController::class, 'purchase'])->name('purchase.summary');
    Route::resource('purchase', App\Http\Controllers\PurchaseController::class)
        ->parameters(['purchase' => 'purchase']);

    // Purchase Returns
    Route::prefix('purchase-return')->name('purchase-return.')->group(function () {
        Route::get('/', [App\Http\Controllers\PurchaseReturnController::class, 'index'])->name('index');
        Route::get('create', [App\Http\Controllers\PurchaseReturnController::class, 'create'])->name('create');
        Route::post('/', [App\Http\Controllers\PurchaseReturnController::class, 'store'])->name('store');
        Route::get('{return}', [App\Http\Controllers\PurchaseReturnController::class, 'show'])->name('show');
        Route::get('{return}/edit', [App\Http\Controllers\PurchaseReturnController::class, 'edit'])->name('edit');
        Route::put('{return}', [App\Http\Controllers\PurchaseReturnController::class, 'update'])->name('update');
        Route::delete('{return}', [App\Http\Controllers\PurchaseReturnController::class, 'destroy'])->name('destroy');
    });
    Route::get('purchase/return', fn() => redirect()->route('purchase-return.index'))->name('purchase.return');

    // Sales
    Route::get('sales/history', [App\Http\Controllers\ReportController::class, 'sales'])->name('sales.history');
    Route::get('sales/summary', [App\Http\Controllers\ReportController::class, 'sales'])->name('sales.summary');
    Route::resource('sales', App\Http\Controllers\SaleController::class)
        ->parameters(['sales' => 'sale']);

    // Sales Returns
    Route::prefix('sale-return')->name('sale-return.')->group(function () {
        Route::get('/', [App\Http\Controllers\SaleReturnController::class, 'index'])->name('index');
        Route::get('create', [App\Http\Controllers\SaleReturnController::class, 'create'])->name('create');
        Route::post('/', [App\Http\Controllers\SaleReturnController::class, 'store'])->name('store');
        Route::get('{return}', [App\Http\Controllers\SaleReturnController::class, 'show'])->name('show');
        Route::get('{return}/edit', [App\Http\Controllers\SaleReturnController::class, 'edit'])->name('edit');
        Route::put('{return}', [App\Http\Controllers\SaleReturnController::class, 'update'])->name('update');
        Route::delete('{return}', [App\Http\Controllers\SaleReturnController::class, 'destroy'])->name('destroy');
    });
    Route::get('sales/return', fn() => redirect()->route('sale-return.index'))->name('sales.return');

    // Stock
    Route::get('/stock', [App\Http\Controllers\StockController::class, 'index'])->name('stock.index');
    Route::get('/stock/ledger', [App\Http\Controllers\StockController::class, 'ledger'])->name('stock.ledger');
    Route::get('/stock/summary', [App\Http\Controllers\StockController::class, 'index'])->name('stock.summary');
    Route::get('/stock/valuation', [App\Http\Controllers\StockController::class, 'index'])->name('stock.valuation');

    // Yarn CRUD
    Route::resource('yarn', App\Http\Controllers\YarnController::class);
    Route::post('yarn/{id}/restore', [App\Http\Controllers\YarnController::class, 'restore'])->name('yarn.restore');
    Route::delete('yarn/{id}/force', [App\Http\Controllers\YarnController::class, 'forceDelete'])->name('yarn.force-delete');

    // Challan
    Route::get('/challan', [App\Http\Controllers\ChallanController::class, 'index'])->name('challan.index');
    Route::get('/challan/create', [App\Http\Controllers\ChallanController::class, 'create'])->name('challan.create');
    Route::get('/challan/print/{id?}', [App\Http\Controllers\ChallanController::class, 'print'])->name('challan.print');
    Route::post('/challan', [App\Http\Controllers\ChallanController::class, 'store'])->name('challan.store');
    Route::patch('/challan/{challan}/status', [App\Http\Controllers\ChallanController::class, 'updateStatus'])->name('challan.status');

    // Parties
    Route::get('/suppliers', [App\Http\Controllers\PartyController::class, 'suppliers'])->name('supplier.index');
    Route::get('/customers', [App\Http\Controllers\PartyController::class, 'customers'])->name('customer.index');
    Route::get('/party/{party}', [App\Http\Controllers\PartyController::class, 'show'])->name('party.show');
    Route::get('/party/{party}/edit', [App\Http\Controllers\PartyController::class, 'edit'])->name('party.edit');
    Route::put('/party/{party}', [App\Http\Controllers\PartyController::class, 'update'])->name('party.update');
    Route::post('/parties', [App\Http\Controllers\PartyController::class, 'store'])->name('party.store');
    Route::delete('/parties/{party}', [App\Http\Controllers\PartyController::class, 'destroy'])->name('party.destroy');
    Route::post('/party/{id}/restore', [App\Http\Controllers\PartyController::class, 'restore'])->name('party.restore');
    Route::delete('/party/{id}/force', [App\Http\Controllers\PartyController::class, 'forceDelete'])->name('party.force-delete');

    // Accounts / Cash
    Route::get('/cash', [App\Http\Controllers\AccountController::class, 'index'])->name('cash.index');
    Route::get('/cash/receipt', [App\Http\Controllers\AccountController::class, 'receiptForm'])->name('cash.receipt');
    Route::post('/cash/receipt', [App\Http\Controllers\AccountController::class, 'storeReceipt']);
    Route::get('/cash/payment', [App\Http\Controllers\AccountController::class, 'paymentForm'])->name('cash.payment');
    Route::post('/cash/payment', [App\Http\Controllers\AccountController::class, 'storePayment']);
    Route::get('/cash/book', [App\Http\Controllers\AccountController::class, 'cashBook'])->name('cash.book');
    Route::get('/bank/book', [App\Http\Controllers\AccountController::class, 'bankBook'])->name('bank.book');

    // Vouchers CRUD
    Route::resource('vouchers', App\Http\Controllers\VoucherController::class);
    Route::post('vouchers/{id}/restore', [App\Http\Controllers\VoucherController::class, 'restore'])->name('vouchers.restore');

    // Reports
    Route::get('/reports/purchase', [App\Http\Controllers\ReportController::class, 'purchase'])->name('reports.purchase');
    Route::get('/reports/sales', [App\Http\Controllers\ReportController::class, 'sales'])->name('reports.sales');
    Route::get('/reports/stock', [App\Http\Controllers\ReportController::class, 'stock'])->name('reports.stock');
    Route::get('/reports/supplier', [App\Http\Controllers\ReportController::class, 'supplierLedger'])->name('reports.supplier');
    Route::get('/reports/customer', [App\Http\Controllers\ReportController::class, 'customerLedger'])->name('reports.customer');
    Route::get('/reports/profit-loss', [App\Http\Controllers\ReportController::class, 'profitLoss'])->name('reports.pl');
    Route::get('/reports/daily', [App\Http\Controllers\ReportController::class, 'dailyTransaction'])->name('reports.daily');
    Route::get('/reports/cash', [App\Http\Controllers\ReportController::class, 'cashBook'])->name('reports.cash');
    Route::get('/reports/bank', [App\Http\Controllers\ReportController::class, 'bankBook'])->name('reports.bank');

    // Daily
    Route::get('/daily/register', [App\Http\Controllers\ReportController::class, 'dailyTransaction'])->name('daily.register');

    // Recycle Bin
    Route::get('/recycle-bin', [App\Http\Controllers\RecycleController::class, 'index'])->name('recycle.bin');
    Route::post('/recycle/purchase/{id}/restore', [App\Http\Controllers\RecycleController::class, 'restorePurchase'])->name('recycle.purchase.restore');
    Route::delete('/recycle/purchase/{id}', [App\Http\Controllers\RecycleController::class, 'destroyPurchase'])->name('recycle.purchase.destroy');
    Route::post('/recycle/sale/{id}/restore', [App\Http\Controllers\RecycleController::class, 'restoreSale'])->name('recycle.sale.restore');
    Route::delete('/recycle/sale/{id}', [App\Http\Controllers\RecycleController::class, 'destroySale'])->name('recycle.sale.destroy');
    Route::post('/recycle/party/{id}/restore', [App\Http\Controllers\RecycleController::class, 'restoreParty'])->name('recycle.party.restore');
    Route::delete('/recycle/party/{id}', [App\Http\Controllers\RecycleController::class, 'destroyParty'])->name('recycle.party.destroy');

    // AI
    Route::get('/ai/dashboard', [App\Http\Controllers\AiController::class, 'dashboard'])->name('ai.dashboard');

    // Settings & Profile
    Route::view('/settings', 'settings.index')->name('settings.index');
    Route::view('/profile', 'profile.index')->name('profile.index');
});

require __DIR__ . '/auth.php';
