<?php

use App\Http\Controllers\AuditLogController;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\BackupController;
use App\Http\Controllers\BillingController;
use App\Http\Controllers\CashDrawerController;
use App\Http\Controllers\CustomerController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\HealthController;
use App\Http\Controllers\InvoiceController;
use App\Http\Controllers\LanguageController;
use App\Http\Controllers\LineController;
use App\Http\Controllers\MainAccountController;
use App\Http\Controllers\OverdueController;
use App\Http\Controllers\PaymentController;
use App\Http\Controllers\PlanController;
use App\Http\Controllers\ReportController;
use App\Http\Controllers\RoleController;
use App\Http\Controllers\SaleController;
use App\Http\Controllers\SearchController;
use App\Http\Controllers\SettingController;
use App\Http\Controllers\SubAccountController;
use App\Http\Controllers\SystemUpdateController;
use App\Http\Controllers\TelecomCompanyController;
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route;

Route::get('language/{locale}', [LanguageController::class, 'switch'])->name('language.switch');

Route::middleware('guest')->group(function () {
    Route::get('login', [LoginController::class, 'show'])->name('login');
    Route::post('login', [LoginController::class, 'login'])->middleware('throttle:login');
});

Route::post('logout', [LoginController::class, 'logout'])->name('logout')->middleware('auth');

Route::middleware('auth')->group(function () {
    Route::get('/', fn () => redirect()->route('dashboard'));
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
    Route::get('/search', [SearchController::class, 'index'])->name('search');

    Route::resource('customers', CustomerController::class);
    Route::resource('companies', TelecomCompanyController::class)->parameters(['companies' => 'company']);
    Route::resource('main-accounts', MainAccountController::class);
    Route::resource('sub-accounts', SubAccountController::class);
    Route::resource('lines', LineController::class);
    Route::post('lines/{line}/transfer', [LineController::class, 'transfer'])->name('lines.transfer');
    Route::post('lines/{line}/return', [LineController::class, 'returnLine'])->name('lines.return');
    Route::resource('plans', PlanController::class);

    Route::get('sales/lookup/customers', [SaleController::class, 'lookupCustomers'])->name('sales.lookup.customers');
    Route::get('sales/lookup/lines', [SaleController::class, 'lookupLines'])->name('sales.lookup.lines');
    Route::get('sales/lookup/plans', [SaleController::class, 'lookupPlans'])->name('sales.lookup.plans');
    Route::resource('sales', SaleController::class)->only(['index', 'create', 'store', 'show']);

    Route::get('billing', [BillingController::class, 'index'])->name('billing.index');
    Route::get('billing/lines/{line}', [BillingController::class, 'show'])->name('billing.show');
    Route::post('billing/collect', [BillingController::class, 'collect'])->name('billing.collect');

    Route::resource('invoices', InvoiceController::class)->only(['index', 'show', 'edit', 'update', 'destroy']);
    Route::get('invoices/{invoice}/print', [InvoiceController::class, 'print'])->name('invoices.print');
    Route::get('invoices/{invoice}/thermal', [InvoiceController::class, 'thermal'])->name('invoices.thermal');
    Route::get('invoices/{invoice}/pdf', [InvoiceController::class, 'pdf'])->name('invoices.pdf');
    Route::post('invoices/{invoice}/cancel', [InvoiceController::class, 'cancel'])->name('invoices.cancel');

    Route::resource('payments', PaymentController::class)->only(['index', 'show', 'store']);

    Route::get('cash', [CashDrawerController::class, 'index'])->name('cash.index');
    Route::post('cash/open', [CashDrawerController::class, 'open'])->name('cash.open');
    Route::post('cash/close', [CashDrawerController::class, 'close'])->name('cash.close');
    Route::post('cash/expense', [CashDrawerController::class, 'expense'])->name('cash.expense');
    Route::post('cash/move', [CashDrawerController::class, 'cashMove'])->name('cash.move');
    Route::get('cash/closings', [CashDrawerController::class, 'closings'])->name('cash.closings');

    Route::get('overdue', [OverdueController::class, 'index'])->name('overdue.index');
    Route::get('overdue/export', [OverdueController::class, 'export'])->name('overdue.export');
    Route::get('overdue/print', [OverdueController::class, 'index'])->name('overdue.print');

    Route::get('reports', [ReportController::class, 'index'])->name('reports.index');
    Route::get('reports/{type}', [ReportController::class, 'show'])->name('reports.show');
    Route::get('reports/{type}/excel', [ReportController::class, 'excel'])->name('reports.excel');
    Route::get('reports/{type}/pdf', [ReportController::class, 'pdf'])->name('reports.pdf');
    Route::get('reports/{type}/print', [ReportController::class, 'print'])->name('reports.print');

    Route::resource('users', UserController::class)->except(['show']);
    Route::get('roles', [RoleController::class, 'index'])->name('roles.index');
    Route::get('roles/{role}/edit', [RoleController::class, 'edit'])->name('roles.edit');
    Route::put('roles/{role}', [RoleController::class, 'update'])->name('roles.update');

    Route::get('audit', [AuditLogController::class, 'index'])->name('audit.index');
    Route::get('audit/{audit}', [AuditLogController::class, 'show'])->name('audit.show');

    Route::get('settings', [SettingController::class, 'index'])->name('settings.index');
    Route::put('settings', [SettingController::class, 'update'])->name('settings.update');

    Route::get('admin/system/update', [SystemUpdateController::class, 'index'])->name('system.update');
    Route::get('admin/system/update/{update}', [SystemUpdateController::class, 'show'])->name('system.update.show');
    Route::post('admin/system/update/migrate', [SystemUpdateController::class, 'migrate'])->name('system.update.migrate');
    Route::post('admin/system/update/upload', [SystemUpdateController::class, 'upload'])->name('system.update.upload');

    Route::get('admin/system/backups', [BackupController::class, 'index'])->name('system.backups');
    Route::post('admin/system/backups', [BackupController::class, 'store'])->name('system.backups.store');
    Route::get('admin/system/backups/{backup}/download', [BackupController::class, 'download'])->name('system.backups.download');
    Route::delete('admin/system/backups/{backup}', [BackupController::class, 'destroy'])->name('system.backups.destroy');

    Route::get('admin/system/health', [HealthController::class, 'index'])->name('system.health');
});
