<?php

use App\Http\Controllers\CareerController;
use App\Http\Controllers\ContactController;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\ModernizationController;
use App\Http\Controllers\PageController;
use App\Http\Controllers\PostController;
use App\Http\Controllers\ProjectController;
use App\Http\Controllers\PvConfiguratorController;
use App\Http\Controllers\ServiceConfiguratorController;
use App\Http\Controllers\ServiceController;
use App\Models\Job;
use App\Models\ModernizationPage;
use App\Models\Page;
use App\Models\Post;
use App\Models\Project;
use App\Models\Service;
use Illuminate\Support\Facades\Route;

Route::get('/', HomeController::class)->name('home');

Route::get('/leistungen', [ServiceController::class, 'index'])->name('services.index');
Route::get('/leistungen/{service}/einfach-erklaert', [ServiceController::class, 'simple'])->name('services.simple');
Route::get('/leistungen/{service}', [ServiceController::class, 'show'])->name('services.show');
Route::get('/intelligent-modernisieren', [ModernizationController::class, 'index'])->name('modernization.index');
Route::get('/intelligent-modernisieren/{modernizationPage}', [ModernizationController::class, 'show'])->name('modernization.show');
Route::get('/photovoltaik-planer', [PvConfiguratorController::class, 'create'])->name('pv-configurator.create');
Route::post('/photovoltaik-planer', [PvConfiguratorController::class, 'store'])->middleware('throttle:5,1')->name('pv-configurator.store');
Route::get('/photovoltaik-planer/vielen-dank', [PvConfiguratorController::class, 'success'])->name('pv-configurator.success');
Route::get('/projekt-planer/{service}', [ServiceConfiguratorController::class, 'create'])->name('service-configurator.create');
Route::post('/projekt-planer/{service}', [ServiceConfiguratorController::class, 'store'])->middleware('throttle:5,1')->name('service-configurator.store');
Route::get('/projekt-planer/{service}/vielen-dank', [ServiceConfiguratorController::class, 'success'])->name('service-configurator.success');

Route::get('/aktuelles', [PostController::class, 'index'])->name('posts.index');
Route::redirect('/aktuelles/intelligent-modernisieren', '/intelligent-modernisieren', 301);
Route::get('/aktuelles/{post}', [PostController::class, 'show'])->name('posts.show');

Route::get('/karriere', [CareerController::class, 'index'])->name('careers.index');
Route::get('/karriere/{job}', [CareerController::class, 'show'])->name('careers.show');

Route::get('/referenzen', [ProjectController::class, 'index'])->name('projects.index');
Route::get('/referenzen/{project}', [ProjectController::class, 'show'])->name('projects.show');

Route::get('/kontakt', [ContactController::class, 'create'])->name('contact');
Route::post('/kontakt', [ContactController::class, 'store'])->middleware('throttle:5,1')->name('contact.store');

Route::get('/unternehmen', fn () => app(PageController::class)->show(Page::where('slug', 'unternehmen')->firstOrFail()))->name('company');
Route::get('/datenschutz', fn () => app(PageController::class)->show(Page::where('slug', 'datenschutz')->firstOrFail()))->name('privacy');
Route::get('/impressum', fn () => app(PageController::class)->show(Page::where('slug', 'impressum')->firstOrFail()))->name('imprint');

Route::get('/sitemap.xml', function () {
    $urls = collect([
        ['loc' => route('home'), 'lastmod' => now()],
        ['loc' => route('services.index'), 'lastmod' => Service::max('updated_at')],
        ['loc' => route('modernization.index'), 'lastmod' => ModernizationPage::max('updated_at')],
        ['loc' => route('posts.index'), 'lastmod' => Post::max('updated_at')],
        ['loc' => route('careers.index'), 'lastmod' => Job::max('updated_at')],
        ['loc' => route('projects.index'), 'lastmod' => Project::max('updated_at')],
        ['loc' => route('contact'), 'lastmod' => now()],
        ['loc' => route('company'), 'lastmod' => Page::where('slug', 'unternehmen')->value('updated_at')],
        ['loc' => route('privacy'), 'lastmod' => Page::where('slug', 'datenschutz')->value('updated_at')],
        ['loc' => route('imprint'), 'lastmod' => Page::where('slug', 'impressum')->value('updated_at')],
    ])->merge(Service::published()->get()->map(fn ($service) => ['loc' => route('services.show', $service), 'lastmod' => $service->updated_at]))
        ->merge(ModernizationPage::published()->get()->map(fn ($page) => ['loc' => route('modernization.show', $page), 'lastmod' => $page->updated_at]))
        ->merge(Service::published()->whereNotNull('simple_explanation')->get()->map(fn ($service) => ['loc' => route('services.simple', $service), 'lastmod' => $service->updated_at]))
        ->merge(Post::published()->where('slug', '!=', 'intelligent-modernisieren')->get()->map(fn ($post) => ['loc' => route('posts.show', $post), 'lastmod' => $post->updated_at]))
        ->merge(Job::published()->get()->map(fn ($job) => ['loc' => route('careers.show', $job), 'lastmod' => $job->updated_at]))
        ->merge(Project::published()->get()->map(fn ($project) => ['loc' => route('projects.show', $project), 'lastmod' => $project->updated_at]));

    return response()->view('sitemap', compact('urls'))->header('Content-Type', 'application/xml');
})->name('sitemap');

Route::redirect('/schwerpunkte/photovoltaik.html', '/leistungen/photovoltaik', 301);
Route::redirect('/schwerpunkte/beleuchtung.html', '/leistungen/intelligente-beleuchtung', 301);
Route::redirect('/schwerpunkte/e-mobilitaet-wallbox.html', '/leistungen/wallbox-e-mobilitaet', 301);
Route::redirect('/schwerpunkte/stromspeicher.html', '/leistungen/stromspeicher', 301);
Route::redirect('/schwerpunkte/smart-home.html', '/leistungen/smart-home-knx', 301);
Route::redirect('/schwerpunkte/sicherheitstechnik.html', '/leistungen/sicherheitstechnik', 301);
Route::redirect('/schwerpunkte.html', '/leistungen', 301);
Route::get('/unsere-leistungen/intelligent-modernisieren.html', fn () => redirect()->route('modernization.index', status: 301));
Route::get('/unsere-leistungen/intelligent-modernisieren/{path}', [ModernizationController::class, 'legacy'])->where('path', '.*');
Route::redirect('/beratung.html', '/kontakt', 301);
Route::redirect('/kontakt.html', '/kontakt', 301);
Route::redirect('/home.html', '/', 301);
Route::redirect('/wir-ueber-uns.html', '/unternehmen', 301);
Route::redirect('/wir-ueber-uns/karriere.html', '/karriere', 301);
Route::redirect('/wir-ueber-uns/stellenangebote.html', '/karriere', 301);
Route::redirect('/wir-ueber-uns/referenzen.html', '/referenzen', 301);
Route::redirect('/wir-ueber-uns/partnerschaften.html', '/unternehmen', 301);
Route::redirect('/wir-ueber-uns/e-masters.html', '/unternehmen', 301);
Route::redirect('/wir-ueber-uns/bewertungen.html', '/unternehmen', 301);
Route::redirect('/wir-ueber-uns/nachrichten.html', '/aktuelles', 301);
Route::redirect('/wir-ueber-uns/nachrichten/nachrichten-detail/lueftung-mit-waermerueckgewinnung-3574.html', '/aktuelles/lueftung-mit-waermerueckgewinnung', 301);
Route::redirect('/wir-ueber-uns/nachrichten/nachrichten-detail/auf-natuerliche-kaeltemittel-umsteigen-und-vorteile-geniessen-3605.html', '/aktuelles/natuerliche-kaeltemittel', 301);
Route::redirect('/wir-ueber-uns/nachrichten/nachrichten-detail/e-check-im-eigenheim-4032.html', '/aktuelles/e-check-elektroanlage-sicher-pruefen', 301);
Route::redirect('/wir-ueber-uns/nachrichten/nachrichten-detail/elektroanschluesse-im-aussenbereich-3764.html', '/aktuelles/elektroanschluesse-im-aussenbereich', 301);
Route::redirect('/wir-ueber-uns/nachrichten/nachrichten-detail/ein-verlaesslicher-elektro-partner-an-ihrer-seite-3697.html', '/aktuelles/verlaesslicher-elektro-partner', 301);
Route::redirect('/wir-ueber-uns/nachrichten/nachrichten-detail/heizen-der-zukunft-mit-strom-3719.html', '/aktuelles/heizen-der-zukunft-mit-strom', 301);
Route::redirect('/wir-ueber-uns/nachrichten/nachrichten-detail/ueber-30-jahre-rauchwarnmelder-wie-hilfreich-sind-sie-3921.html', '/aktuelles/rauch-gas-co-wasser-gefahrenmelder', 301);
Route::redirect('/wir-ueber-uns/nachrichten/nachrichten-detail/smart-meter-das-intelligente-messsystem-fuer-ihren-stromverbrauch-3843.html', '/aktuelles/smart-meter-stromverbrauch-verstehen', 301);
Route::redirect('/wir-ueber-uns/nachrichten/nachrichten-detail/energiespar-tipps-im-haushalt-3669.html', '/aktuelles/energiemanagement-verbrauch-verstehen', 301);
Route::redirect('/wir-ueber-uns/nachrichten/nachrichten-detail/so-senken-sie-ihre-energiekosten-ohne-verzicht-3550.html', '/aktuelles/energiemanagement-verbrauch-verstehen', 301);
Route::redirect('/wir-ueber-uns/nachrichten/nachrichten-detail/wallbox-der-sichere-strom-fuer-ihr-e-auto-3532.html', '/aktuelles/wallbox-sicher-intelligent-laden', 301);
Route::redirect('/wir-ueber-uns/nachrichten/nachrichten-detail/lithium-ionen-batterien-4027.html', '/aktuelles/lithium-ionen-speicher-verstehen', 301);
Route::redirect('/wir-ueber-uns/nachrichten/nachrichten-detail/diese-smart-home-trends-erobern-die-eigenheime-3744.html', '/aktuelles/smart-home-trends-eigenheim', 301);
Route::redirect('/wir-ueber-uns/nachrichten/nachrichten-detail/smart-home-in-mietwohnungen-nachruesten-was-ist-erlaubt-3515.html', '/aktuelles/smart-home-im-bestand-nachruesten', 301);
Route::redirect('/wir-ueber-uns/nachrichten/nachrichten-detail/so-finden-sie-die-richtige-gluehlampe-fuer-die-leuchte-3499.html', '/aktuelles/led-licht-richtig-planen', 301);
Route::redirect('/wir-ueber-uns/nachrichten/nachrichten-detail/led-beleuchtung-zur-insektenabwehr-3806.html', '/aktuelles/led-beleuchtung-zur-insektenabwehr', 301);
Route::redirect('/wir-ueber-uns/nachrichten/nachrichten-detail/{any}', '/aktuelles', 301)->where('any', '.*');
Route::redirect('/datenschutz.html', '/datenschutz', 301);
Route::redirect('/impressum.html', '/impressum', 301);
Route::redirect('/suche.html', '/', 301);
