<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\View\View;

class WelcomeController extends Controller
{
    /**
     * Display the homepage with all necessary data
     */
    public function index(Request $request): View
    {
        // Get active theme
        $theme = \App\Models\HomepageTheme::getActiveTheme();
        
        // Get active layout
        $layout = \App\Models\HomepageLayout::getActiveLayout();
        
        // Get active widgets (cached)
        // Note: content_with_defaults accessor is automatically loaded when accessed in views
        $widgets = \App\Models\HomepageWidget::active()->ordered()->get();
        
        // Determine rendering mode
        $useWidgetSystem = false;
        if ($layout && $layout->use_widget_system) {
            $useWidgetSystem = true;
        } elseif (!$layout && $widgets->isNotEmpty()) {
            $useWidgetSystem = true;
        }
        
        return view('welcome', compact(
            'layout',
            'theme',
            'widgets',
            'useWidgetSystem'
        ));
    }
}
