<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Auth\RegisterController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\BankAccountController;
use App\Http\Controllers\DigitalPaymentController;
use App\Http\Controllers\TransactionController;
use App\Http\Controllers\ExpenseController;
use App\Http\Controllers\ExpenseTypeController;
use App\Http\Controllers\ProductController;
use App\Http\Controllers\InvoiceController;
use App\Http\Controllers\SalesDashboardController;
use App\Http\Controllers\ReportController;
use App\Http\Controllers\AdminController;
use App\Http\Controllers\SegmentController;
use App\Http\Controllers\ParticularController;
use App\Http\Controllers\ActivityLogController;
use App\Http\Controllers\QuotationController;
use App\Http\Controllers\AccountsController;
use App\Http\Controllers\PasswordController;
use App\Http\Controllers\ServiceController;

// Guest routes
Route::middleware('guest')->group(function () {
    Route::get('/login', [LoginController::class, 'showLoginForm'])->name('login');
    Route::post('/login', [LoginController::class, 'login']);
    // OTP verification routes
    Route::get('/otp', [\App\Http\Controllers\Auth\OtpController::class, 'showVerifyForm'])->name('otp.verify.form');
    Route::post('/otp', [\App\Http\Controllers\Auth\OtpController::class, 'verify'])->name('otp.verify');
    Route::post('/otp/resend', [\App\Http\Controllers\Auth\OtpController::class, 'resend'])->name('otp.resend');
    Route::get('/register', [RegisterController::class, 'showRegistrationForm'])->name('register');
    Route::post('/register', [RegisterController::class, 'register']);
});

// Authenticated routes
Route::middleware('auth')->group(function () {
    Route::post('/logout', [LoginController::class, 'logout'])->name('logout');

    // Dashboard
    Route::get('/', [DashboardController::class, 'index'])->name('home');
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
    Route::get('/dashboard/print', [DashboardController::class, 'print'])->name('dashboard.print');
    // settings update endpoint (super admin only)
    Route::post('/dashboard/settings', [DashboardController::class, 'updateSettings'])->name('dashboard.settings.update');
    // serve logo file without storage link
    Route::get('/dashboard/logo/{filename}', [DashboardController::class, 'logo'])->name('dashboard.logo');

    // Bank Accounts
    Route::resource('bank-accounts', BankAccountController::class);

    // Digital Payments
    Route::resource('digital-payments', DigitalPaymentController::class);

    // Cash management
    Route::get('/cash', [TransactionController::class, 'cashIndex'])->name('cash.index');

    // All Accounts (aggregated view)
    Route::get('/accounts', [AccountsController::class, 'index'])->name('accounts.all');
    // Transactions
    Route::get('transactions/print', [TransactionController::class, 'print'])->name('transactions.print');
    Route::resource('transactions', TransactionController::class);

    // Expenses
    Route::resource('expenses', ExpenseController::class);

    // Expense Types
    Route::resource('expense-types', ExpenseTypeController::class);

    // Particulars
    Route::resource('particulars', ParticularController::class);

    // Products
    Route::get('/products/api/get-stocks', [ProductController::class, 'getStocks'])->name('products.get-stocks');
    Route::resource('products', ProductController::class);

    // Services
    Route::get('services/print', [ServiceController::class, 'print'])->name('services.print');
    Route::resource('services', ServiceController::class);

    // Service Sales (separate from Sales Dashboard)
    Route::get('/service-sales', [App\Http\Controllers\ServiceSalesController::class, 'index'])->name('service-sales.index');
    Route::get('/service-sales/create', [App\Http\Controllers\ServiceSalesController::class, 'create'])->name('service-sales.create');
    Route::post('/service-sales', [App\Http\Controllers\ServiceSalesController::class, 'store'])->name('service-sales.store');
    Route::get('/service-sales/{serviceSale}/edit', [App\Http\Controllers\ServiceSalesController::class, 'edit'])->name('service-sales.edit');
    Route::put('/service-sales/{serviceSale}', [App\Http\Controllers\ServiceSalesController::class, 'update'])->name('service-sales.update');
    Route::delete('/service-sales/{serviceSale}', [App\Http\Controllers\ServiceSalesController::class, 'destroy'])->name('service-sales.destroy');

    // Customers
    Route::post('/customers/quick-store', [\App\Http\Controllers\CustomerController::class, 'quickStore'])->name('customers.quick-store');
    Route::get('customers/print', [\App\Http\Controllers\CustomerController::class, 'print'])->name('customers.print');
    // dedicated route for printing a single customer (must appear before the resource so it doesn't get overwritten)
    Route::get('customers/{customer}/print', [\App\Http\Controllers\CustomerController::class, 'printSingle'])->name('customers.printSingle');
    Route::resource('customers', \App\Http\Controllers\CustomerController::class);

    // Stock Management
    Route::get('/stock', [ProductController::class, 'stockIndex'])->name('stock.index');
    Route::post('/stock/adjust', [ProductController::class, 'adjustStock'])->name('stock.adjust');

    // Invoices
    Route::resource('invoices', InvoiceController::class);
    // Sales Dashboard
    Route::resource('sales-dashboard', SalesDashboardController::class);

    // Quotations / proposals
    // Define specific routes before the resource to avoid wildcard collisions.
    Route::get('quotations/sales', [QuotationController::class,'sales'])->name('quotations.sales');
    Route::get('quotations/print', [QuotationController::class, 'print'])->name('quotations.print');
    Route::patch('quotations/{quotation}/status', [QuotationController::class,'updateStatus'])->name('quotations.updateStatus');
    Route::resource('quotations', QuotationController::class);

    // Segments
    Route::resource('segments', SegmentController::class);
    Route::get('/invoices/{invoice}/print', [InvoiceController::class, 'print'])->name('invoices.print');

    // Reports
    Route::prefix('reports')->name('reports.')->group(function () {
        Route::get('/profit-loss', [ReportController::class, 'profitLoss'])->name('profit-loss');
        Route::get('/profit-loss/print', [ReportController::class, 'profitLossPrint'])->name('profit-loss-print');
        Route::get('/balance-sheet', [ReportController::class, 'balanceSheet'])->name('balance-sheet');
        Route::get('/balance-sheet/print', [ReportController::class, 'balanceSheetPrint'])->name('balance-sheet-print');
        Route::get('/cash-book', [ReportController::class, 'cashBook'])->name('cash-book');
        Route::get('/cash-book/print', [ReportController::class, 'cashBookPrint'])->name('cash-book-print');
        Route::get('/bank-book', [ReportController::class, 'bankBook'])->name('bank-book');
        Route::get('/bank-book/print', [ReportController::class, 'bankBookPrint'])->name('bank-book-print');
        Route::get('/sales', [ReportController::class, 'salesReport'])->name('sales');
        Route::get('/sales/print', [ReportController::class, 'salesPrint'])->name('sales-print');
        Route::get('/receivable-payable', [ReportController::class, 'receivablePayable'])->name('receivable-payable');
    });

    // Admin routes
    Route::middleware('admin')->prefix('admin')->name('admin.')->group(function () {
        Route::get('/dashboard', [AdminController::class, 'dashboard'])->name('dashboard');
        Route::get('/users', [AdminController::class, 'users'])->name('users');
        Route::get('/users/{user}', [AdminController::class, 'userDetails'])->name('user-details');
        Route::get('/reports', [AdminController::class, 'reports'])->name('reports');
    });

    // Super Admin routes
    Route::middleware('super_admin')->prefix('super-admin')->name('super-admin.')->group(function () {
        Route::get('/dashboard', [App\Http\Controllers\SuperAdminController::class, 'dashboard'])->name('dashboard');
        Route::get('/manage-admins', [App\Http\Controllers\SuperAdminController::class, 'manageAdmins'])->name('admins');
        Route::post('/create-admin', [App\Http\Controllers\SuperAdminController::class, 'storeAdmin'])->name('store-admin');
        Route::get('/create-admin', [App\Http\Controllers\SuperAdminController::class, 'createAdmin'])->name('create-admin');
        Route::get('/edit-permissions/{user}', [App\Http\Controllers\SuperAdminController::class, 'editAdminPermissions'])->name('edit-permissions');
        Route::put('/update-permissions/{user}', [App\Http\Controllers\SuperAdminController::class, 'updateAdminPermissions'])->name('update-permissions');
        Route::delete('/delete-admin/{user}', [App\Http\Controllers\SuperAdminController::class, 'deleteAdmin'])->name('delete-admin');
        Route::get('/permissions', [App\Http\Controllers\SuperAdminController::class, 'permissions'])->name('permissions');
        Route::get('/statistics', [App\Http\Controllers\SuperAdminController::class, 'statistics'])->name('statistics');
        Route::get('/statistics/print', [App\Http\Controllers\SuperAdminController::class, 'statisticsPrint'])->name('statistics-print');

        // Activity Log Routes
        Route::prefix('activity-logs')->name('activity-logs.')->group(function () {
            Route::get('/', [ActivityLogController::class, 'index'])->name('index');
            Route::get('/transactions', [ActivityLogController::class, 'transactions'])->name('transactions');
            Route::get('/admin/{admin}', [ActivityLogController::class, 'adminActivity'])->name('admin-activity');
            Route::get('/export', [ActivityLogController::class, 'export'])->name('export');
        });
    });

    // Profile & Settings (simple placeholders)
    Route::get('/profile', function () {
        return view('profile');
    })->name('profile');

    Route::put('/profile/update', [PasswordController::class, 'updateProfile'])->name('profile.update');

    Route::get('/settings', function () {
        return view('settings');
    })->name('settings');

    // Password Change
    Route::get('/password/change', [PasswordController::class, 'showChangePasswordForm'])->name('password.change');
    // form submits with PUT, so route should accept PUT as well
    Route::put('/password/update', [PasswordController::class, 'updatePassword'])->name('password.update');
});

