<?php

use App\Http\Controllers\ProfileController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\PosController;
use App\Http\Controllers\ProductController;
use App\Http\Controllers\CategoryController;
use App\Http\Controllers\ReportController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\SettingController;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('welcome');
});

Route::get('/dashboard', [DashboardController::class, 'index'])
    ->middleware(['auth', 'verified'])
    ->name('dashboard');

Route::middleware('auth')->group(function () {
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');

    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
    Route::get('/pos', [PosController::class, 'index'])->name('pos.index');
    Route::get('/pos/products/search', [PosController::class, 'searchProducts'])->name('pos.products.search');
    Route::get('/pos/categories', [PosController::class, 'getCategories'])->name('pos.categories');
    Route::post('/pos/search', [PosController::class, 'searchProducts'])->name('pos.search');
    Route::post('/pos/transaction', [PosController::class, 'store'])->name('pos.transaction');



    // Protected Routes (Admin or Permission based)
    Route::resource('products', ProductController::class)->middleware('permission:view_products');
    Route::get('/products/barcodes/print', [App\Http\Controllers\ProductController::class, 'printBarcodes'])->name('products.print_barcodes')->middleware('permission:view_products');
    
    Route::resource('categories', CategoryController::class)->middleware('permission:view_categories');
    
    Route::resource('suppliers', \App\Http\Controllers\SupplierController::class)->middleware('permission:view_suppliers');
    Route::resource('customers', \App\Http\Controllers\CustomerController::class)->middleware('permission:view_customers');
    
    Route::resource('purchases', \App\Http\Controllers\PurchaseController::class)->middleware('permission:view_purchases');
    Route::resource('expenses', \App\Http\Controllers\ExpenseController::class)->middleware('permission:view_expenses');
    
    Route::resource('transactions', \App\Http\Controllers\TransactionController::class)->only(['index', 'show', 'destroy'])->middleware('permission:view_transactions');
    
    Route::get('/reports', [ReportController::class, 'index'])->name('reports.index')->middleware('permission:view_reports');
    Route::get('/reports/sales', [ReportController::class, 'sales'])->name('reports.sales')->middleware('permission:view_reports');
    Route::get('/reports/products', [ReportController::class, 'products'])->name('reports.products')->middleware('permission:view_reports');
    Route::get('/reports/stock', [ReportController::class, 'stock'])->name('reports.stock')->middleware('permission:view_reports');
    Route::get('/reports/sales/export-pdf', [ReportController::class, 'exportSalesPdf'])->name('reports.sales.pdf')->middleware('permission:view_reports');
    Route::get('/reports/sales/export-excel', [ReportController::class, 'exportSalesExcel'])->name('reports.sales.excel')->middleware('permission:view_reports');
    Route::get('/reports/products/export-pdf', [ReportController::class, 'exportProductsPdf'])->name('reports.products.pdf')->middleware('permission:view_reports');
    Route::get('/reports/products/export-excel', [ReportController::class, 'exportProductsExcel'])->name('reports.products.excel')->middleware('permission:view_reports');
    Route::get('/reports/stock/export-pdf', [ReportController::class, 'exportStockPdf'])->name('reports.stock.pdf')->middleware('permission:view_reports');
    Route::get('/reports/stock/export-excel', [ReportController::class, 'exportStockExcel'])->name('reports.stock.excel')->middleware('permission:view_reports');
    
    Route::get('/reports/receivables', [ReportController::class, 'receivables'])->name('reports.receivables')->middleware('permission:view_receivables');
    Route::post('/reports/receivables/{transaction}/paid', [ReportController::class, 'markAsPaid'])->name('reports.receivables.paid')->middleware('permission:view_receivables');

    // Admin Only
    Route::middleware('admin')->group(function () {
        Route::resource('users', UserController::class);
        Route::get('/settings', [SettingController::class, 'index'])->name('settings.index');
        Route::put('/settings', [SettingController::class, 'update'])->name('settings.update');
    });
});

require __DIR__.'/auth.php';
