<?php

use Illuminate\Support\Facades\Route;
use Modules\Bms\Http\Controllers\AddonController;
use Modules\Bms\Http\Controllers\AreaController;
use Modules\Bms\Http\Controllers\AssetController;
use Modules\Bms\Http\Controllers\BmsController;
use Modules\Bms\Http\Controllers\BookingController;
use Modules\Bms\Http\Controllers\CalendarController;
use Modules\Bms\Http\Controllers\CatalogOptionController;
use Modules\Bms\Http\Controllers\HallController;
use Modules\Bms\Http\Controllers\MaintenanceController;
use Modules\Bms\Http\Controllers\MatchController;
use Modules\Bms\Http\Controllers\MatchingSettingController;
use Modules\Bms\Http\Controllers\NotificationMatrixController;
use Modules\Bms\Http\Controllers\OfficeVisitController;
use Modules\Bms\Http\Controllers\PackageController;
use Modules\Bms\Http\Controllers\TaskTemplateController;
use Modules\Bms\Http\Controllers\TransferController;
use Modules\Bms\Http\Controllers\VenueController;

/*
|--------------------------------------------------------------------------
| Angular PWA shells (public) — history-mode fallback
|--------------------------------------------------------------------------
| Serve the Vite/Angular-built index.html for any /bms-client/* or
| /bms-staff/* URL so the SPA router works on reload. Hashed asset files in
| public/bms-client|bms-staff are served by the web server directly.
*/
foreach (['bms-client', 'bms-staff'] as $bmsSpa) {
    Route::middleware(['web'])->prefix($bmsSpa)->group(function () use ($bmsSpa) {
        Route::get('/{any?}', function (string $any = '') use ($bmsSpa) {
            // A request for a hashed asset (has a file extension) that does not
            // exist on disk must 404 — NOT fall through to index.html, or the
            // browser receives text/html for a .js and throws a MIME error.
            // This typically means a stale cached index.html references an old
            // chunk; the SPA's chunk-error handler then reloads to recover.
            if (preg_match('/\.[a-z0-9]{2,5}$/i', $any)) {
                abort(404);
            }

            $shell = public_path($bmsSpa . '/index.html');
            if (! file_exists($shell)) {
                return response("Build the {$bmsSpa} PWA first (npm run build in {$bmsSpa}/).", 200);
            }
            // index.html is never cached so a new deploy is picked up immediately;
            // hashed assets are cached forever by the web server.
            return response()->file($shell, [
                'Content-Type'  => 'text/html; charset=utf-8',
                'Cache-Control' => 'no-store, no-cache, must-revalidate, max-age=0',
                'Pragma'        => 'no-cache',
            ]);
        })->where('any', '.*');
    });
}

/*
|--------------------------------------------------------------------------
| BMS P1 Back-Office (Blade, inside the ERP UI)
|--------------------------------------------------------------------------
*/
Route::middleware(['web', 'auth', 'SetSessionData', 'language', 'timezone', 'AdminSidebarMenu'])
    ->prefix('bms')
    ->name('bms.')
    ->group(function () {
        Route::get('/', [BmsController::class, 'index'])->name('dashboard');

        // Coverage areas
        Route::get('areas', [AreaController::class, 'index'])->name('areas.index');
        Route::get('areas/create', [AreaController::class, 'create'])->name('areas.create');
        Route::post('areas', [AreaController::class, 'store'])->name('areas.store');
        Route::get('areas/{id}/edit', [AreaController::class, 'edit'])->name('areas.edit');
        Route::put('areas/{id}', [AreaController::class, 'update'])->name('areas.update');
        Route::delete('areas/{id}', [AreaController::class, 'destroy'])->name('areas.destroy');

        // Venues
        Route::get('venues', [VenueController::class, 'index'])->name('venues.index');
        Route::get('venues/create', [VenueController::class, 'create'])->name('venues.create');
        Route::post('venues', [VenueController::class, 'store'])->name('venues.store');
        Route::get('venues/{id}/edit', [VenueController::class, 'edit'])->name('venues.edit');
        Route::put('venues/{id}', [VenueController::class, 'update'])->name('venues.update');
        Route::delete('venues/{id}', [VenueController::class, 'destroy'])->name('venues.destroy');

        // Halls
        Route::get('halls', [HallController::class, 'index'])->name('halls.index');
        Route::get('halls/create', [HallController::class, 'create'])->name('halls.create');
        Route::post('halls', [HallController::class, 'store'])->name('halls.store');
        Route::get('halls/{id}/edit', [HallController::class, 'edit'])->name('halls.edit');
        Route::put('halls/{id}', [HallController::class, 'update'])->name('halls.update');
        Route::delete('halls/{id}', [HallController::class, 'destroy'])->name('halls.destroy');
        // Hall media manager
        Route::get('halls/{id}/media', [HallController::class, 'media'])->name('halls.media');
        Route::post('halls/{id}/media', [HallController::class, 'storeMedia'])->name('halls.media.store');
        Route::delete('halls/{id}/media/{mediaId}', [HallController::class, 'destroyMedia'])->name('halls.media.destroy');

        // Packages
        Route::get('packages', [PackageController::class, 'index'])->name('packages.index');
        Route::get('packages/product-search', [PackageController::class, 'productSearch'])->name('packages.product-search');
        Route::get('packages/product-preview/{id}', [PackageController::class, 'productPreview'])->whereNumber('id')->name('packages.product-preview');

        // Product marketing media (image/video/360), opened from the product form
        Route::get('products/{id}/media', [\Modules\Bms\Http\Controllers\ProductMediaController::class, 'media'])->name('products.media');
        Route::post('products/{id}/media', [\Modules\Bms\Http\Controllers\ProductMediaController::class, 'storeMedia'])->name('products.media.store');
        Route::delete('products/{id}/media/{mediaId}', [\Modules\Bms\Http\Controllers\ProductMediaController::class, 'destroyMedia'])->name('products.media.destroy');
        Route::get('packages/create', [PackageController::class, 'create'])->name('packages.create');
        Route::post('packages', [PackageController::class, 'store'])->name('packages.store');
        Route::get('packages/{id}/edit', [PackageController::class, 'edit'])->name('packages.edit');
        Route::put('packages/{id}', [PackageController::class, 'update'])->name('packages.update');
        Route::delete('packages/{id}', [PackageController::class, 'destroy'])->name('packages.destroy');

        // Add-ons
        Route::get('addons', [AddonController::class, 'index'])->name('addons.index');
        Route::get('addons/create', [AddonController::class, 'create'])->name('addons.create');
        Route::post('addons', [AddonController::class, 'store'])->name('addons.store');
        Route::get('addons/{id}/edit', [AddonController::class, 'edit'])->name('addons.edit');
        Route::put('addons/{id}', [AddonController::class, 'update'])->name('addons.update');
        Route::delete('addons/{id}', [AddonController::class, 'destroy'])->name('addons.destroy');

        // Matching engine settings + tester
        Route::get('matching', [MatchingSettingController::class, 'edit'])->name('matching.edit');
        Route::put('matching', [MatchingSettingController::class, 'update'])->name('matching.update');
        Route::get('matching/tester', [MatchController::class, 'tester'])->name('matching.tester');
        Route::post('matching/run', [MatchController::class, 'run'])->name('matching.run');

        // Notification matrix (P1)
        Route::get('notifications', [NotificationMatrixController::class, 'edit'])->name('notifications.edit');
        Route::put('notifications', [NotificationMatrixController::class, 'update'])->name('notifications.update');

        // Booking policy: cancellation tiers + payment schedule + deposit gate (P1)
        Route::get('settings/booking-policy', [\Modules\Bms\Http\Controllers\BookingPolicyController::class, 'edit'])->name('booking-policy.edit');
        Route::put('settings/booking-policy', [\Modules\Bms\Http\Controllers\BookingPolicyController::class, 'update'])->name('booking-policy.update');

        // QC checklist template (P1)
        Route::get('settings/qc-checklist', [\Modules\Bms\Http\Controllers\QcChecklistController::class, 'edit'])->name('qc-checklist.edit');
        Route::put('settings/qc-checklist', [\Modules\Bms\Http\Controllers\QcChecklistController::class, 'update'])->name('qc-checklist.update');

        // Catalog options (F&B levels + Features) settings
        Route::get('settings/catalog', [CatalogOptionController::class, 'edit'])->name('catalog-options.edit');
        Route::put('settings/catalog', [CatalogOptionController::class, 'update'])->name('catalog-options.update');

        // Task templates (P1)
        Route::get('task-templates', [TaskTemplateController::class, 'index'])->name('task-templates.index');
        Route::get('task-templates/create', [TaskTemplateController::class, 'create'])->name('task-templates.create');
        Route::post('task-templates', [TaskTemplateController::class, 'store'])->name('task-templates.store');
        Route::get('task-templates/{id}/edit', [TaskTemplateController::class, 'edit'])->name('task-templates.edit');
        Route::put('task-templates/{id}', [TaskTemplateController::class, 'update'])->name('task-templates.update');
        Route::delete('task-templates/{id}', [TaskTemplateController::class, 'destroy'])->name('task-templates.destroy');

        // Transfer oversight (P1, read-only)
        Route::get('transfers', [TransferController::class, 'index'])->name('transfers.index');

        // Asset register (Release 2) — literal routes before {id} to avoid clashes
        Route::get('assets', [AssetController::class, 'index'])->name('assets.index');
        Route::get('assets/create', [AssetController::class, 'create'])->name('assets.create');
        Route::get('assets/reports', [AssetController::class, 'reports'])->name('assets.reports');
        Route::post('assets', [AssetController::class, 'store'])->name('assets.store');
        Route::get('assets/{id}/edit', [AssetController::class, 'edit'])->name('assets.edit');
        Route::put('assets/{id}', [AssetController::class, 'update'])->name('assets.update');
        Route::get('assets/{id}/relocate', [AssetController::class, 'relocateForm'])->name('assets.relocate.form');
        Route::post('assets/{id}/relocate', [AssetController::class, 'relocate'])->name('assets.relocate');
        Route::get('assets/{id}/history', [AssetController::class, 'history'])->name('assets.history');
        Route::get('assets/{id}/label', [AssetController::class, 'label'])->name('assets.label');
        Route::delete('assets/{id}', [AssetController::class, 'destroy'])->name('assets.destroy');

        // Maintenance (Release 2)
        Route::get('maintenance', [MaintenanceController::class, 'index'])->name('maintenance.index');
        Route::get('maintenance/create', [MaintenanceController::class, 'create'])->name('maintenance.create');
        Route::post('maintenance', [MaintenanceController::class, 'store'])->name('maintenance.store');
        Route::get('maintenance/{id}', [MaintenanceController::class, 'show'])->whereNumber('id')->name('maintenance.show');
        Route::post('maintenance/{id}/transition', [MaintenanceController::class, 'transition'])->name('maintenance.transition');
        Route::post('maintenance/{id}/comment', [MaintenanceController::class, 'comment'])->name('maintenance.comment');
        Route::delete('maintenance/{id}', [MaintenanceController::class, 'destroy'])->name('maintenance.destroy');

        // Bookings (Release 2) — view/manage from the calendar
        Route::get('bookings', [BookingController::class, 'index'])->name('bookings.index');
        Route::get('bookings/create', [BookingController::class, 'create'])->name('bookings.create');
        Route::get('bookings/package-items/{id}', [BookingController::class, 'packageItems'])->whereNumber('id')->name('bookings.package-items');
        Route::post('bookings', [BookingController::class, 'store'])->name('bookings.store');
        Route::get('bookings/{id}', [BookingController::class, 'show'])->whereNumber('id')->name('bookings.show');
        Route::put('bookings/{id}', [BookingController::class, 'update'])->whereNumber('id')->name('bookings.update');
        // Booking lifecycle actions (delegate to shared services)
        Route::post('bookings/{id}/confirm', [BookingController::class, 'confirm'])->whereNumber('id')->name('bookings.confirm');
        Route::post('bookings/{id}/contract', [BookingController::class, 'contractBooking'])->whereNumber('id')->name('bookings.contract');
        Route::post('bookings/{id}/complete', [BookingController::class, 'complete'])->whereNumber('id')->name('bookings.complete');
        Route::post('bookings/{id}/payments', [BookingController::class, 'recordPayment'])->whereNumber('id')->name('bookings.pay');
        Route::post('bookings/{id}/documents/{type}', [BookingController::class, 'generateDocument'])->whereNumber('id')->name('bookings.document');
        Route::get('documents/{doc}/view/{lang?}', [BookingController::class, 'viewDocument'])->whereNumber('doc')->name('documents.view');

        // Contracts lifecycle (request → contract → invoice/payment)
        Route::get('contracts', [\Modules\Bms\Http\Controllers\ContractController::class, 'index'])->name('contracts.index');
        Route::get('contracts/{id}', [\Modules\Bms\Http\Controllers\ContractController::class, 'show'])->whereNumber('id')->name('contracts.show');
        Route::post('bookings/{id}/cancel', [BookingController::class, 'cancelBooking'])->whereNumber('id')->name('bookings.cancel');
        Route::post('bookings/{id}/reschedule', [BookingController::class, 'reschedule'])->whereNumber('id')->name('bookings.reschedule');

        // Quality-control runner (P1 counterpart of the bms-staff QC card).
        Route::get('bookings/{id}/qc', [\Modules\Bms\Http\Controllers\QcController::class, 'run'])->whereNumber('id')->name('qc.run');
        Route::post('qc/{id}/submit', [\Modules\Bms\Http\Controllers\QcController::class, 'submit'])->whereNumber('id')->name('qc.submit');
        Route::post('qc/{id}/voice', [\Modules\Bms\Http\Controllers\QcController::class, 'voice'])->whereNumber('id')->name('qc.voice');

        // Office visits
        Route::get('visits', [OfficeVisitController::class, 'index'])->name('visits.index');
        Route::get('visits/{id}', [OfficeVisitController::class, 'show'])->whereNumber('id')->name('visits.show');
        Route::put('visits/{id}', [OfficeVisitController::class, 'update'])->whereNumber('id')->name('visits.update');

        // Availability calendar
        Route::get('calendar', [CalendarController::class, 'index'])->name('calendar.index');
        Route::get('calendar/feed', [CalendarController::class, 'feed'])->name('calendar.feed');
        Route::post('calendar/block', [CalendarController::class, 'block'])->name('calendar.block');
        Route::delete('calendar/{id}', [CalendarController::class, 'release'])->name('calendar.release');
    });
