<?php

declare(strict_types=1);

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Services\HomepageService;
use App\Support\Seo\SeoFactory;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

final class HomeController extends Controller
{
    public function __construct(private readonly HomepageService $homepage) {}

    public function index(Request $request): Response
    {
        $user = $request->user();
        $sections = $user
            ? $this->homepage->allForUser($user)
            : array_merge($this->homepage->all(), ['is_logged_in' => false]);

        $hero = $sections['hero'];

        $meta = [
            'title' => 'Skinbase – Digital Art & Wallpapers',
            'description' => 'Discover stunning digital art, wallpapers, and skins from a global community of creators. Browse trending works, fresh uploads, and beloved classics.',
            'keywords' => 'wallpapers, digital art, skins, photography, community, wallpaper downloads',
            'og_image' => $hero['featured_image']['preload_url'] ?? $hero['thumb_lg'] ?? $hero['thumb'] ?? null,
            'canonical' => url('/'),
        ];

        $response = response()->view('web.home', [
            'seo' => app(SeoFactory::class)->homepage($meta)->toArray(),
            'useUnifiedSeo' => true,
            'meta' => $meta,
            'props' => $sections,
        ]);

        if ($user) {
            return $response->header('Cache-Control', 'private, no-store');
        }

        return $response->header('Cache-Control', 'public, max-age=60, s-maxage=300, stale-while-revalidate=600');
    }
}
