<?php

namespace App\Http\Controllers;

use App\Models\CustomPage;
use Inertia\Inertia;
use Illuminate\Support\Str;

class PublicPageController extends Controller
{
    public function show($slug)
    {
        $page = CustomPage::where('slug', $slug)->where('is_active', true)->firstOrFail();

        $seoDesc = strip_tags(html_entity_decode($page->content));
        $seoDesc = Str::limit(trim(preg_replace('/\s+/', ' ', $seoDesc)), 155, '...');

        return Inertia::render('CustomPage', [
            'page' => $page,
            'seo' => [
                'title' => $page->title,
                'description' => $seoDesc ?: $page->title,
                'url' => url()->current()
            ]
        ]);
    }
}