<?php

namespace App\Http\Controllers;

use App\Models\Content;
use App\Models\SiteSetting;
use Illuminate\Http\Request;
use Illuminate\View\View;

class RessourcesController extends Controller
{
    public function index(Request $request): View
    {
        $settings = SiteSetting::allKeyed();
        $activeSection = $request->string('section')->toString();
        if (! in_array($activeSection, ['blog', 'guides', 'actualites'], true)) {
            $activeSection = 'blog';
        }

        $type = match ($activeSection) {
            'actualites' => Content::TYPE_NEWS,
            'guides' => Content::TYPE_GUIDE,
            default => Content::TYPE_BLOG,
        };
        $resourceItems = Content::query()
            ->published()
            ->ofType($type)
            ->ordered()
            ->paginate(10)
            ->withQueryString();

        return view('ressources', [
            'heroTitle' => $settings['ressources_hero_title'] ?? null,
            'heroSub' => $settings['ressources_hero_sub'] ?? null,
            'activeSection' => $activeSection,
            'resourceItems' => $resourceItems,
        ]);
    }

    public function show(Content $content): View
    {
        if (! $content->isPubliclyVisible()) {
            abort(404);
        }

        return view('content-show', [
            'content' => $content,
        ]);
    }
}
