<?php

use App\Http\Controllers\SuperAdmin\TenantController;
use App\Http\Controllers\SuperAdmin\SessionController;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\DB;

Route::get('/debug-db', function () {
    return response()->json([
        'db' => DB::connection()->getDatabaseName(),
        'host' => request()->getHost(),
        'tenant' => tenant('id'),
    ]);
});

Route::get('/', function () {
    // Redirect central domain to Admin Login
    return redirect('/admin/login');
});

// Super Admin Routes
Route::group(['prefix' => 'super-admin', 'as' => 'super-admin.'], function () {
    // Session
    Route::get('login', [SessionController::class, 'create'])->name('session.create');
    Route::post('login', [SessionController::class, 'store'])->name('session.store');
    Route::delete('logout', [SessionController::class, 'destroy'])->name('session.destroy');

    // Protected Routes
    Route::group(['middleware' => ['super_admin']], function () {
        Route::get('tenants', [TenantController::class, 'index'])->name('tenants.index');
        Route::get('tenants/create', [TenantController::class, 'create'])->name('tenants.create');
        Route::post('tenants', [TenantController::class, 'store'])->name('tenants.store');
        Route::get('tenants/{id}/edit', [TenantController::class, 'edit'])->name('tenants.edit');
        Route::put('tenants/{id}', [TenantController::class, 'update'])->name('tenants.update');
        Route::delete('tenants/{id}', [TenantController::class, 'destroy'])->name('tenants.destroy');
    });
});

Route::get('/central-debug', function () {
    Session::put('central_key', 'central_value_' . now());
    Session::save();

    return [
        'context' => 'central',
        'session_id' => Session::getId(),
        'csrf_token' => csrf_token(),
        'session_config' => config('session'),
        'request_cookies' => request()->cookie(),
        'test_value' => Session::get('central_key'),
        'host' => request()->getHost(),
    ];
});
