<?php

declare(strict_types=1);

use App\Core\Middleware\AuthMiddleware;
use App\Modules\Reporting\Presentation\Controllers\BankReportController;
use App\Modules\Reporting\Presentation\Controllers\DocumentPrintController;
use App\Modules\Reporting\Presentation\Controllers\ExpenseReportController;
use App\Modules\Reporting\Presentation\Controllers\InventoryReportController;
use App\Modules\Reporting\Presentation\Controllers\PartyStatementController;
use App\Modules\Reporting\Presentation\Controllers\ProfitReportController;
use App\Modules\Reporting\Presentation\Controllers\ReportsHubController;
use App\Modules\Reporting\Presentation\Controllers\GenericReportController;
use App\Modules\Reporting\Application\Support\ReportCatalog;
use App\Modules\Reporting\Presentation\Controllers\PurchaseReportController;
use App\Modules\Reporting\Presentation\Controllers\SupplierAgingReportController;
use App\Controllers\PublicQuotationPdfController;

$router->get('/reports', static function (): void { (new ReportsHubController())->index(); }, [new AuthMiddleware()]);
$router->get('/reports/ajax/lookup', static function (): void { (new GenericReportController())->lookup(); }, [new AuthMiddleware()]);

$router->get('/reports/parties/customers', static function (): void { (new PartyStatementController())->show('customer'); }, [new AuthMiddleware()]);
$router->get('/reports/parties/customers/print', static function (): void { $_GET['role'] = 'customer'; (new PartyStatementController())->print(); }, [new AuthMiddleware()]);
$router->get('/reports/parties/suppliers', static function (): void { (new PartyStatementController())->show('supplier'); }, [new AuthMiddleware()]);
$router->get('/reports/parties/suppliers/print', static function (): void { $_GET['role'] = 'supplier'; (new PartyStatementController())->print(); }, [new AuthMiddleware()]);
$router->get('/reports/parties/customer-statement', static function (): void { (new PartyStatementController())->show('customer'); }, [new AuthMiddleware()]);
$router->get('/reports/parties/customer-statement/print', static function (): void { $_GET['role'] = 'customer'; (new PartyStatementController())->print(); }, [new AuthMiddleware()]);
$router->get('/reports/parties/customer-statement/pdf', static function (): void { $_GET['role'] = 'customer'; (new PartyStatementController())->exportPdf(); }, [new AuthMiddleware()]);
$router->get('/reports/parties/customer-statement/export', static function (): void { $_GET['role'] = 'customer'; (new PartyStatementController())->exportExcel(); }, [new AuthMiddleware()]);
$router->get('/reports/parties/supplier-statement', static function (): void { (new PartyStatementController())->show('supplier'); }, [new AuthMiddleware()]);
$router->get('/reports/parties/supplier-statement/print', static function (): void { $_GET['role'] = 'supplier'; (new PartyStatementController())->print(); }, [new AuthMiddleware()]);
$router->get('/reports/parties/supplier-statement/pdf', static function (): void { $_GET['role'] = 'supplier'; (new PartyStatementController())->exportPdf(); }, [new AuthMiddleware()]);
$router->get('/reports/parties/supplier-statement/export', static function (): void { $_GET['role'] = 'supplier'; (new PartyStatementController())->exportExcel(); }, [new AuthMiddleware()]);
$router->get('/reports/parties/supplier-aging', static function (): void { (new SupplierAgingReportController())->index(); }, [new AuthMiddleware()]);
$router->get('/reports/parties/supplier-aging/print', static function (): void { (new SupplierAgingReportController())->print(); }, [new AuthMiddleware()]);
$router->get('/reports/parties/supplier-aging/pdf', static function (): void { (new SupplierAgingReportController())->exportPdf(); }, [new AuthMiddleware()]);
$router->get('/reports/parties/supplier-aging/export', static function (): void { (new SupplierAgingReportController())->exportExcel(); }, [new AuthMiddleware()]);

$router->get('/reports/inventory/stock', static function (): void { (new InventoryReportController())->stock(); }, [new AuthMiddleware()]);
$router->get('/reports/inventory/stock/print', static function (): void { (new InventoryReportController())->printStock(); }, [new AuthMiddleware()]);
$router->get('/reports/inventory/stock/pdf', static function (): void { (new InventoryReportController())->exportStockPdf(); }, [new AuthMiddleware()]);
$router->get('/reports/inventory/stock/export', static function (): void { (new InventoryReportController())->exportStockExcel(); }, [new AuthMiddleware()]);
$router->get('/reports/inventory/supplier-stock', static function (): void { (new InventoryReportController())->supplierStock(); }, [new AuthMiddleware()]);
$router->get('/reports/inventory/supplier-stock/print', static function (): void { (new InventoryReportController())->printSupplierStock(); }, [new AuthMiddleware()]);
$router->get('/reports/inventory/supplier-stock/pdf', static function (): void { (new InventoryReportController())->exportSupplierStockPdf(); }, [new AuthMiddleware()]);
$router->get('/reports/inventory/supplier-stock/export', static function (): void { (new InventoryReportController())->exportSupplierStockExcel(); }, [new AuthMiddleware()]);
$router->get('/reports/inventory/item-movement', static function (): void { (new InventoryReportController())->itemMovement(); }, [new AuthMiddleware()]);
$router->get('/reports/inventory/item-movement/print', static function (): void { (new InventoryReportController())->printItemMovement(); }, [new AuthMiddleware()]);
$router->get('/reports/inventory/item-movement/pdf', static function (): void { (new InventoryReportController())->exportItemMovementPdf(); }, [new AuthMiddleware()]);
$router->get('/reports/inventory/item-movement/export', static function (): void { (new InventoryReportController())->exportItemMovementExcel(); }, [new AuthMiddleware()]);
$router->get('/reports/inventory/item-balance', static function (): void { (new App\Modules\Inventory\Presentation\Controllers\ItemBalanceController())->index(); }, [new AuthMiddleware()]);

$router->get('/reports/purchases/period', static function (): void { (new GenericReportController())->show('purchases-period'); }, [new AuthMiddleware()]);
$router->get('/reports/purchases/period/print', static function (): void { (new GenericReportController())->print('purchases-period'); }, [new AuthMiddleware()]);
$router->get('/reports/purchases/period/pdf', static function (): void { (new GenericReportController())->pdf('purchases-period'); }, [new AuthMiddleware()]);
$router->get('/reports/purchases/period/export', static function (): void { (new GenericReportController())->excel('purchases-period'); }, [new AuthMiddleware()]);
$router->get('/reports/purchases/by-supplier', static function (): void { (new GenericReportController())->show('purchases-by-supplier'); }, [new AuthMiddleware()]);
$router->get('/reports/purchases/by-supplier/print', static function (): void { (new GenericReportController())->print('purchases-by-supplier'); }, [new AuthMiddleware()]);
$router->get('/reports/purchases/by-supplier/pdf', static function (): void { (new GenericReportController())->pdf('purchases-by-supplier'); }, [new AuthMiddleware()]);
$router->get('/reports/purchases/by-supplier/export', static function (): void { (new GenericReportController())->excel('purchases-by-supplier'); }, [new AuthMiddleware()]);
$router->get('/reports/purchases/items', static function (): void { (new GenericReportController())->show('purchase-items'); }, [new AuthMiddleware()]);
$router->get('/reports/purchases/items/print', static function (): void { (new GenericReportController())->print('purchase-items'); }, [new AuthMiddleware()]);
$router->get('/reports/purchases/items/pdf', static function (): void { (new GenericReportController())->pdf('purchase-items'); }, [new AuthMiddleware()]);
$router->get('/reports/purchases/items/export', static function (): void { (new GenericReportController())->excel('purchase-items'); }, [new AuthMiddleware()]);
$router->get('/reports/purchases/supplier-stock', static function (): void { (new InventoryReportController())->supplierStock(); }, [new AuthMiddleware()]);
$router->get('/reports/purchases/supplier-stock/print', static function (): void { (new InventoryReportController())->printSupplierStock(); }, [new AuthMiddleware()]);
$router->get('/reports/purchases/supplier-stock/pdf', static function (): void { (new InventoryReportController())->exportSupplierStockPdf(); }, [new AuthMiddleware()]);
$router->get('/reports/purchases/supplier-stock/export', static function (): void { (new InventoryReportController())->exportSupplierStockExcel(); }, [new AuthMiddleware()]);

$router->get('/reports/finance/expenses', static function (): void { \App\Core\Response::redirect('/reports/generic/expenses-period' . (empty($_GET) ? '' : '?' . http_build_query($_GET))); }, [new AuthMiddleware()]);
$router->get('/reports/finance/expenses/print', static function (): void { (new GenericReportController())->print('expenses-period'); }, [new AuthMiddleware()]);
$router->get('/reports/finance/expenses/pdf', static function (): void { (new GenericReportController())->pdf('expenses-period'); }, [new AuthMiddleware()]);
$router->get('/reports/finance/expenses/export', static function (): void { (new GenericReportController())->excel('expenses-period'); }, [new AuthMiddleware()]);
$router->get('/reports/finance/profit', static function (): void { \App\Core\Response::redirect('/reports/generic/gross-profit' . (empty($_GET) ? '' : '?' . http_build_query($_GET))); }, [new AuthMiddleware()]);
$router->get('/reports/finance/profit/print', static function (): void { (new GenericReportController())->print('gross-profit'); }, [new AuthMiddleware()]);
$router->get('/reports/finance/profit/pdf', static function (): void { (new GenericReportController())->pdf('gross-profit'); }, [new AuthMiddleware()]);
$router->get('/reports/finance/profit/export', static function (): void { (new GenericReportController())->excel('gross-profit'); }, [new AuthMiddleware()]);
$router->get('/reports/finance/bank-balances', static function (): void { \App\Core\Response::redirect('/reports/generic/bank-balances' . (empty($_GET) ? '' : '?' . http_build_query($_GET))); }, [new AuthMiddleware()]);
$router->get('/reports/finance/bank-balances/print', static function (): void { (new GenericReportController())->print('bank-balances'); }, [new AuthMiddleware()]);
$router->get('/reports/finance/bank-balances/pdf', static function (): void { (new GenericReportController())->pdf('bank-balances'); }, [new AuthMiddleware()]);
$router->get('/reports/finance/bank-balances/export', static function (): void { (new GenericReportController())->excel('bank-balances'); }, [new AuthMiddleware()]);
$router->get('/reports/finance/bank-statement', static function (): void { \App\Core\Response::redirect('/reports/generic/bank-statement' . (empty($_GET) ? '' : '?' . http_build_query($_GET))); }, [new AuthMiddleware()]);
$router->get('/reports/finance/bank-statement/print', static function (): void { (new GenericReportController())->print('bank-statement'); }, [new AuthMiddleware()]);
$router->get('/reports/finance/bank-statement/pdf', static function (): void { (new GenericReportController())->pdf('bank-statement'); }, [new AuthMiddleware()]);
$router->get('/reports/finance/bank-statement/export', static function (): void { (new GenericReportController())->excel('bank-statement'); }, [new AuthMiddleware()]);


foreach (ReportCatalog::all() as $reportDefinition) {
    $slug = (string)($reportDefinition['slug'] ?? '');
    $url = (string)($reportDefinition['url'] ?? '');
    if ($slug === '' || $url !== '/reports/generic/' . $slug) {
        continue;
    }
    $router->get('/reports/generic/' . $slug, static function () use ($slug): void { (new GenericReportController())->show($slug); }, [new AuthMiddleware()]);
    $router->get('/reports/generic/' . $slug . '/print', static function () use ($slug): void { (new GenericReportController())->print($slug); }, [new AuthMiddleware()]);
    $router->get('/reports/generic/' . $slug . '/pdf', static function () use ($slug): void { (new GenericReportController())->pdf($slug); }, [new AuthMiddleware()]);
    $router->get('/reports/generic/' . $slug . '/export', static function () use ($slug): void { (new GenericReportController())->excel($slug); }, [new AuthMiddleware()]);
}

$router->get('/prints/check', static function (): void { (new DocumentPrintController())->check(); }, [new AuthMiddleware()]);
$router->get('/prints/customer', static function (): void { (new DocumentPrintController())->customer(); }, [new AuthMiddleware()]);
$router->get('/prints/supplier', static function (): void { (new DocumentPrintController())->supplier(); }, [new AuthMiddleware()]);
$router->get('/prints/sales-invoice', static function (): void { (new DocumentPrintController())->salesInvoice(); }, [new AuthMiddleware()]);
$router->get('/prints/purchase-invoice', static function (): void { (new DocumentPrintController())->purchaseInvoice(); }, [new AuthMiddleware()]);
$router->get('/prints/purchase-return', static function (): void { (new DocumentPrintController())->purchaseReturn(); }, [new AuthMiddleware()]);
$router->get('/prints/sales-return', static function (): void { (new DocumentPrintController())->salesReturn(); }, [new AuthMiddleware()]);
$router->get('/prints/customer-receipt', static function (): void { (new DocumentPrintController())->customerReceipt(); }, [new AuthMiddleware()]);
$router->get('/prints/cash-payment', static function (): void { (new DocumentPrintController())->cashPayment(); }, [new AuthMiddleware()]);
$router->get('/prints/supplier-payment', static function (): void { (new DocumentPrintController())->supplierPayment(); }, [new AuthMiddleware()]);
$router->get('/prints/quotation', static function (): void { (new DocumentPrintController())->quotation(); }, [new AuthMiddleware()]);
$router->get('/quotations/pdf', static function (): void { (new PublicQuotationPdfController())->handle(); });
