<?php

use App\Http\Controllers\Admin\CustomerController;
use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\ExpenseController;
use App\Http\Controllers\Admin\InvoiceController;
use App\Http\Controllers\Admin\ItemController;
use App\Http\Controllers\Admin\PurchaseBillController;
use App\Http\Controllers\Admin\QuotationController;
use App\Http\Controllers\Admin\PreferenceController;
use App\Http\Controllers\Admin\Gstr1ReportController;
use App\Http\Controllers\Admin\Gstr3bReportController;
use App\Http\Controllers\Admin\AccountingController;
use App\Http\Controllers\Admin\InventoryReportController;
use App\Http\Controllers\Admin\ReportController;
use App\Http\Controllers\Admin\UnitController;
use App\Http\Controllers\Admin\CreditNoteDocumentPreferenceController;
use App\Http\Controllers\Admin\DebitNoteDocumentPreferenceController;
use App\Http\Controllers\Admin\InvoiceDocumentPreferenceController;
use App\Http\Controllers\Admin\PurchaseBillDocumentPreferenceController;
use App\Http\Controllers\Admin\ExpenseDocumentPreferenceController;
use App\Http\Controllers\Admin\PurchaseOrderDocumentPreferenceController;
use App\Http\Controllers\Admin\QuotationDocumentPreferenceController;
use App\Http\Controllers\Admin\TaxController;
use App\Http\Controllers\Admin\VendorController;
use App\Http\Controllers\Auth\AuthController;
use App\Http\Controllers\PublicInvoiceController;
use App\Http\Controllers\PublicQuotationController;
use App\Http\Controllers\PublicDeliveryChallanController;
use App\Http\Controllers\PublicCreditNoteController;
use App\Http\Controllers\PublicDebitNoteController;
use App\Http\Controllers\PublicPurchaseOrderController;
use App\Http\Controllers\PublicPurchaseBillController;
use App\Http\Controllers\PublicExpenseController;
use App\Http\Controllers\PublicPaymentController;
use App\Http\Controllers\Admin\PaymentController;
use App\Http\Controllers\Admin\PaymentDocumentPreferenceController;
use App\Http\Controllers\Admin\DebitNoteController;
use App\Http\Controllers\Admin\PurchaseOrderController;
use App\Http\Controllers\Admin\DeliveryChallanController;
use App\Http\Controllers\Admin\DeliveryChallanDocumentPreferenceController;
use App\Http\Controllers\Admin\CreditNoteController;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return auth()->check()
        ? redirect()->route('admin.dashboard')
        : redirect()->route('admin.login');
});

Route::prefix('documents')->name('public.invoices.')->group(function () {
    Route::get('{token}', [PublicInvoiceController::class, 'show'])->name('show');
    Route::get('{token}/pdf', [PublicInvoiceController::class, 'pdf'])->name('pdf');
    Route::get('{token}/print', [PublicInvoiceController::class, 'print'])->name('print');
});

Route::prefix('quote-documents')->name('public.quotations.')->group(function () {
    Route::get('{token}', [PublicQuotationController::class, 'show'])->name('show');
    Route::get('{token}/pdf', [PublicQuotationController::class, 'pdf'])->name('pdf');
    Route::get('{token}/print', [PublicQuotationController::class, 'print'])->name('print');
});

Route::prefix('challan-documents')->name('public.delivery-challans.')->group(function () {
    Route::get('{token}', [PublicDeliveryChallanController::class, 'show'])->name('show');
    Route::get('{token}/pdf', [PublicDeliveryChallanController::class, 'pdf'])->name('pdf');
    Route::get('{token}/print', [PublicDeliveryChallanController::class, 'print'])->name('print');
});

Route::prefix('credit-note-documents')->name('public.credit-notes.')->group(function () {
    Route::get('{token}', [PublicCreditNoteController::class, 'show'])->name('show');
    Route::get('{token}/pdf', [PublicCreditNoteController::class, 'pdf'])->name('pdf');
    Route::get('{token}/print', [PublicCreditNoteController::class, 'print'])->name('print');
});

Route::prefix('debit-note-documents')->name('public.debit-notes.')->group(function () {
    Route::get('{token}', [PublicDebitNoteController::class, 'show'])->name('show');
    Route::get('{token}/pdf', [PublicDebitNoteController::class, 'pdf'])->name('pdf');
    Route::get('{token}/print', [PublicDebitNoteController::class, 'print'])->name('print');
});

Route::prefix('purchase-order-documents')->name('public.purchase-orders.')->group(function () {
    Route::get('{token}', [PublicPurchaseOrderController::class, 'show'])->name('show');
    Route::get('{token}/pdf', [PublicPurchaseOrderController::class, 'pdf'])->name('pdf');
    Route::get('{token}/print', [PublicPurchaseOrderController::class, 'print'])->name('print');
});

Route::prefix('purchase-bill-documents')->name('public.purchase-bills.')->group(function () {
    Route::get('{token}', [PublicPurchaseBillController::class, 'show'])->name('show');
    Route::get('{token}/pdf', [PublicPurchaseBillController::class, 'pdf'])->name('pdf');
    Route::get('{token}/print', [PublicPurchaseBillController::class, 'print'])->name('print');
});

Route::prefix('expense-documents')->name('public.expenses.')->group(function () {
    Route::get('{token}', [PublicExpenseController::class, 'show'])->name('show');
    Route::get('{token}/pdf', [PublicExpenseController::class, 'pdf'])->name('pdf');
    Route::get('{token}/print', [PublicExpenseController::class, 'print'])->name('print');
});

Route::prefix('payment-documents')->name('public.payments.')->group(function () {
    Route::get('{token}', [PublicPaymentController::class, 'show'])->name('show');
    Route::get('{token}/pdf', [PublicPaymentController::class, 'pdf'])->name('pdf');
    Route::get('{token}/print', [PublicPaymentController::class, 'print'])->name('print');
});

Route::prefix('admin')->name('admin.')->group(function () {
    Route::middleware('guest')->group(function () {
        Route::get('/', [AuthController::class, 'showLogin']);
        Route::get('/login', [AuthController::class, 'showLogin'])->name('login');
        Route::post('/login', [AuthController::class, 'login'])->name('login.submit');
    });

    Route::middleware('auth')->group(function () {
        Route::post('/logout', [AuthController::class, 'logout'])->name('logout');
        Route::get('/', fn () => redirect()->route('admin.dashboard'));
        Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');

        Route::get('customers/export', [CustomerController::class, 'export'])->name('customers.export');
        Route::post('customers/quick', [CustomerController::class, 'quickStore'])->name('customers.quick-store');
        Route::resource('customers', CustomerController::class)->except(['show']);
        Route::get('vendors/export', [VendorController::class, 'export'])->name('vendors.export');
        Route::resource('vendors', VendorController::class)->except(['show']);
        Route::resource('items', ItemController::class)->except(['show']);
        Route::post('units', [UnitController::class, 'store'])->name('units.store');
        Route::get('invoices/export', [InvoiceController::class, 'export'])->name('invoices.export');
        Route::get('invoices/{invoice}/print', [InvoiceController::class, 'print'])->name('invoices.print');
        Route::get('invoices/{invoice}/pdf', [InvoiceController::class, 'downloadPdf'])->name('invoices.pdf');
        Route::post('invoices/{invoice}/share-link', [InvoiceController::class, 'shareLink'])->name('invoices.share-link');
        Route::post('units/custom', [InvoiceController::class, 'storeCustomUnit'])->name('units.custom.store');
        Route::resource('invoices', InvoiceController::class);
        Route::get('purchase-bills/export', [PurchaseBillController::class, 'export'])->name('purchase-bills.export');
        Route::get('purchase-bills/{purchase_bill}/print', [PurchaseBillController::class, 'print'])->name('purchase-bills.print');
        Route::get('purchase-bills/{purchase_bill}/pdf', [PurchaseBillController::class, 'downloadPdf'])->name('purchase-bills.pdf');
        Route::post('purchase-bills/{purchase_bill}/share-link', [PurchaseBillController::class, 'shareLink'])->name('purchase-bills.share-link');
        Route::resource('purchase-bills', PurchaseBillController::class)->except(['show']);
        Route::post('expenses/categories', [ExpenseController::class, 'storeCategory'])->name('expenses.categories.store');
        Route::get('expenses/export', [ExpenseController::class, 'export'])->name('expenses.export');
        Route::get('expenses/{expense}/print', [ExpenseController::class, 'print'])->name('expenses.print');
        Route::get('expenses/{expense}/pdf', [ExpenseController::class, 'downloadPdf'])->name('expenses.pdf');
        Route::post('expenses/{expense}/share-link', [ExpenseController::class, 'shareLink'])->name('expenses.share-link');
        Route::resource('expenses', ExpenseController::class)->except(['show']);
        Route::get('payments/unpaid-documents', [PaymentController::class, 'unpaidDocuments'])->name('payments.unpaid-documents');
        Route::get('payments/export', [PaymentController::class, 'export'])->name('payments.export');
        Route::get('payments/{payment}/print', [PaymentController::class, 'print'])->name('payments.print');
        Route::get('payments/{payment}/pdf', [PaymentController::class, 'downloadPdf'])->name('payments.pdf');
        Route::post('payments/{payment}/share-link', [PaymentController::class, 'shareLink'])->name('payments.share-link');
        Route::resource('payments', PaymentController::class)->except(['show']);
        Route::get('quotations/export', [QuotationController::class, 'export'])->name('quotations.export');
        Route::get('quotations/{quotation}/print', [QuotationController::class, 'print'])->name('quotations.print');
        Route::get('quotations/{quotation}/pdf', [QuotationController::class, 'downloadPdf'])->name('quotations.pdf');
        Route::post('quotations/{quotation}/share-link', [QuotationController::class, 'shareLink'])->name('quotations.share-link');
        Route::resource('quotations', QuotationController::class)->except(['show']);
        Route::get('delivery-challans/export', [DeliveryChallanController::class, 'export'])->name('delivery-challans.export');
        Route::get('delivery-challans/{delivery_challan}/print', [DeliveryChallanController::class, 'print'])->name('delivery-challans.print');
        Route::get('delivery-challans/{delivery_challan}/pdf', [DeliveryChallanController::class, 'downloadPdf'])->name('delivery-challans.pdf');
        Route::post('delivery-challans/{delivery_challan}/share-link', [DeliveryChallanController::class, 'shareLink'])->name('delivery-challans.share-link');
        Route::resource('delivery-challans', DeliveryChallanController::class)->except(['show']);
        Route::get('credit-notes/invoices-json', [CreditNoteController::class, 'invoicesJson'])->name('credit-notes.invoices-json');
        Route::get('credit-notes/export', [CreditNoteController::class, 'export'])->name('credit-notes.export');
        Route::get('credit-notes/{credit_note}/print', [CreditNoteController::class, 'print'])->name('credit-notes.print');
        Route::get('credit-notes/{credit_note}/pdf', [CreditNoteController::class, 'downloadPdf'])->name('credit-notes.pdf');
        Route::post('credit-notes/{credit_note}/share-link', [CreditNoteController::class, 'shareLink'])->name('credit-notes.share-link');
        Route::resource('credit-notes', CreditNoteController::class)->except(['show']);
        Route::get('debit-notes/invoices-json', [DebitNoteController::class, 'invoicesJson'])->name('debit-notes.invoices-json');
        Route::get('debit-notes/purchase-bills-json', [DebitNoteController::class, 'purchaseBillsJson'])->name('debit-notes.purchase-bills-json');
        Route::get('debit-notes/export', [DebitNoteController::class, 'export'])->name('debit-notes.export');
        Route::get('debit-notes/{debit_note}/print', [DebitNoteController::class, 'print'])->name('debit-notes.print');
        Route::get('debit-notes/{debit_note}/pdf', [DebitNoteController::class, 'downloadPdf'])->name('debit-notes.pdf');
        Route::post('debit-notes/{debit_note}/share-link', [DebitNoteController::class, 'shareLink'])->name('debit-notes.share-link');
        Route::resource('debit-notes', DebitNoteController::class)->except(['show']);
        Route::get('purchase-orders/export', [PurchaseOrderController::class, 'export'])->name('purchase-orders.export');
        Route::get('purchase-orders/{purchase_order}/print', [PurchaseOrderController::class, 'print'])->name('purchase-orders.print');
        Route::get('purchase-orders/{purchase_order}/pdf', [PurchaseOrderController::class, 'downloadPdf'])->name('purchase-orders.pdf');
        Route::post('purchase-orders/{purchase_order}/share-link', [PurchaseOrderController::class, 'shareLink'])->name('purchase-orders.share-link');
        Route::resource('purchase-orders', PurchaseOrderController::class)->except(['show']);
        Route::get('/reports', [ReportController::class, 'index'])->name('reports');
        Route::get('/reports/gstr1', [Gstr1ReportController::class, 'index'])->name('reports.gstr1');
        Route::get('/reports/gstr1/export/xls', [Gstr1ReportController::class, 'exportXls'])->name('reports.gstr1.export.xls');
        Route::get('/reports/gstr1/export/json', [Gstr1ReportController::class, 'exportJson'])->name('reports.gstr1.export.json');
        Route::get('/reports/gstr3b', [Gstr3bReportController::class, 'index'])->name('reports.gstr3b');
        Route::get('/reports/gstr3b/export', [Gstr3bReportController::class, 'export'])->name('reports.gstr3b.export');
        Route::get('/reports/tds-tcs', [ReportController::class, 'tdsTcs'])->name('reports.tds-tcs');
        Route::get('/reports/inventory/{report}', [InventoryReportController::class, 'index'])->name('reports.inventory');
        Route::get('/reports/inventory/{report}/export', [InventoryReportController::class, 'export'])->name('reports.inventory.export');

        Route::get('/accounting', [AccountingController::class, 'index'])->name('accounting.index');
        Route::get('/accounting/{report}', [AccountingController::class, 'show'])->name('accounting.show');
        Route::get('/accounting/{report}/export', [AccountingController::class, 'export'])->name('accounting.export');

        Route::get('/preferences/general', [PreferenceController::class, 'general'])->name('preferences.general');
        Route::put('/preferences/general', [PreferenceController::class, 'updateGeneral'])->name('preferences.general.update');
        Route::get('/preferences/profile', [PreferenceController::class, 'profile'])->name('preferences.profile');
        Route::put('/preferences/profile', [PreferenceController::class, 'updateProfile'])->name('preferences.profile.update');

        Route::get('/preferences/taxes', [TaxController::class, 'index'])->name('preferences.taxes');
        Route::get('/preferences/taxes/create', [TaxController::class, 'createTax'])->name('preferences.taxes.create');
        Route::post('/preferences/taxes', [TaxController::class, 'storeTax'])->name('preferences.taxes.store');
        Route::get('/preferences/taxes/groups/create', [TaxController::class, 'createGroup'])->name('preferences.taxes.groups.create');
        Route::post('/preferences/taxes/groups', [TaxController::class, 'storeGroup'])->name('preferences.taxes.groups.store');
        Route::get('/preferences/taxes/{tax}/edit', [TaxController::class, 'editTax'])->name('preferences.taxes.edit');
        Route::put('/preferences/taxes/{tax}', [TaxController::class, 'updateTax'])->name('preferences.taxes.update');
        Route::get('/preferences/taxes/groups/{tax}/edit', [TaxController::class, 'editGroup'])->name('preferences.taxes.groups.edit');
        Route::put('/preferences/taxes/groups/{tax}', [TaxController::class, 'updateGroup'])->name('preferences.taxes.groups.update');
        Route::delete('/preferences/taxes/{tax}', [TaxController::class, 'destroy'])->name('preferences.taxes.destroy');

        Route::get('/preferences/invoices', [InvoiceDocumentPreferenceController::class, 'index'])->name('preferences.invoices');
        Route::put('/preferences/invoices', [InvoiceDocumentPreferenceController::class, 'update'])->name('preferences.invoices.update');

        Route::get('/preferences/quotes', [QuotationDocumentPreferenceController::class, 'index'])->name('preferences.quotes');
        Route::put('/preferences/quotes', [QuotationDocumentPreferenceController::class, 'update'])->name('preferences.quotes.update');

        Route::get('/preferences/delivery-challans', [DeliveryChallanDocumentPreferenceController::class, 'index'])->name('preferences.delivery-challans');
        Route::put('/preferences/delivery-challans', [DeliveryChallanDocumentPreferenceController::class, 'update'])->name('preferences.delivery-challans.update');

        Route::get('/preferences/credit-notes', [CreditNoteDocumentPreferenceController::class, 'index'])->name('preferences.credit-notes');
        Route::put('/preferences/credit-notes', [CreditNoteDocumentPreferenceController::class, 'update'])->name('preferences.credit-notes.update');

        Route::get('/preferences/debit-notes', [DebitNoteDocumentPreferenceController::class, 'index'])->name('preferences.debit-notes');
        Route::put('/preferences/debit-notes', [DebitNoteDocumentPreferenceController::class, 'update'])->name('preferences.debit-notes.update');

        Route::get('/preferences/purchase-orders', [PurchaseOrderDocumentPreferenceController::class, 'index'])->name('preferences.purchase-orders');
        Route::put('/preferences/purchase-orders', [PurchaseOrderDocumentPreferenceController::class, 'update'])->name('preferences.purchase-orders.update');

        Route::get('/preferences/purchase-bills', [PurchaseBillDocumentPreferenceController::class, 'index'])->name('preferences.purchase-bills');
        Route::put('/preferences/purchase-bills', [PurchaseBillDocumentPreferenceController::class, 'update'])->name('preferences.purchase-bills.update');

        Route::get('/preferences/expenses', [ExpenseDocumentPreferenceController::class, 'index'])->name('preferences.expenses');
        Route::put('/preferences/expenses', [ExpenseDocumentPreferenceController::class, 'update'])->name('preferences.expenses.update');

        Route::get('/preferences/payments', [PaymentDocumentPreferenceController::class, 'index'])->name('preferences.payments');
        Route::put('/preferences/payments', [PaymentDocumentPreferenceController::class, 'update'])->name('preferences.payments.update');
    });
});
