<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\CustomerController;
use App\Http\Controllers\ProductController;
use App\Http\Controllers\SaleController;
use App\Http\Controllers\InstallmentController;
use App\Http\Controllers\PaymentController;
use App\Http\Controllers\ReportController;
use App\Http\Controllers\SettingController;
use App\Http\Controllers\Setting\UnitController;
use App\Http\Controllers\Setting\BrandController;
use App\Http\Controllers\Setting\CategoryController;
use App\Http\Controllers\Setting\ModelController;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Auth\ForgotPasswordController;
use App\Http\Controllers\Auth\ResetPasswordController;
use Illuminate\Support\Facades\Artisan;


Route::get('/fix-it', function() {
    Artisan::call('config:clear');
    Artisan::call('cache:clear');
    Artisan::call('view:clear');
    return "Cache Cleared Successfully!";
});
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
*/
Route::get('/clear-cache', function() {
    Artisan::call('config:clear');
    return "Config cache is cleared";
});

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

Route::get('/login', [LoginController::class, 'showLoginForm']);
// Authentication Routes
Route::get('/login', [LoginController::class, 'showLoginForm'])->name('login');
Route::post('/login', [LoginController::class, 'login']);
Route::post('/logout', [LoginController::class, 'logout'])->name('logout');

// Password Reset Routes
Route::get('/password/reset', [ForgotPasswordController::class, 'showLinkRequestForm'])->name('password.request');
Route::post('/password/email', [ForgotPasswordController::class, 'sendResetLinkEmail'])->name('password.email');
Route::get('/password/reset/{token}', [ResetPasswordController::class, 'showResetForm'])->name('password.reset');
Route::post('/password/reset', [ResetPasswordController::class, 'reset'])->name('password.update');

// Protected routes - require authentication
Route::middleware(['auth'])->group(function () {
    
    // ==================== DASHBOARD ====================
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
    
    // ==================== CUSTOMER MANAGEMENT ====================
    Route::resource('customers', CustomerController::class);
    Route::get('customers/{customer}/statement', [CustomerController::class, 'statement'])->name('customers.statement');
    Route::post('customers/{customer}/update-balance', [CustomerController::class, 'updateBalance'])->name('customers.update-balance');
    Route::get('api/customers/search', [CustomerController::class, 'search'])->name('api.customers.search');
    
    // ==================== PRODUCT MANAGEMENT ====================
    Route::resource('products', ProductController::class);
    Route::get('products/search', [ProductController::class, 'search'])->name('products.search');
    Route::get('products/{product}/stock', [ProductController::class, 'stock'])->name('products.stock');
    Route::get('api/products/search', [ProductController::class, 'search'])->name('api.products.search');
    Route::post('products/{product}/stock', [ProductController::class, 'updateStock'])->name('products.stock.update');
    
    // ==================== SALE MANAGEMENT ====================
    Route::resource('sales', SaleController::class);
    Route::get('sales/{sale}/invoice', [SaleController::class, 'invoice'])->name('sales.invoice');
    Route::get('sales/{sale}/print', [SaleController::class, 'printInvoice'])->name('sales.print');
    Route::get('api/sales/{sale}/installments', [InstallmentController::class, 'getBySale'])->name('api.sales.installments');
    
    // ==================== INSTALLMENT MANAGEMENT ====================
    Route::resource('installments', InstallmentController::class);
    Route::get('installments/due/today', [InstallmentController::class, 'dueToday'])->name('installments.due-today');
    Route::get('installments/overdue', [InstallmentController::class, 'overdue'])->name('installments.overdue');
    Route::post('installments/{installment}/pay', [InstallmentController::class, 'pay'])->name('installments.pay');
    Route::post('api/installments/{installment}/calculate-late-fee', [InstallmentController::class, 'calculateLateFee'])->name('api.installments.calculate-late-fee');
    
    // ==================== PAYMENT MANAGEMENT ====================
    Route::resource('payments', PaymentController::class);
    Route::get('payments/today/collection', [PaymentController::class, 'todayCollection'])->name('payments.today');
    Route::get('payments/export', [PaymentController::class, 'export'])->name('payments.export');
    
    // ==================== REPORTS ====================
    Route::prefix('reports')->name('reports.')->group(function () {
        Route::get('/', [ReportController::class, 'index'])->name('index');
        Route::get('/sales', [ReportController::class, 'sales'])->name('sales');
        Route::get('/installments', [ReportController::class, 'installments'])->name('installments');
        Route::get('/customers', [ReportController::class, 'customers'])->name('customers');
        Route::get('/products', [ReportController::class, 'products'])->name('products');
        Route::get('/payments', [ReportController::class, 'payments'])->name('payments');
        Route::get('/profit-loss', [ReportController::class, 'profitLoss'])->name('profit-loss');
        
        // Export routes
        Route::get('/export/sales', [ReportController::class, 'exportSales'])->name('export.sales');
        Route::get('/export/payments', [ReportController::class, 'exportPayments'])->name('export.payments');
        Route::get('/export/customers', [ReportController::class, 'exportCustomers'])->name('export.customers');
        Route::get('/export/products', [ReportController::class, 'exportProducts'])->name('export.products');
        Route::get('/export/{type}', [ReportController::class, 'export'])->name('export');
    });
    
    // ==================== SETTINGS ====================
    Route::prefix('settings')->name('settings.')->group(function () {
        
        // Main Settings Dashboard
        Route::get('/', [SettingController::class, 'index'])->name('index');
        
        // General Settings
        Route::post('/update', [SettingController::class, 'update'])->name('update');
        Route::get('/backup', [SettingController::class, 'backup'])->name('backup');
        Route::post('/restore', [SettingController::class, 'restore'])->name('restore');
        
        // Unit Management
        Route::resource('units', UnitController::class)->except(['show']);
        Route::post('units/{unit}/toggle-status', [UnitController::class, 'toggleStatus'])->name('units.toggle-status');
        
        // Brand Management
        Route::resource('brands', BrandController::class)->except(['show']);
        
        // Category Management
        Route::resource('categories', CategoryController::class)->except(['show']);
        Route::get('categories/tree', [CategoryController::class, 'tree'])->name('categories.tree');
        
        // Model Management
        Route::resource('models', ModelController::class)->except(['show']);
        Route::get('models/by-brand/{brand}', [ModelController::class, 'getByBrand'])->name('models.by-brand');
        Route::get('models/by-category/{category}', [ModelController::class, 'getByCategory'])->name('models.by-category');
    });
    
    // ==================== API ROUTES (AJAX calls) ====================
    Route::prefix('api')->name('api.')->group(function () {
        Route::get('brands/list', [BrandController::class, 'getAll'])->name('brands.list');
        Route::get('categories/list', [CategoryController::class, 'getAll'])->name('categories.list');
        Route::get('units/list', [UnitController::class, 'getAll'])->name('units.list');
    });
});