<?php

use Illuminate\Support\Facades\Route;
use Modules\SystemCore\App\Http\Controllers\CustomerController;
use Modules\SystemCore\App\Http\Controllers\CustomerDocumentController;
use Modules\SystemCore\App\Http\Controllers\DashboardController;
use Modules\SystemCore\App\Http\Controllers\EntityAddressController;
use Modules\SystemCore\App\Http\Controllers\EntityContactController;
use Modules\SystemCore\App\Http\Controllers\PaymentTermController;
use Modules\SystemCore\App\Http\Controllers\ProductCategoryController;
use Modules\SystemCore\App\Http\Controllers\ProductController;
use Modules\SystemCore\App\Http\Controllers\PurchaseOrderController;
use Modules\SystemCore\App\Http\Controllers\SalesOrderController;
use Modules\SystemCore\App\Http\Controllers\SettingsController;
use Modules\SystemCore\App\Http\Controllers\SupplierController;
use Modules\SystemCore\App\Http\Controllers\SupplierDocumentController;
use Modules\SystemCore\App\Http\Controllers\SyncController;
use Modules\SystemCore\App\Http\Controllers\TaxConfigurationController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::middleware(['auth'])->prefix('systemcore')->name('systemcore.')->group(function () {
    // Dashboard
    Route::get('/', [DashboardController::class, 'index'])->name('dashboard');

    // Customers
    Route::prefix('customers')->name('customers.')->group(function () {
        Route::get('/', [CustomerController::class, 'index'])->name('index');
        Route::get('/ajax', [CustomerController::class, 'indexAjax'])->name('ajax');
        Route::get('/search', [CustomerController::class, 'search'])->name('search'); // MUST be before {customer}
        Route::get('/create', [CustomerController::class, 'create'])->name('create');
        Route::post('/', [CustomerController::class, 'store'])->name('store');
        Route::get('/{customer}', [CustomerController::class, 'show'])->name('show');
        Route::get('/{customer}/edit', [CustomerController::class, 'edit'])->name('edit');
        Route::put('/{customer}', [CustomerController::class, 'update'])->name('update');
        Route::delete('/{customer}', [CustomerController::class, 'destroy'])->name('destroy');
    });

    // Customer documents
    Route::prefix('customers/{customer}/documents')->name('customers.documents.')->group(function () {
        Route::post('/', [CustomerDocumentController::class, 'store'])->name('store');
        Route::get('/{document}', [CustomerDocumentController::class, 'show'])->name('show');
        Route::put('/{document}', [CustomerDocumentController::class, 'update'])->name('update');
        Route::delete('/{document}', [CustomerDocumentController::class, 'destroy'])->name('destroy');
        Route::get('/{document}/download', [CustomerDocumentController::class, 'download'])->name('download');
    });

    // Entity addresses and contacts (reusable for customer/supplier)
    Route::prefix('{entityType}/{entityId}')->where(['entityType' => 'customer|supplier'])->group(function () {
        Route::prefix('addresses')->name('entity.addresses.')->group(function () {
            Route::post('/', [EntityAddressController::class, 'store'])->name('store');
            Route::get('/{address}', [EntityAddressController::class, 'show'])->name('show');
            Route::put('/{address}', [EntityAddressController::class, 'update'])->name('update');
            Route::delete('/{address}', [EntityAddressController::class, 'destroy'])->name('destroy');
            Route::post('/{address}/set-primary', [EntityAddressController::class, 'setPrimary'])->name('set-primary');
        });
        Route::prefix('contacts')->name('entity.contacts.')->group(function () {
            Route::post('/', [EntityContactController::class, 'store'])->name('store');
            Route::get('/{contact}', [EntityContactController::class, 'show'])->name('show');
            Route::put('/{contact}', [EntityContactController::class, 'update'])->name('update');
            Route::delete('/{contact}', [EntityContactController::class, 'destroy'])->name('destroy');
            Route::post('/{contact}/set-primary', [EntityContactController::class, 'setPrimary'])->name('set-primary');
        });
    });

    // Suppliers
    Route::prefix('suppliers')->name('suppliers.')->group(function () {
        Route::get('/', [SupplierController::class, 'index'])->name('index');
        Route::get('/ajax', [SupplierController::class, 'indexAjax'])->name('ajax');
        Route::get('/search', [SupplierController::class, 'search'])->name('search'); // MUST be before {supplier}
        Route::get('/create', [SupplierController::class, 'create'])->name('create');
        Route::post('/', [SupplierController::class, 'store'])->name('store');
        Route::get('/{supplier}', [SupplierController::class, 'show'])->name('show');
        Route::get('/{supplier}/edit', [SupplierController::class, 'edit'])->name('edit');
        Route::put('/{supplier}', [SupplierController::class, 'update'])->name('update');
        Route::delete('/{supplier}', [SupplierController::class, 'destroy'])->name('destroy');
    });

    // Supplier documents
    Route::prefix('suppliers/{supplier}/documents')->name('suppliers.documents.')->group(function () {
        Route::post('/', [SupplierDocumentController::class, 'store'])->name('store');
        Route::get('/{document}', [SupplierDocumentController::class, 'show'])->name('show');
        Route::put('/{document}', [SupplierDocumentController::class, 'update'])->name('update');
        Route::delete('/{document}', [SupplierDocumentController::class, 'destroy'])->name('destroy');
        Route::get('/{document}/download', [SupplierDocumentController::class, 'download'])->name('download');
    });

    // Products
    Route::prefix('products')->name('products.')->group(function () {
        Route::get('/', [ProductController::class, 'index'])->name('index');
        Route::get('/ajax', [ProductController::class, 'indexAjax'])->name('ajax');
        Route::get('/search', [ProductController::class, 'search'])->name('search'); // MUST be before {product}
        Route::post('/upload-image', [ProductController::class, 'uploadImage'])->name('upload-image');
        Route::get('/create', [ProductController::class, 'create'])->name('create');
        Route::post('/', [ProductController::class, 'store'])->name('store');
        Route::get('/{product}', [ProductController::class, 'show'])->name('show');
        Route::get('/{product}/edit', [ProductController::class, 'edit'])->name('edit');
        Route::put('/{product}', [ProductController::class, 'update'])->name('update');
        Route::delete('/{product}', [ProductController::class, 'destroy'])->name('destroy');
    });

    // Sales Orders
    Route::prefix('sales-orders')->name('sales-orders.')->group(function () {
        Route::get('/', [SalesOrderController::class, 'index'])->name('index');
        Route::get('/ajax', [SalesOrderController::class, 'indexAjax'])->name('ajax');
        Route::get('/create', [SalesOrderController::class, 'create'])->name('create');
        Route::post('/', [SalesOrderController::class, 'store'])->name('store');
        Route::get('/{salesOrder}', [SalesOrderController::class, 'show'])->name('show');
        Route::get('/{salesOrder}/edit', [SalesOrderController::class, 'edit'])->name('edit');
        Route::put('/{salesOrder}', [SalesOrderController::class, 'update'])->name('update');
        Route::delete('/{salesOrder}', [SalesOrderController::class, 'destroy'])->name('destroy');
    });

    // Purchase Orders
    Route::prefix('purchase-orders')->name('purchase-orders.')->group(function () {
        Route::get('/', [PurchaseOrderController::class, 'index'])->name('index');
        Route::get('/ajax', [PurchaseOrderController::class, 'indexAjax'])->name('ajax');
        Route::get('/create', [PurchaseOrderController::class, 'create'])->name('create');
        Route::post('/', [PurchaseOrderController::class, 'store'])->name('store');
        Route::get('/{purchaseOrder}', [PurchaseOrderController::class, 'show'])->name('show');
        Route::get('/{purchaseOrder}/edit', [PurchaseOrderController::class, 'edit'])->name('edit');
        Route::put('/{purchaseOrder}', [PurchaseOrderController::class, 'update'])->name('update');
        Route::delete('/{purchaseOrder}', [PurchaseOrderController::class, 'destroy'])->name('destroy');
    });

    // Payment Terms
    Route::prefix('payment-terms')->name('payment-terms.')->group(function () {
        Route::get('/', [PaymentTermController::class, 'index'])->name('index');
        Route::get('/ajax', [PaymentTermController::class, 'indexAjax'])->name('ajax');
        Route::get('/create', [PaymentTermController::class, 'create'])->name('create');
        Route::post('/', [PaymentTermController::class, 'store'])->name('store');
        Route::get('/{paymentTerm}/edit', [PaymentTermController::class, 'edit'])->name('edit');
        Route::put('/{paymentTerm}', [PaymentTermController::class, 'update'])->name('update');
        Route::delete('/{paymentTerm}', [PaymentTermController::class, 'destroy'])->name('destroy');
        Route::get('/search', [PaymentTermController::class, 'search'])->name('search');
    });

    // Tax Configurations
    Route::prefix('tax-configurations')->name('tax-configurations.')->group(function () {
        Route::get('/', [TaxConfigurationController::class, 'index'])->name('index');
        Route::get('/ajax', [TaxConfigurationController::class, 'indexAjax'])->name('ajax');
        Route::get('/create', [TaxConfigurationController::class, 'create'])->name('create');
        Route::post('/', [TaxConfigurationController::class, 'store'])->name('store');
        Route::get('/{taxConfiguration}/edit', [TaxConfigurationController::class, 'edit'])->name('edit');
        Route::put('/{taxConfiguration}', [TaxConfigurationController::class, 'update'])->name('update');
        Route::delete('/{taxConfiguration}', [TaxConfigurationController::class, 'destroy'])->name('destroy');
        Route::get('/search', [TaxConfigurationController::class, 'search'])->name('search');
    });

    // Product Categories
    Route::prefix('product-categories')->name('product-categories.')->group(function () {
        Route::get('/', [ProductCategoryController::class, 'index'])->name('index');
        Route::get('/ajax', [ProductCategoryController::class, 'indexAjax'])->name('ajax');
        Route::get('/create', [ProductCategoryController::class, 'create'])->name('create');
        Route::post('/', [ProductCategoryController::class, 'store'])->name('store');
        Route::get('/{productCategory}/edit', [ProductCategoryController::class, 'edit'])->name('edit');
        Route::put('/{productCategory}', [ProductCategoryController::class, 'update'])->name('update');
        Route::delete('/{productCategory}', [ProductCategoryController::class, 'destroy'])->name('destroy');
        Route::get('/search', [ProductCategoryController::class, 'search'])->name('search');
    });

    // Sync Management / Posting History
    Route::prefix('sync')->name('sync.')->group(function () {
        Route::get('/', [SyncController::class, 'index'])->name('index');
        Route::get('/postings', [SyncController::class, 'postings'])->name('postings');
        Route::get('/postings/{postingLog}/details', [SyncController::class, 'viewDetails'])->name('details');
        Route::post('/postings/{postingLog}/retry', [SyncController::class, 'retryPosting'])->name('retry');
        Route::get('/statistics', [SyncController::class, 'statistics'])->name('statistics.ajax');
    });

    // Settings
    Route::prefix('settings')->name('settings.')->group(function () {
        Route::get('/', [SettingsController::class, 'index'])->name('index');
        Route::post('/', [SettingsController::class, 'update'])->name('update');
    });
});
