<?php

use App\Http\Controllers\Ranch\CattleCalvingController;
use App\Http\Controllers\Ranch\CattleController;
use App\Http\Controllers\Ranch\CattleLocationMovementController;
use App\Http\Controllers\Ranch\PastureController;
use App\Http\Controllers\Ranch\CattleHealthEventController;
use App\Http\Controllers\Ranch\MobileBootstrapController;
use App\Http\Controllers\Ranch\RanchAlertCenterController;
use App\Http\Controllers\Ranch\FatteningLotController;
use App\Http\Controllers\Ranch\FatteningLotFeedIntakeController;
use App\Http\Controllers\Ranch\MaterialExpenseController;
use App\Http\Controllers\Ranch\RanchNotificationController;
use App\Http\Controllers\Ranch\RanchDatabaseBackupController;
use App\Http\Controllers\Ranch\RanchDashboardController;
use App\Http\Controllers\Ranch\RanchReportController;
use App\Http\Controllers\Ranch\WeightRecordController;
use Illuminate\Support\Facades\Route;

Route::prefix('ranch')->group(function (): void {
    Route::get('/mobile/bootstrap', MobileBootstrapController::class)
        ->middleware('permission:ranch.dashboard|ranch.cattle.view|ranch.cattle.manage|ranch.weight.record|ranch.fattening.manage|ranch.expenses.manage|ranch.reports.view|ranch.health.view|ranch.health.manage');

    Route::get('/catalog', [CattleController::class, 'catalog'])
        ->middleware('permission:ranch.cattle.view|ranch.cattle.manage');

    Route::get('/dashboard', [RanchDashboardController::class, 'summary'])
        ->middleware('permission:ranch.dashboard');
    Route::get('/alerts/center', [RanchAlertCenterController::class, 'index'])
        ->middleware('permission:ranch.dashboard|ranch.reports.view|ranch.cattle.view');

    Route::get('/cattle', [CattleController::class, 'index'])
        ->middleware('permission:ranch.cattle.view|ranch.cattle.manage');
    Route::get('/cattle/search', [CattleController::class, 'search'])
        ->middleware('permission:ranch.cattle.view|ranch.cattle.manage|ranch.weight.record');
    Route::post('/cattle', [CattleController::class, 'store'])
        ->middleware('permission:ranch.cattle.manage');
    Route::get('/cattle/{cattle}', [CattleController::class, 'show'])
        ->middleware('permission:ranch.cattle.view|ranch.cattle.manage');
    Route::get('/cattle/{cattle}/expediente/pdf', [CattleController::class, 'expedientePdf'])
        ->middleware('permission:ranch.cattle.view|ranch.cattle.manage|ranch.reports.view');
    Route::post('/cattle/{cattle}/photo', [CattleController::class, 'uploadPhoto'])
        ->middleware('permission:ranch.cattle.manage');
    Route::delete('/cattle/{cattle}/photo', [CattleController::class, 'deletePhoto'])
        ->middleware('permission:ranch.cattle.manage');
    Route::put('/cattle/{cattle}', [CattleController::class, 'update'])
        ->middleware('permission:ranch.cattle.manage');
    Route::post('/cattle/{cattle}/deactivate', [CattleController::class, 'deactivate'])
        ->middleware('permission:ranch.cattle.manage');

    Route::get('/calvings', [CattleCalvingController::class, 'index'])
        ->middleware('permission:ranch.cattle.view|ranch.cattle.manage');
    Route::post('/calvings', [CattleCalvingController::class, 'store'])
        ->middleware('permission:ranch.cattle.manage');

    Route::get('/location-movements', [CattleLocationMovementController::class, 'index'])
        ->middleware('permission:ranch.cattle.view|ranch.cattle.manage');
    Route::post('/cattle/{cattle}/move-pasture', [CattleLocationMovementController::class, 'movePasture'])
        ->middleware('permission:ranch.cattle.manage');

    Route::get('/pastures', [PastureController::class, 'index'])
        ->middleware('permission:ranch.cattle.view|ranch.cattle.manage');
    Route::post('/pastures', [PastureController::class, 'store'])
        ->middleware('permission:ranch.cattle.manage');
    Route::put('/pastures/{pasture}', [PastureController::class, 'update'])
        ->middleware('permission:ranch.cattle.manage');

    Route::get('/weight-records/calves', [WeightRecordController::class, 'calves'])
        ->middleware('permission:ranch.weight.record|ranch.cattle.view');
    Route::get('/weight-records', [WeightRecordController::class, 'index'])
        ->middleware('permission:ranch.weight.record|ranch.cattle.view');
    Route::post('/weight-records', [WeightRecordController::class, 'store'])
        ->middleware('permission:ranch.weight.record');

    Route::get('/health-events/catalog', [CattleHealthEventController::class, 'catalog'])
        ->middleware('permission:ranch.health.view|ranch.health.manage');
    Route::get('/health-events/summary', [CattleHealthEventController::class, 'summary'])
        ->middleware('permission:ranch.health.view|ranch.reports.view');
    Route::get('/health-events', [CattleHealthEventController::class, 'index'])
        ->middleware('permission:ranch.health.view|ranch.health.manage');
    Route::post('/health-events', [CattleHealthEventController::class, 'store'])
        ->middleware('permission:ranch.health.manage');

    Route::get('/fattening-lots', [FatteningLotController::class, 'index'])
        ->middleware('permission:ranch.fattening.manage|ranch.cattle.view');
    Route::post('/fattening-lots', [FatteningLotController::class, 'store'])
        ->middleware('permission:ranch.fattening.manage');
    Route::get('/fattening-lots/{fatteningLot}', [FatteningLotController::class, 'show'])
        ->middleware('permission:ranch.fattening.manage|ranch.cattle.view');
    Route::get('/fattening-lots/{fatteningLot}/pdf', [FatteningLotController::class, 'pdf'])
        ->middleware('permission:ranch.fattening.manage|ranch.reports.view|ranch.cattle.view');
    Route::put('/fattening-lots/{fatteningLot}', [FatteningLotController::class, 'update'])
        ->middleware('permission:ranch.fattening.manage');
    Route::post('/fattening-lots/{fatteningLot}/preview-close', [FatteningLotController::class, 'previewClose'])
        ->middleware('permission:ranch.fattening.manage');
    Route::post('/fattening-lots/{fatteningLot}/animals', [FatteningLotController::class, 'addAnimal'])
        ->middleware('permission:ranch.fattening.manage');
    Route::post('/fattening-lots/{fatteningLot}/close', [FatteningLotController::class, 'close'])
        ->middleware('permission:ranch.fattening.manage');
    Route::post('/fattening-lots/{fatteningLot}/close-and-sell', [FatteningLotController::class, 'closeAndSell'])
        ->middleware('permission:ranch.fattening.manage');
    Route::get('/feed-inventory', [MaterialExpenseController::class, 'feedInventory'])
        ->middleware('permission:ranch.fattening.manage|ranch.expenses.manage|ranch.cattle.view');
    Route::get('/fattening-lots/{fatteningLot}/feed-intakes', [FatteningLotFeedIntakeController::class, 'index'])
        ->middleware('permission:ranch.fattening.manage|ranch.cattle.view');
    Route::post('/fattening-lots/{fatteningLot}/feed-intakes', [FatteningLotFeedIntakeController::class, 'store'])
        ->middleware('permission:ranch.fattening.manage');
    Route::put('/fattening-lots/{fatteningLot}/feed-intakes/{feedIntake}', [FatteningLotFeedIntakeController::class, 'update'])
        ->middleware('permission:ranch.fattening.manage');
    Route::delete('/fattening-lots/{fatteningLot}/feed-intakes/{feedIntake}', [FatteningLotFeedIntakeController::class, 'destroy'])
        ->middleware('permission:ranch.fattening.manage');

    Route::get('/material-types', [MaterialExpenseController::class, 'materialTypes'])
        ->middleware('permission:ranch.expenses.manage|ranch.cattle.view|ranch.fattening.manage');
    Route::post('/material-types', [MaterialExpenseController::class, 'storeMaterialType'])
        ->middleware('permission:ranch.expenses.manage');
    Route::put('/material-types/{materialType}', [MaterialExpenseController::class, 'updateMaterialType'])
        ->middleware('permission:ranch.expenses.manage');
    Route::get('/expenses', [MaterialExpenseController::class, 'index'])
        ->middleware('permission:ranch.expenses.manage|ranch.reports.view');
    Route::post('/expenses', [MaterialExpenseController::class, 'store'])
        ->middleware('permission:ranch.expenses.manage');

    Route::get('/reports/period-balance', [RanchReportController::class, 'periodBalance'])
        ->middleware('permission:ranch.reports.view');
    Route::get('/reports/period-balance/csv', [RanchReportController::class, 'periodBalanceCsv'])
        ->middleware('permission:ranch.reports.view');
    Route::get('/reports/period-balance/pdf', [RanchReportController::class, 'periodBalancePdf'])
        ->middleware('permission:ranch.reports.view');
    Route::get('/reports/weight-performance', [RanchReportController::class, 'weightPerformance'])
        ->middleware('permission:ranch.reports.view|ranch.cattle.view');
    Route::get('/reports/mother-performance', [RanchReportController::class, 'motherPerformance'])
        ->middleware('permission:ranch.reports.view|ranch.cattle.view');
    Route::get('/reports/animal-profitability', [RanchReportController::class, 'animalProfitability'])
        ->middleware('permission:ranch.reports.view');
    Route::get('/reports/animal-profitability/csv', [RanchReportController::class, 'animalProfitabilityCsv'])
        ->middleware('permission:ranch.reports.view');
    Route::get('/reports/animal-profitability/pdf', [RanchReportController::class, 'animalProfitabilityPdf'])
        ->middleware('permission:ranch.reports.view');
    Route::get('/reports/closed-lots-ranking', [RanchReportController::class, 'closedLotsRanking'])
        ->middleware('permission:ranch.reports.view|ranch.fattening.manage');
    Route::get('/reports/closed-lots-ranking/csv', [RanchReportController::class, 'closedLotsRankingCsv'])
        ->middleware('permission:ranch.reports.view|ranch.fattening.manage');
    Route::get('/reports/inventory', [RanchReportController::class, 'inventory'])
        ->middleware('permission:ranch.cattle.view');
    Route::get('/reports/inventory/csv', [RanchReportController::class, 'inventoryCsv'])
        ->middleware('permission:ranch.cattle.view');
    Route::get('/reports/open-lots-projection', [RanchReportController::class, 'openLotsProjection'])
        ->middleware('permission:ranch.fattening.manage|ranch.reports.view');
    Route::get('/reports/open-lots-projection/csv', [RanchReportController::class, 'openLotsProjectionCsv'])
        ->middleware('permission:ranch.fattening.manage|ranch.reports.view');
    Route::get('/reports/open-lots-gdp-alerts', [RanchReportController::class, 'openLotsGdpAlerts'])
        ->middleware('permission:ranch.fattening.manage|ranch.reports.view|ranch.weight.record');
    Route::get('/reports/calvings', [RanchReportController::class, 'calvingsReport'])
        ->middleware('permission:ranch.reports.view|ranch.cattle.view');
    Route::get('/reports/calvings/csv', [RanchReportController::class, 'calvingsReportCsv'])
        ->middleware('permission:ranch.reports.view|ranch.cattle.view');
    Route::get('/reports/active-cost-alerts', [RanchReportController::class, 'activeCostAlerts'])
        ->middleware('permission:ranch.reports.view|ranch.cattle.view');

    Route::post('/notifications/cost-alerts', [RanchNotificationController::class, 'sendCostAlerts'])
        ->middleware('permission:administracion.usuarios|ranch.reports.view');

    Route::post('/notifications/weight-alerts', [RanchNotificationController::class, 'sendWeightAlerts'])
        ->middleware('permission:administracion.usuarios|ranch.reports.view');
    Route::post('/notifications/open-lots-gdp-alerts', [RanchNotificationController::class, 'sendOpenLotsGdpAlerts'])
        ->middleware('permission:administracion.usuarios|ranch.reports.view|ranch.fattening.manage');
    Route::post('/notifications/calving-report', [RanchNotificationController::class, 'sendCalvingReport'])
        ->middleware('permission:administracion.usuarios|ranch.reports.view|ranch.cattle.view');

    Route::get('/admin/database-backup', [RanchDatabaseBackupController::class, 'index'])
        ->middleware('permission:administracion.usuarios');
    Route::get('/admin/database-backup/download', [RanchDatabaseBackupController::class, 'download'])
        ->middleware('permission:administracion.usuarios');
});
