<?php

namespace App\Http\Controllers;

use App\Models\Post;
use App\Models\AdPlacement;
use Illuminate\Support\Facades\Route;
use Inertia\Inertia;

class HomeController extends Controller
{
    public function index()
    {
        // --- 'rssFeed' ---
        $heroSlider = Post::with(['user:id,name,avatar', 'category:id,name', 'rssFeed']) 
            ->where('status', 'published')
            ->whereNotNull('image_path')
            ->latest()
            ->take(5)
            ->get();

        $excludeIds = $heroSlider->pluck('id')->toArray();

        // --- Added 'rssFeed' ---
        $subHeroPosts = Post::with(['category:id,name', 'rssFeed']) 
            ->where('status', 'published')
            ->whereNotIn('id', $excludeIds)
            ->latest()
            ->take(4)
            ->get();

        $ads = AdPlacement::where('is_active', true)
            ->whereIn('placement_key', ['home_mobile_top', 'home_sidebar_top', 'home_sidebar_bottom'])
            ->get()
            ->keyBy('placement_key');

        return Inertia::render('Welcome', [
            'heroSlider' => $heroSlider,
            'subHeroPosts' => $subHeroPosts,
            'ads' => $ads,
            'canLogin' => Route::has('login'),
            'canRegister' => Route::has('register'),
        ]);
    }
}