<?php

declare(strict_types=1);

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Session;
use Stancl\Tenancy\Middleware\PreventAccessFromCentralDomains;
use App\Http\Middleware\UniversalTenancy;

Route::middleware([
    'web',
])->group(function () {
    
    // Root route handles both Central and Tenant contexts
    Route::get('/', function () {
        $centralDomains = config('tenancy.central_domains');
        if (in_array(request()->getHost(), $centralDomains)) {
            return redirect('/admin/login');
        }
        return redirect()->route('admin.session.create');
    });

    // Protected Tenant Routes
    Route::middleware([
        PreventAccessFromCentralDomains::class,
    ])->group(function () {
        
        Route::get('/tenant-debug', function () {
            Session::put('tenant_key', 'tenant_value_' . now());
            Session::save();

            return [
                'context' => 'tenant',
                'session_id' => Session::getId(),
                'csrf_token' => csrf_token(),
                'session_config' => config('session'),
                'request_cookies' => request()->cookie(),
                'test_value' => Session::get('tenant_key'),
                'host' => request()->getHost(),
                'tenant_id' => tenant('id'),
                'locale' => app()->getLocale(),
                'core_config_locale' => core()->getConfigData('general.general.locale_settings.locale'),
            ];
        });

    });
});
