<?php

namespace App\Http\Controllers\Legacy;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Services\ArtworkService;

class HomeController extends Controller
{
    protected ArtworkService $artworks;

    public function __construct(ArtworkService $artworks)
    {
        $this->artworks = $artworks;
    }

    public function index(Request $request)
    {
        $page_title = 'Skinbase - Photography, Skins & Wallpapers';
        $page_meta_description = 'Skinbase legacy home, rendered via Laravel.';
        $page_meta_keywords = 'wallpapers, skins, photography, community';

        // Use new ArtworkService as primary data source
        $featuredResult = $this->artworks->getFeaturedArtworks(null, 39);
        // If service returned a paginator, extract the first model for the single "featured" slot
        if ($featuredResult instanceof \Illuminate\Pagination\LengthAwarePaginator) {
            $featured = $featuredResult->getCollection()->first();
        } elseif (is_array($featuredResult)) {
            $featured = $featuredResult[0] ?? null;
        } else {
            // Collection or single item
            $featured = method_exists($featuredResult, 'first') ? $featuredResult->first() : $featuredResult;
        }

        // Provide a memberFeatured fallback so the legacy view always has a value
        $memberFeatured = $featured;

        $latestUploads = $this->artworks->getLatestArtworks(20);

        // Legacy forum/news data not available in new services yet — provide empty defaults
        $forumNews = [];
        $ourNews = [];
        $latestForumActivity = [];

        return view('legacy::home', compact(
            'page_title',
            'page_meta_description',
            'page_meta_keywords',
            'featured',
            'memberFeatured',
            'latestUploads',
            'forumNews',
            'ourNews',
            'latestForumActivity'
        ));
    }
}
