<?php

use App\Http\Controllers\Page\AgenciesController;
use App\Http\Controllers\Page\AgencyCompanyProfileController;
use App\Http\Controllers\Page\AgencyRegistrationController;
use App\Http\Controllers\Page\AuthController;
use App\Http\Controllers\Page\ContactController;
use App\Http\Controllers\Page\HomeController;
use App\Http\Controllers\Page\PublicResourceScheduleController;
use Illuminate\Support\Facades\Route;

require __DIR__ . '/admin.php';
require __DIR__ . '/agency.php';

// Public kaynak (rehber/şoför) takvimi — auth gerektirmez, token ile erişilir
Route::get('/resource-schedule/{token}', [PublicResourceScheduleController::class, 'show'])->name('public.resource.schedule');

/*
|--------------------------------------------------------------------------
| Host-modu: müşteri domaininden gelen istekler (Faz 3)
|--------------------------------------------------------------------------
| İstek platform domaininden DEĞİLSE (özel domain ya da {sub}.platform.com),
| kök path'ler doğrudan acente sitesine gider; acente ResolveSiteHost
| middleware'i tarafından Host header'dan çözülür.
|
| NOT: Kayıt istek host'una göre koşullu yapıldığı için `route:cache`
| KULLANILMAMALIDIR (cache tek moda sabitler).
*/
$centralDomains = array_filter(array_map('trim', explode(',', (string) env('SITE_CENTRAL_DOMAINS', ''))));
$centralDomains = $centralDomains ?: array_filter([
    parse_url((string) config('app.url'), PHP_URL_HOST),
    '127.0.0.1',
    'localhost',
]);
$requestHost = preg_replace('/^www\./', '', strtolower(request()->getHost() ?: 'localhost'));

if (! app()->runningInConsole() && ! in_array($requestHost, $centralDomains, true)) {
    Route::middleware([\App\Http\Middleware\ResolveSiteHost::class])
        ->name('site.host.')
        ->group(function () {
            Route::get('/', [\App\Http\Controllers\Site\SitePageController::class, 'home'])->name('home');
            Route::get('/contact', [\App\Http\Controllers\Site\SitePageController::class, 'contact'])->name('contact');
            Route::get('/p/{pageSlug}', [\App\Http\Controllers\Site\SitePageController::class, 'custom'])->name('page');
            Route::get('/tours', [\App\Http\Controllers\Site\SiteTourController::class, 'index'])->name('tours.index');
            Route::get('/tours/search', [\App\Http\Controllers\Site\SiteTourController::class, 'search'])->name('tours.search');
            Route::get('/tours/{type}', [\App\Http\Controllers\Site\SiteTourController::class, 'list'])->name('tours.list');
            Route::get('/tours/{type}/{slug}', [\App\Http\Controllers\Site\SiteTourController::class, 'show'])->name('tours.show');
            Route::post('/forms/{formKey}', [\App\Http\Controllers\Site\SiteFormController::class, 'submit'])
                ->middleware('throttle:20,10')
                ->name('forms.submit');
        });
}

// Ana sayfa
Route::get('/', [HomeController::class, 'index'])->name('home');
Route::get('/agencies', [AgenciesController::class, 'index'])->name('customer.agencies');

// Customer Agency
Route::prefix('agency')->name('agency.')->group(function () {
    Route::get('register', [AgencyRegistrationController::class, 'showRegisterForm'])->name('register');
    Route::post('register', [AgencyRegistrationController::class, 'register']);
});

// Agency public site (Site Yönetimi v2 — "Voyage"). Eski route adları korunarak
// yeni Site controller'larına bağlandı; eski Page\AgencyPageController emekli.
Route::prefix('agency/{agency:slug}')->name('customer.agency.')->group(function () {
    Route::get('/', [\App\Http\Controllers\Site\SitePageController::class, 'home'])->name('show');
    // Herkese açık, link ile paylaşılabilir şirket bilgileri sayfası
    Route::get('/company-profile', [AgencyCompanyProfileController::class, 'show'])->name('company-profile');
    Route::get('/contact', [\App\Http\Controllers\Site\SitePageController::class, 'contact'])->name('contact');
    Route::get('/p/{pageSlug}', [\App\Http\Controllers\Site\SitePageController::class, 'custom'])->name('page');
    Route::get('/tours', [\App\Http\Controllers\Site\SiteTourController::class, 'index'])->name('tours.index');
    Route::get('/tours/search', [\App\Http\Controllers\Site\SiteTourController::class, 'search'])->name('tours.search');
    Route::get('/tours/{type}', [\App\Http\Controllers\Site\SiteTourController::class, 'list'])->name('tours.list');
    Route::get('/tours/{type}/{slug}', [\App\Http\Controllers\Site\SiteTourController::class, 'show'])->name('tours.detail');
    Route::post('/forms/{formKey}', [\App\Http\Controllers\Site\SiteFormController::class, 'submit'])
        ->middleware('throttle:20,10')
        ->name('forms.submit');

    // Ziyaretçi canlı desteği (Faz 4)
    Route::prefix('chat')->name('chat.')->middleware('throttle:120,1')->group(function () {
        Route::get('/status', [\App\Http\Controllers\Site\SiteChatController::class, 'status'])->name('status');
        Route::post('/start', [\App\Http\Controllers\Site\SiteChatController::class, 'start'])->name('start');
        Route::post('/send', [\App\Http\Controllers\Site\SiteChatController::class, 'send'])->name('send');
        Route::get('/messages', [\App\Http\Controllers\Site\SiteChatController::class, 'messages'])->name('messages');
    });
});

// Customer Auth Routes
Route::prefix('customer')->name('customer.')->group(function () {
    Route::middleware('guest')->group(function () {
        Route::get('register', [AuthController::class, 'showRegisterForm'])->name('register');
        Route::post('register', [AuthController::class, 'register']);
        Route::get('login', [AuthController::class, 'showLoginForm'])->name('login');
        Route::post('login', [AuthController::class, 'login']);
        Route::get('forgot-password', [AuthController::class, 'showForgotPasswordForm'])->name('password.request');
        Route::post('forgot-password', [AuthController::class, 'sendResetLink'])->name('password.email');
        Route::get('reset-password/{token}', [AuthController::class, 'showResetPasswordForm'])->name('password.reset');
        Route::post('reset-password', [AuthController::class, 'resetPassword'])->name('password.update');
    });

    Route::middleware('auth')->group(function () {
        Route::post('logout', [AuthController::class, 'logout'])->name('logout');
        Route::get('dashboard', function () {
            return view('customer.dashboard');
        })->name('dashboard');
    });
});

// Contact form
Route::post('/contact/submit', [ContactController::class, 'submit'])->name('customer.contact.submit');
