<?php

use App\Http\Controllers\POSController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\ProductController;
use App\Http\Controllers\SaleController;
use App\Http\Controllers\CustomerController;
use App\Http\Controllers\SupplierController;
use App\Http\Controllers\PurchaseController;
use App\Http\Controllers\ExpenseController;
use App\Http\Controllers\ReportController;
use App\Http\Controllers\CategoryController;
use App\Http\Controllers\TaxController;
use App\Http\Controllers\ProfileController;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return redirect()->route('dashboard');
});

Auth::routes();

Route::middleware(['auth'])->group(function () {
    // Dashboard
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
    
    // Profile Routes
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::put('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::put('/profile/password', [ProfileController::class, 'updatePassword'])->name('profile.password');
    Route::delete('/profile/avatar', [ProfileController::class, 'destroyAvatar'])->name('profile.avatar.destroy');
    
    // Categories Routes
    Route::resource('categories', CategoryController::class);
    Route::put('/categories/{category}/toggle', [CategoryController::class, 'toggleStatus'])->name('categories.toggle');
    
    // Tax Routes
    Route::resource('taxes', TaxController::class);
    Route::put('/taxes/{tax}/toggle', [TaxController::class, 'toggleStatus'])->name('taxes.toggle');
    Route::put('/taxes/{tax}/set-default', [TaxController::class, 'setDefault'])->name('taxes.set-default');
    
    // POS Routes
    Route::prefix('pos')->name('pos.')->group(function () {
        Route::get('/', [POSController::class, 'index'])->name('index');
        Route::get('/search-by-barcode', [POSController::class, 'searchByBarcode'])->name('search-by-barcode');
        Route::post('/add-to-cart', [POSController::class, 'addToCart'])->name('add-to-cart');
        Route::post('/update-cart', [POSController::class, 'updateCart'])->name('update-cart');
        Route::delete('/remove-from-cart/{productId}', [POSController::class, 'removeFromCart'])->name('remove-from-cart');
        Route::post('/clear-cart', [POSController::class, 'clearCart'])->name('clear-cart');
        Route::post('/process-sale', [POSController::class, 'processSale'])->name('process-sale');
        Route::get('/get-cart', [POSController::class, 'getCart'])->name('get-cart');
        Route::get('/search-product', [POSController::class, 'searchProduct'])->name('search-product');
        Route::post('/update-tax', [POSController::class, 'updateTax'])->name('update-tax');
    });
    
    // Products Routes
    Route::resource('products', ProductController::class);
        Route::get('/products/{product}', [ProductController::class, 'show'])->name('products.show');
        Route::get('/products/{product}/print-barcode', [ProductController::class, 'printBarcode'])->name('products.print-barcode');    
    // Sales Routes
    Route::resource('sales', SaleController::class);
        Route::get('/sales/invoice/{sale}', [SaleController::class, 'invoice'])->name('sales.invoice');
    
    // Customers Routes
    Route::resource('customers', CustomerController::class);
        Route::put('/customers/{customer}/toggle', [CustomerController::class, 'toggleStatus'])->name('customers.toggle');
        Route::post('/customers/{customer}/add-points', [CustomerController::class, 'addPoints'])->name('customers.add-points');
        Route::post('/customers/{customer}/redeem-points', [CustomerController::class, 'redeemPoints'])->name('customers.redeem-points');
        Route::get('/customers/export', [CustomerController::class, 'export'])->name('customers.export');
    
    // Suppliers Routes
    Route::resource('suppliers', SupplierController::class);
        Route::put('/suppliers/{supplier}/toggle', [SupplierController::class, 'toggleStatus'])->name('suppliers.toggle');
        Route::get('/suppliers/export', [SupplierController::class, 'export'])->name('suppliers.export');
    
    // Purchase routes
    Route::patch('/purchases/{purchase}/complete', [PurchaseController::class, 'complete'])->name('purchases.complete');
        Route::patch('/purchases/{purchase}/cancel', [PurchaseController::class, 'cancel'])->name('purchases.cancel');
        Route::get('/purchases/filter', [PurchaseController::class, 'filter'])->name('purchases.filter');
        Route::get('/purchases/export', [PurchaseController::class, 'export'])->name('purchases.export');
        Route::resource('purchases', PurchaseController::class);
    
    // Expenses Routes
    Route::resource('expenses', ExpenseController::class);
        Route::put('/expenses/{expense}/approve', [ExpenseController::class, 'approve'])->name('expenses.approve');
        Route::put('/expenses/{expense}/reject', [ExpenseController::class, 'reject'])->name('expenses.reject');
    
    // Reports Routes
    Route::prefix('reports')->name('reports.')->group(function () {
        Route::get('/', [ReportController::class, 'index'])->name('index');
        Route::get('/sales', [ReportController::class, 'sales'])->name('sales');
        Route::get('/purchases', [ReportController::class, 'purchases'])->name('purchases');
        Route::get('/expenses', [ReportController::class, 'expenses'])->name('expenses');
        Route::get('/profit-loss', [ReportController::class, 'profitLoss'])->name('profit-loss');
        Route::get('/stock', [ReportController::class, 'stock'])->name('stock');
        Route::get('/tax', [ReportController::class, 'tax'])->name('tax');
        Route::get('/export/{type}', [ReportController::class, 'exportPDF'])->name('export');
    });
});