<?php

use App\Http\Controllers\CustomerController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\FeedingController;
use App\Http\Controllers\FinanceController;
use App\Http\Controllers\HarvestController;
use App\Http\Controllers\HealthController;
use App\Http\Controllers\ImpersonationController;
use App\Http\Controllers\InventoryController;
use App\Http\Controllers\NotificationController;
use App\Http\Controllers\PondController;
use App\Http\Controllers\ProductionCycleController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\ReportController;
use App\Http\Controllers\SalesController;
use App\Http\Controllers\SamplingController;
use App\Http\Controllers\SettingsController;
use App\Http\Controllers\SupplierController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\WaterChangeController;
use App\Http\Controllers\WaterQualityController;
use Illuminate\Support\Facades\Route;

Route::get('/', fn () => redirect()->route('dashboard'));

require __DIR__.'/auth.php';

Route::middleware(['auth', 'verified'])->group(function () {

    // Dashboard
    Route::get('/dashboard', DashboardController::class)->name('dashboard');

    // Profile
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');

    // Impersonation — stop is available to any authenticated user (they may not be admin while impersonating)
    Route::post('/impersonate/stop', [ImpersonationController::class, 'stop'])->name('impersonate.stop');

    // Notifications
    Route::get('/notifications', [NotificationController::class, 'index'])->name('notifications.index');
    Route::get('/notifications/unread-count', [NotificationController::class, 'unreadCount'])->name('notifications.unread-count');
    Route::get('/notifications/recent', [NotificationController::class, 'recent'])->name('notifications.recent');
    Route::post('/notifications/read-all', [NotificationController::class, 'markAllRead'])->name('notifications.read-all');
    Route::post('/notifications/{notification}/read', [NotificationController::class, 'markRead'])->name('notifications.read');

    // -------------------------------------------------------------------------
    // PONDS — literal routes before parameterized ones
    // -------------------------------------------------------------------------
    Route::get('/ponds', [PondController::class, 'index'])->name('ponds.index');

    Route::middleware('role:admin,manager')->group(function () {
        Route::get('/ponds/create', [PondController::class, 'create'])->name('ponds.create');
        Route::post('/ponds', [PondController::class, 'store'])->name('ponds.store');
    });

    // Bulk log (farm_hand, manager, admin) — must be before /{pond}
    Route::middleware('role:admin,manager,farm_hand')->group(function () {
        Route::get('/ponds/bulk-log', [PondController::class, 'bulkLogForm'])->name('ponds.bulk-log');
        Route::post('/ponds/bulk-log', [PondController::class, 'bulkLogStore'])->name('ponds.bulk-log.store');
    });

    // Parameterized pond routes (after literal /ponds/* routes)
    Route::get('/ponds/{pond}', [PondController::class, 'show'])->name('ponds.show');

    Route::middleware('role:admin,manager')->group(function () {
        Route::get('/ponds/{pond}/edit', [PondController::class, 'edit'])->name('ponds.edit');
        Route::patch('/ponds/{pond}', [PondController::class, 'update'])->name('ponds.update');
        Route::delete('/ponds/{pond}', [PondController::class, 'destroy'])->name('ponds.destroy');
    });

    // -------------------------------------------------------------------------
    // CYCLES — literal routes before parameterized ones
    // -------------------------------------------------------------------------
    Route::middleware('role:admin,manager')->group(function () {
        Route::get('/ponds/{pond}/cycles/create', [ProductionCycleController::class, 'create'])->name('cycles.create');
        Route::post('/ponds/{pond}/cycles', [ProductionCycleController::class, 'store'])->name('cycles.store');
    });

    // Parameterized cycle routes (after /cycles/create)
    Route::get('/ponds/{pond}/cycles/{cycle}', [ProductionCycleController::class, 'show'])->name('cycles.show');

    Route::middleware('role:admin,manager')->group(function () {
        Route::patch('/ponds/{pond}/cycles/{cycle}/close', [ProductionCycleController::class, 'close'])->name('cycles.close');
    });

    // -------------------------------------------------------------------------
    // OPERATIONS (farm_hand, manager, admin)
    // -------------------------------------------------------------------------
    Route::middleware('role:admin,manager,farm_hand')->group(function () {

        // Water Changes
        Route::get('/water-changes/create', [WaterChangeController::class, 'create'])->name('water-changes.create');
        Route::post('/water-changes', [WaterChangeController::class, 'store'])->name('water-changes.store');
        Route::delete('/water-changes/{waterChangeLog}', [WaterChangeController::class, 'destroy'])->name('water-changes.destroy');

        // Feeding
        Route::get('/feeding', [FeedingController::class, 'index'])->name('feeding.index');
        Route::get('/feeding/create', [FeedingController::class, 'create'])->name('feeding.create');
        Route::post('/feeding', [FeedingController::class, 'store'])->name('feeding.store');
        Route::delete('/feeding/{feedingLog}', [FeedingController::class, 'destroy'])->name('feeding.destroy');

        // Water Quality — literal /create before /{log}
        Route::get('/water-quality', [WaterQualityController::class, 'index'])->name('water-quality.index');
        Route::get('/water-quality/create', [WaterQualityController::class, 'create'])->name('water-quality.create');
        Route::post('/water-quality', [WaterQualityController::class, 'store'])->name('water-quality.store');
        Route::get('/water-quality/{waterQualityLog}', [WaterQualityController::class, 'show'])->name('water-quality.show');

        // Health & Mortality
        Route::get('/health', [HealthController::class, 'index'])->name('health.index');
        Route::get('/health/mortality/create', [HealthController::class, 'createMortality'])->name('health.mortality.create');
        Route::post('/health/mortality', [HealthController::class, 'storeMortality'])->name('health.mortality.store');
        Route::get('/health/treatment/create', [HealthController::class, 'createTreatment'])->name('health.treatment.create');
        Route::post('/health/treatment', [HealthController::class, 'storeTreatment'])->name('health.treatment.store');
        Route::patch('/health/treatment/{treatmentLog}/outcome', [HealthController::class, 'updateTreatmentOutcome'])->name('health.treatment.outcome');

        // Sampling
        Route::get('/sampling', [SamplingController::class, 'index'])->name('sampling.index');
        Route::get('/sampling/create', [SamplingController::class, 'create'])->name('sampling.create');
        Route::post('/sampling', [SamplingController::class, 'store'])->name('sampling.store');
    });

    // -------------------------------------------------------------------------
    // HARVEST, SALES, INVENTORY, FINANCE, REPORTS (admin + manager)
    // -------------------------------------------------------------------------
    Route::middleware('role:admin,manager')->group(function () {

        // Harvest — literal /create before /{harvest}
        Route::get('/harvest', [HarvestController::class, 'index'])->name('harvest.index');
        Route::get('/harvest/create', [HarvestController::class, 'create'])->name('harvest.create');
        Route::post('/harvest', [HarvestController::class, 'store'])->name('harvest.store');
        Route::get('/harvest/{harvest}', [HarvestController::class, 'show'])->name('harvest.show');

        // Sales — literal routes before /{sale}
        Route::get('/sales', [SalesController::class, 'index'])->name('sales.index');
        Route::get('/sales/create', [SalesController::class, 'create'])->name('sales.create');
        Route::post('/sales', [SalesController::class, 'store'])->name('sales.store');
        Route::get('/sales/{sale}', [SalesController::class, 'show'])->name('sales.show');
        Route::patch('/sales/{sale}/payment', [SalesController::class, 'updatePayment'])->name('sales.update-payment');

        // Inventory — literal /create before /{item}
        Route::get('/inventory', [InventoryController::class, 'index'])->name('inventory.index');
        Route::get('/inventory/create', [InventoryController::class, 'create'])->name('inventory.create');
        Route::post('/inventory', [InventoryController::class, 'store'])->name('inventory.store');
        Route::get('/inventory/{item}', [InventoryController::class, 'show'])->name('inventory.show');
        Route::get('/inventory/{item}/edit', [InventoryController::class, 'edit'])->name('inventory.edit');
        Route::patch('/inventory/{item}', [InventoryController::class, 'update'])->name('inventory.update');
        Route::post('/inventory/{item}/receipt', [InventoryController::class, 'receipt'])->name('inventory.receipt');
        Route::post('/inventory/{item}/adjust', [InventoryController::class, 'adjust'])->name('inventory.adjust');
        Route::delete('/inventory/{item}', [InventoryController::class, 'destroy'])->name('inventory.destroy');

        // Suppliers — literal /create before /{supplier}
        Route::get('/suppliers', [SupplierController::class, 'index'])->name('suppliers.index');
        Route::get('/suppliers/create', [SupplierController::class, 'create'])->name('suppliers.create');
        Route::post('/suppliers', [SupplierController::class, 'store'])->name('suppliers.store');
        Route::get('/suppliers/{supplier}', [SupplierController::class, 'show'])->name('suppliers.show');
        Route::get('/suppliers/{supplier}/edit', [SupplierController::class, 'edit'])->name('suppliers.edit');
        Route::patch('/suppliers/{supplier}', [SupplierController::class, 'update'])->name('suppliers.update');
        Route::post('/suppliers/{supplier}/flag', [SupplierController::class, 'flag'])->name('suppliers.flag');
        Route::delete('/suppliers/{supplier}', [SupplierController::class, 'destroy'])->name('suppliers.destroy');

        // Customers — literal /create before /{customer}
        Route::get('/customers', [CustomerController::class, 'index'])->name('customers.index');
        Route::get('/customers/create', [CustomerController::class, 'create'])->name('customers.create');
        Route::post('/customers', [CustomerController::class, 'store'])->name('customers.store');
        Route::get('/customers/{customer}', [CustomerController::class, 'show'])->name('customers.show');
        Route::get('/customers/{customer}/edit', [CustomerController::class, 'edit'])->name('customers.edit');
        Route::patch('/customers/{customer}', [CustomerController::class, 'update'])->name('customers.update');
        Route::post('/customers/{customer}/flag', [CustomerController::class, 'flag'])->name('customers.flag');
        Route::delete('/customers/{customer}', [CustomerController::class, 'destroy'])->name('customers.destroy');

        // Finance — literal /create before /{expense}
        Route::get('/finance', [FinanceController::class, 'index'])->name('finance.index');
        Route::get('/finance/create', [FinanceController::class, 'create'])->name('finance.create');
        Route::post('/finance', [FinanceController::class, 'store'])->name('finance.store');
        Route::delete('/finance/{expense}', [FinanceController::class, 'destroy'])->name('finance.destroy');

        // Reports
        Route::get('/reports', [ReportController::class, 'index'])->name('reports.index');
        Route::get('/reports/cycle-summary', [ReportController::class, 'cycleSummary'])->name('reports.cycle-summary');
        Route::get('/reports/feed-usage', [ReportController::class, 'feedUsage'])->name('reports.feed-usage');
        Route::get('/reports/sales-receivables', [ReportController::class, 'salesReceivables'])->name('reports.sales-receivables');
        Route::get('/reports/inventory-valuation', [ReportController::class, 'inventoryValuation'])->name('reports.inventory-valuation');
    });

    // -------------------------------------------------------------------------
    // ADMIN ONLY — Settings & Users
    // -------------------------------------------------------------------------
    Route::middleware('role:admin')->group(function () {

        Route::get('/settings', [SettingsController::class, 'index'])->name('settings.index');
        Route::post('/settings/species', [SettingsController::class, 'storeSpecies'])->name('settings.species.store');
        Route::put('/settings/species/{species}', [SettingsController::class, 'updateSpecies'])->name('settings.species.update');
        Route::delete('/settings/species/{species}', [SettingsController::class, 'destroySpecies'])->name('settings.species.destroy');
        Route::post('/settings/feed', [SettingsController::class, 'storeFeed'])->name('settings.feed.store');
        Route::put('/settings/feed/{feed}', [SettingsController::class, 'updateFeed'])->name('settings.feed.update');
        Route::delete('/settings/feed/{feed}', [SettingsController::class, 'destroyFeed'])->name('settings.feed.destroy');
        Route::post('/settings/lookup', [SettingsController::class, 'storeLookup'])->name('settings.lookup.store');
        Route::put('/settings/lookup/{lookup}', [SettingsController::class, 'updateLookup'])->name('settings.lookup.update');
        Route::delete('/settings/lookup/{lookup}', [SettingsController::class, 'destroyLookup'])->name('settings.lookup.destroy');
        Route::post('/settings/app', [SettingsController::class, 'storeAppSetting'])->name('settings.app.store');

        // Roles
        Route::post('/settings/roles', [SettingsController::class, 'storeRole'])->name('settings.roles.store');
        Route::put('/settings/roles/{role}', [SettingsController::class, 'updateRole'])->name('settings.roles.update');
        Route::delete('/settings/roles/{role}', [SettingsController::class, 'destroyRole'])->name('settings.roles.destroy');

        // Sales — edit/update (admin only)
        Route::get('/sales/{sale}/edit', [SalesController::class, 'edit'])->name('sales.edit');
        Route::patch('/sales/{sale}', [SalesController::class, 'update'])->name('sales.update');

        // Users — literal /create before /{user}
        Route::get('/users', [UserController::class, 'index'])->name('users.index');
        Route::get('/users/create', [UserController::class, 'create'])->name('users.create');
        Route::post('/users', [UserController::class, 'store'])->name('users.store');
        Route::get('/users/{user}/edit', [UserController::class, 'edit'])->name('users.edit');
        Route::patch('/users/{user}', [UserController::class, 'update'])->name('users.update');
        Route::delete('/users/{user}', [UserController::class, 'destroy'])->name('users.destroy');
        Route::post('/users/{user}/impersonate', [ImpersonationController::class, 'start'])->name('impersonate.start');
    });

});
