<?php

/*
|--------------------------------------------------------------------------
| Web Routes — Landed Cost
|--------------------------------------------------------------------------
| Generic, parent-polymorphic endpoints. {parentType} is one of
| pr_share | purchase | purchase_order; {parentRef} is the share token
| (pr_share) or the transaction id (core).
*/

use Modules\LandedCost\Http\Controllers\LandedCostController;
use Modules\LandedCost\Http\Controllers\InstallController;

// Install / Update / Uninstall (used by the module-management page)
Route::middleware(['web', 'auth'])->prefix('landed-cost-module')->group(function () {
    Route::get('/install', [InstallController::class, 'index']);
    Route::post('/install', [InstallController::class, 'install']);
    Route::get('/uninstall', [InstallController::class, 'uninstall']);
    Route::get('/update', [InstallController::class, 'update']);
    Route::get('/repair', [InstallController::class, 'repair']);
});

Route::group([
    'middleware' => ['web', 'auth', 'SetSessionData', 'language', 'timezone'],
    'prefix'     => 'landed-cost',
], function () {
    Route::get('/{parentType}/{parentRef}/expenses',                       [LandedCostController::class, 'getExpenses'])->name('landed-cost.getExpenses');
    Route::post('/{parentType}/{parentRef}/expenses',                      [LandedCostController::class, 'storeExpense'])->name('landed-cost.storeExpense');
    Route::post('/{parentType}/{parentRef}/expenses/preview',             [LandedCostController::class, 'previewDistribution'])->name('landed-cost.preview');
    Route::post('/{parentType}/{parentRef}/expenses/{expenseId}',         [LandedCostController::class, 'updateExpense'])->name('landed-cost.updateExpense');
    Route::delete('/{parentType}/{parentRef}/expenses/{expenseId}',       [LandedCostController::class, 'deleteExpense'])->name('landed-cost.deleteExpense');
    Route::post('/{parentType}/{parentRef}/expenses/{expenseId}/custom-ratios', [LandedCostController::class, 'saveCustomRatios'])->name('landed-cost.saveCustomRatios');

    // Apply landed cost to the real purchase cost / roll it back
    Route::post('/{parentType}/{parentRef}/apply',    [LandedCostController::class, 'applyLandedCosts'])->name('landed-cost.apply');
    Route::post('/{parentType}/{parentRef}/rollback', [LandedCostController::class, 'rollbackLandedCosts'])->name('landed-cost.rollback');
});
