<?php

use App\Http\Controllers\ContactSubmissionController;
use App\Http\Controllers\QrCodeImageController;
use App\Http\Controllers\QrRedirectController;
use App\Http\Controllers\SubscriptionController;
use App\Http\Middleware\EnsureTeamMembership;
use App\Http\Middleware\EnsureUserIsAdmin;
use Illuminate\Support\Facades\Route;

// Public marketing site (Pushpvatika Florist). Registered ahead of the
// {current_team} wildcard group below so these clean paths are never
// swallowed as a team slug.
Route::view('/', 'pages.site.home')->name('home');
Route::view('/about', 'pages.site.about')->name('about');
Route::view('/services', 'pages.site.services')->name('services');
Route::view('/contact', 'pages.site.contact')->name('contact');
Route::view('/subscribe', 'pages.site.subscribe')->name('subscribe');
Route::view('/privacy', 'pages.site.privacy')->name('privacy');
Route::view('/terms', 'pages.site.terms')->name('terms');

Route::get('/sitemap.xml', function () {
    $urls = [
        ['route' => 'home', 'changefreq' => 'weekly', 'priority' => '1.0'],
        ['route' => 'subscribe', 'changefreq' => 'weekly', 'priority' => '0.9'],
        ['route' => 'services', 'changefreq' => 'weekly', 'priority' => '0.9'],
        ['route' => 'about', 'changefreq' => 'monthly', 'priority' => '0.8'],
        ['route' => 'contact', 'changefreq' => 'monthly', 'priority' => '0.7'],
        ['route' => 'terms', 'changefreq' => 'yearly', 'priority' => '0.3'],
        ['route' => 'privacy', 'changefreq' => 'yearly', 'priority' => '0.3'],
    ];

    // The XML declaration is prepended here rather than in the Blade view:
    // Blade cannot compile an echo whose contents open a PHP tag ("<?xml").
    $xml = '<?xml version="1.0" encoding="UTF-8"?>'."\n".view('sitemap', ['urls' => $urls])->render();

    return response($xml, 200, ['Content-Type' => 'application/xml']);
})->name('sitemap');

// Legacy .html URLs redirect permanently to the clean routes above (SEO).
Route::redirect('/index.html', '/', 301);
Route::redirect('/about.html', '/about', 301);
Route::redirect('/services.html', '/services', 301);
Route::redirect('/contact.html', '/contact', 301);
Route::redirect('/subscribe.html', '/subscribe', 301);
Route::redirect('/privacy.html', '/privacy', 301);
Route::redirect('/terms.html', '/terms', 301);

// Public form endpoints. The clean paths are canonical; the "*.php" aliases
// preserve the URLs the legacy static site posted to.
Route::post('/api/contact', ContactSubmissionController::class)->name('api.contact');
Route::post('/api/subscription', SubscriptionController::class)->name('api.subscription');
Route::post('/api/submit-contact.php', ContactSubmissionController::class);
Route::post('/api/submit-subscription.php', SubscriptionController::class);

// Public QR code redirect. Registered here (ahead of the {current_team}
// wildcard group below) so "/q/{short_code}" is never swallowed as a team
// slug. Throttled so a scraper can't inflate scan counts arbitrarily.
Route::get('/q/{qrCode}', QrRedirectController::class)
    ->middleware('throttle:120,1')
    ->name('qr.redirect');

// Staff area: replaces the legacy admin/*.php pages, which were guarded by a
// single shared password. Now gated by Fortify auth plus an is_admin check.
Route::prefix('admin')
    ->middleware(['auth', EnsureUserIsAdmin::class])
    ->group(function () {
        Route::redirect('/', '/admin/contacts');
        Route::livewire('contacts', 'pages::admin.contacts')->name('admin.contacts');
        Route::livewire('contacts/{contactSubmission}', 'pages::admin.contact-detail')->name('admin.contacts.show');
        Route::livewire('subscriptions', 'pages::admin.subscriptions')->name('admin.subscriptions');

        Route::livewire('qr-codes', 'pages::admin.qr-codes.index')->name('admin.qr-codes');
        Route::livewire('qr-codes/create', 'pages::admin.qr-codes.create')->name('admin.qr-codes.create');
        Route::livewire('qr-codes/{qrCode}', 'pages::admin.qr-codes.show')->name('admin.qr-codes.show');
        Route::get('qr-codes/{qrCode}/download/{format}', QrCodeImageController::class)->name('admin.qr-codes.download');
        Route::get('qr-codes/{qrCode}/preview', [QrCodeImageController::class, 'preview'])->name('admin.qr-codes.preview');
    });

Route::prefix('{current_team}')
    ->middleware(['auth', 'verified', EnsureTeamMembership::class])
    ->group(function () {
        Route::view('dashboard', 'dashboard')->name('dashboard');
    });

Route::middleware(['auth'])->group(function () {
    Route::livewire('invitations/{invitation}/accept', 'pages::teams.accept-invitation')->name('invitations.accept');
});

require __DIR__.'/settings.php';
