<?php

/**
 * Health Directory Marketing Platform Routes
 *
 * Include this file in your main web.php:
 * require_once __DIR__ . '/health-directory.php';
 *
 * Routes follow controller-based pattern (no closures) for route caching.
 * All routes use middleware for permission checks.
 *
 * Requirements: 10.4
 */

use App\Http\Controllers\HealthInstitutionController;
use App\Http\Controllers\DigitalMachineController;
use App\Http\Controllers\PaymentController;
use Illuminate\Support\Facades\Route;

// =========================================================================
// PUBLIC ROUTES (No Authentication Required)
// =========================================================================

// --- Health Institution Directory ---

// Public directory listing with search
Route::get('/institutions', [HealthInstitutionController::class, 'index'])
    ->name('institutions.index');

// Registration form (multi-step) — must be before {slug} to avoid "register" being treated as a slug
Route::get('/institutions/register', [HealthInstitutionController::class, 'register'])
    ->name('institutions.register');

// Process registration
Route::post('/institutions/register', [HealthInstitutionController::class, 'store'])
    ->name('institutions.register.store');

// Public institution detail page
Route::get('/institutions/{slug}', [HealthInstitutionController::class, 'show'])
    ->name('institutions.show');

// AJAX search for real-time results (legacy — used by navbar typeahead)
Route::get('/api/institutions/search', [HealthInstitutionController::class, 'ajaxSearch'])
    ->name('api.institutions.search');

// Full real-time search for home component (sources entire DB, no min-char limit)
Route::get('/api/institutions/filter', [HealthInstitutionController::class, 'search'])
    ->name('api.institutions.filter');


// --- Digital Machine Showcase ---

// Public machine showcase listing
Route::get('/machines', [DigitalMachineController::class, 'index'])
    ->name('machines.index');

// Public machine detail page
Route::get('/machines/{slug}', [DigitalMachineController::class, 'show'])
    ->name('machines.show');

// AJAX search for real-time dropdown results (legacy — used by navbar typeahead)
Route::get('/api/machines/search', [DigitalMachineController::class, 'ajaxSearch'])
    ->name('api.machines.search');

// Full real-time search for home component (sources entire DB, no min-char limit)
Route::get('/api/machines/filter', [DigitalMachineController::class, 'search'])
    ->name('api.machines.filter');


// --- Dynamic Publishing Pages ---

// Public page rendering by slug is registered in routes/web.php as 'pages.show'.
// Do NOT re-register it here to avoid duplicate named route definitions.
// Route::get('/pages/{slug}', [PublishingController::class, 'show'])->name('pages.show');


// =========================================================================
// AUTHENTICATED ROUTES
// =========================================================================

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

    // --- Health Marketor Dashboard ---

    Route::prefix('dashboard')->name('dashboard.')->group(function () {

        // Institution Management
        Route::get('/institutions', [HealthInstitutionController::class, 'dashboard'])
            ->name('institutions.index')
            ->middleware('permission:view institutions');

        Route::get('/institutions/create', [HealthInstitutionController::class, 'register'])
            ->name('institutions.create')
            ->middleware('permission:create institutions');

        Route::get('/institutions/{institution}/edit', [HealthInstitutionController::class, 'edit'])
            ->name('institutions.edit')
            ->middleware('permission:edit institutions');

        Route::put('/institutions/{institution}', [HealthInstitutionController::class, 'update'])
            ->name('institutions.update')
            ->middleware('permission:edit institutions');

        Route::get('/institutions/{institution}/payment', [HealthInstitutionController::class, 'payment'])
            ->name('institutions.payment')
            ->middleware('permission:view institutions');

        Route::get('/institutions/{institution}/renew', [HealthInstitutionController::class, 'renew'])
            ->name('institutions.renew')
            ->middleware('permission:edit institutions');

        // Machine Management
        Route::get('/machines', [DigitalMachineController::class, 'dashboard'])
            ->name('machines.index')
            ->middleware('permission:view machines');

        Route::get('/machines/create', [DigitalMachineController::class, 'create'])
            ->name('machines.create')
            ->middleware('permission:create machines');

        Route::post('/machines', [DigitalMachineController::class, 'store'])
            ->name('machines.store')
            ->middleware('permission:create machines');

        Route::get('/machines/{machine}/edit', [DigitalMachineController::class, 'edit'])
            ->name('machines.edit')
            ->middleware('permission:edit machines');

        Route::put('/machines/{machine}', [DigitalMachineController::class, 'update'])
            ->name('machines.update')
            ->middleware('permission:edit machines');

        Route::delete('/machines/{machine}', [DigitalMachineController::class, 'destroy'])
            ->name('machines.destroy')
            ->middleware('permission:delete machines');

        Route::get('/machines/{machine}/payment', [DigitalMachineController::class, 'payment'])
            ->name('machines.payment')
            ->middleware('permission:view machines');

        // Payment History
        Route::get('/payments', [PaymentController::class, 'history'])
            ->name('payments.index')
            ->middleware('permission:view payments');
    });


    // --- Payment Processing ---

    Route::prefix('payments')->name('payments.')->group(function () {

        Route::post('/institution/{institution}', [PaymentController::class, 'institutionPayment'])
            ->name('institution')
            ->middleware('permission:view institutions');

        Route::post('/machine/{machine}', [PaymentController::class, 'machinePayment'])
            ->name('machine')
            ->middleware('permission:view machines');

        Route::get('/status/{payment}', [PaymentController::class, 'status'])
            ->name('status');

        Route::get('/check/{payment}', [PaymentController::class, 'checkStatus'])
            ->name('check');
    });


    // =========================================================================
    // ADMIN ROUTES (Manager & Super Manager)
    // =========================================================================

    Route::middleware(['role:Manager|Super Manager'])->prefix('admin')->name('admin.')->group(function () {

        // --- Institution Administration ---

        Route::get('/institutions', [HealthInstitutionController::class, 'adminIndex'])
            ->name('institutions.index')
            ->middleware('permission:manage institutions');

        Route::post('/institutions/{institution}/toggle', [HealthInstitutionController::class, 'toggleStatus'])
            ->name('institutions.toggle')
            ->middleware('permission:manage institutions');

        Route::post('/institutions/{institution}/assign', [HealthInstitutionController::class, 'assignMarketor'])
            ->name('institutions.assign')
            ->middleware('role:Super Manager');

        Route::delete('/institutions/{institution}/terminate', [HealthInstitutionController::class, 'terminate'])
            ->name('institutions.terminate')
            ->middleware('role:Super Manager');

        Route::delete('/institutions/{institution}', [HealthInstitutionController::class, 'destroy'])
            ->name('institutions.destroy')
            ->middleware('permission:delete institutions');


        // --- Machine Administration ---

        Route::get('/machines', [DigitalMachineController::class, 'adminIndex'])
            ->name('machines.index')
            ->middleware('permission:manage machines');

        Route::post('/machines/{machine}/approve', [DigitalMachineController::class, 'approve'])
            ->name('machines.approve')
            ->middleware('permission:approve machines');

        Route::post('/machines/{machine}/reject', [DigitalMachineController::class, 'reject'])
            ->name('machines.reject')
            ->middleware('permission:approve machines');


        // --- Payment Administration ---

        Route::get('/payments', [PaymentController::class, 'adminIndex'])
            ->name('payments.index')
            ->middleware('permission:view payments');

        Route::get('/payments/configure', [PaymentController::class, 'configure'])
            ->name('payments.configure')
            ->middleware('permission:configure payments');

        Route::post('/payments/configure', [PaymentController::class, 'saveConfiguration'])
            ->name('payments.configure.save')
            ->middleware('permission:configure payments');

        Route::post('/payments/renewal-reminder', [PaymentController::class, 'sendRenewalReminder'])
            ->name('payments.renewal-reminder')
            ->middleware('permission:manage payments');


        // Publishing Page Administration is registered in routes/web.php under
        // prefix('admin/pages')->name('admin.pages.'). Do NOT re-register here
        // to avoid duplicate named route definitions (admin.pages.update, etc.).
    });
});
