<?php

use Modules\VetPortal\Http\Controllers\InstallController;
use Modules\VetPortal\Http\Controllers\VpSettingsController;
use Modules\VetPortal\Http\Controllers\VpAccountController;
use Modules\VetPortal\Http\Controllers\Portal\AuthController;
use Modules\VetPortal\Http\Controllers\Portal\DashboardController;
use Modules\VetPortal\Http\Controllers\Portal\PetController;
use Modules\VetPortal\Http\Controllers\Portal\AppointmentController;

// ── Install / Update routes ──────────────────────────────────────────────
Route::middleware(['web', 'auth'])->prefix('vet-portal')->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']);
});

// ── Admin (ERP staff) routes ─────────────────────────────────────────────
Route::group([
    'middleware' => ['web', 'auth', 'SetSessionData', 'language', 'timezone', 'AdminSidebarMenu', 'module:VetPortal'],
    'prefix'     => 'vet-portal',
], function () {
    Route::get('/settings',          [VpSettingsController::class, 'index'])->name('vp.settings.index');
    Route::post('/settings/save',    [VpSettingsController::class, 'save'])->name('vp.settings.save');

    Route::get('/accounts',                      [VpAccountController::class, 'index'])->name('vp.accounts.index');
    Route::get('/accounts/list',                 [VpAccountController::class, 'list'])->name('vp.accounts.list');
    Route::post('/accounts/store',               [VpAccountController::class, 'store'])->name('vp.accounts.store');
    Route::post('/accounts/{id}/reset-password', [VpAccountController::class, 'resetPassword'])->name('vp.accounts.reset-password');
    Route::post('/accounts/{id}/toggle',         [VpAccountController::class, 'toggle'])->name('vp.accounts.toggle');
    Route::delete('/accounts/{id}',              [VpAccountController::class, 'destroy'])->name('vp.accounts.destroy');
});

// ── Public pet-owner portal ──────────────────────────────────────────────
Route::middleware(['web'])->prefix('portal')->group(function () {
    Route::get('/login',  [AuthController::class, 'showLogin'])->name('vp.portal.login');
    Route::post('/login', [AuthController::class, 'login'])->name('vp.portal.login.post');
    Route::post('/logout',[AuthController::class, 'logout'])->name('vp.portal.logout');

    Route::middleware(['portal.auth'])->group(function () {
        Route::get('/',             [DashboardController::class, 'index'])->name('vp.portal.dashboard');
        Route::get('/pets',         [PetController::class, 'index'])->name('vp.portal.pets');
        Route::get('/pets/{id}',    [PetController::class, 'show'])->name('vp.portal.pet');
        Route::get('/appointments', [AppointmentController::class, 'index'])->name('vp.portal.appointments');
    });
});

// (Pet-owner PWA shell route now lives in Modules/PortalApi/Routes/web.php
// — it serves public/app/index.html with proper fallback. Don't add a
// /app/* route here.)
