<?php

use Illuminate\Support\Facades\Route;
use Modules\VetSite\Http\Controllers\InstallController;
use Modules\VetSite\Http\Controllers\Admin\SiteController;
use Modules\VetSite\Http\Controllers\Admin\ThemeController;
use Modules\VetSite\Http\Controllers\Admin\PageController;
use Modules\VetSite\Http\Controllers\Admin\MediaController;
use Modules\VetSite\Http\Controllers\Admin\MessageController;
use Modules\VetSite\Http\Controllers\Admin\DomainController;

// ── Install / Update routes ──────────────────────────────────────────────
Route::middleware(['web', 'auth'])->prefix('vet-site')->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) CMS routes ─────────────────────────────────────────
Route::group([
    'middleware' => ['web', 'auth', 'SetSessionData', 'language', 'timezone', 'AdminSidebarMenu', 'module:VetSite'],
    'prefix'     => 'vet-site',
], function () {
    Route::get('/',                 [SiteController::class, 'dashboard'])->name('vs.dashboard');

    Route::get('/settings',         [SiteController::class, 'settings'])->name('vs.settings');
    Route::post('/settings',        [SiteController::class, 'saveSettings'])->name('vs.settings.save');

    Route::get('/theme',            [ThemeController::class, 'index'])->name('vs.theme');
    Route::post('/theme/apply',     [ThemeController::class, 'apply'])->name('vs.theme.apply');
    Route::post('/theme/customize', [ThemeController::class, 'customize'])->name('vs.theme.customize');

    Route::get('/navigation',       [SiteController::class, 'navigation'])->name('vs.navigation');
    Route::post('/navigation',      [SiteController::class, 'saveNavigation'])->name('vs.navigation.save');

    // Pages + block builder
    Route::get('/pages',                 [PageController::class, 'index'])->name('vs.pages');
    Route::post('/pages',                [PageController::class, 'store'])->name('vs.pages.store');
    Route::get('/pages/{id}/edit',       [PageController::class, 'edit'])->name('vs.pages.edit');
    Route::post('/pages/{id}',           [PageController::class, 'update'])->name('vs.pages.update');
    Route::post('/pages/{id}/blocks',    [PageController::class, 'saveBlocks'])->name('vs.pages.blocks');
    Route::post('/pages/{id}/home',      [PageController::class, 'setHome'])->name('vs.pages.home');
    Route::delete('/pages/{id}',         [PageController::class, 'destroy'])->name('vs.pages.destroy');

    // Media library
    Route::get('/media',            [MediaController::class, 'index'])->name('vs.media');
    Route::post('/media',           [MediaController::class, 'upload'])->name('vs.media.upload');
    Route::delete('/media/{id}',    [MediaController::class, 'destroy'])->name('vs.media.destroy');

    // Messages / leads
    Route::get('/messages',             [MessageController::class, 'index'])->name('vs.messages');
    Route::get('/messages/{id}',        [MessageController::class, 'show'])->name('vs.messages.show');
    Route::post('/messages/{id}/read',  [MessageController::class, 'toggleRead'])->name('vs.messages.read');
    Route::delete('/messages/{id}',     [MessageController::class, 'destroy'])->name('vs.messages.destroy');

    // Domain & SEO
    Route::get('/domain',           [DomainController::class, 'index'])->name('vs.domain');
    Route::post('/domain',          [DomainController::class, 'save'])->name('vs.domain.save');

    // Live preview of the public site (admin, host-independent — handy on
    // localhost and for the "Preview" buttons in the CMS).
    Route::get('/preview/{pageId?}', [SiteController::class, 'preview'])->name('vs.preview');
});
