<?php

namespace App\Http\Controllers;

use App\Models\Entry;
use App\Services\ActivityService;
use Artesaos\SEOTools\Facades\SEOTools;
use Illuminate\Http\Request;
use Illuminate\View\View;
use App\Models\News;

class HomeController extends Controller
{

    public function __construct( private ActivityService $service ) {}

    public function index( Request $request ): View {

        SEOTools::setTitle('A Romhack community where you can share your creations with the world.');

        $filters = [ 'entries', 'news', 'messages', 'threads', 'clubs', 'reviews' ];

        $cookie = $request->cookie('activity_filters');
        $activeFilters = $cookie ? array_intersect( json_decode( $cookie, true ) ?? [], $filters ) : $filters;

        if( empty( $activeFilters ) ) {
            $activeFilters = $filters;
        }

        $items = $this->service->getActivities( array_values( $activeFilters ) );

        $viewFilters = [
            'entries'  => ['label' => 'Entries',  'icon' => 'database'],
            'news'     => ['label' => 'News',     'icon' => 'newspaper'],
            'messages' => ['label' => 'Posts',    'icon' => 'message-square'],
            'threads'  => ['label' => 'Threads',  'icon' => 'messages-square'],
            'clubs'    => ['label' => 'Clubs',    'icon' => 'balloon'],
            'reviews'  => ['label' => 'Reviews',  'icon' => 'star'],
        ];

        $latestNews = News::published()->latest('created_at')->limit(5)->get();
        $featuredEntries = Entry::published()->where('featured', true)->latest('featured_at')->get();

        return view('home', compact('items', 'activeFilters', 'viewFilters', 'latestNews', 'featuredEntries'));
    }

}
