@extends('admin.layouts.app')

@section('title', 'Pages')
@section('subtitle', 'Manage homepage, about content, and future CMS pages from one list.')

@section('content')
    <div class="admin-card admin-library">
        <div class="admin-toolbar">
            <div>
                <p class="admin-section-title">Page library</p>
                <p class="admin-section-subtitle">{{ $pages->count() }} content entries currently available.</p>
            </div>
            <a href="{{ route('admin.pages.create') }}" class="admin-button">New page</a>
        </div>

        <div class="mt-6 admin-library__stack">
            @forelse($pages as $page)
                @php
                    $publicUrl = match ($page->slug) {
                        'home' => route('home'),
                        'about' => route('about'),
                        'contact' => route('contact'),
                        'blog' => route('blog'),
                        'services' => route('services.index'),
                        default => null,
                    };

                    $pageDescription = match ($page->template ?: 'standard') {
                        'home' => 'Homepage sections, hero content, pricing, and CTA widgets.',
                        'about' => 'Brand story, mission, expertise, and trust-building content.',
                        'blog' => 'Blog landing page sections and editorial intro blocks.',
                        'services' => 'Public services landing page heading, intro copy, and SEO fields.',
                        default => \Illuminate\Support\Str::limit(trim(strip_tags((string) $page->content)), 150) ?: 'Standard CMS page content ready for editing.',
                    };
                @endphp

                <article class="admin-library__item">
                    <div class="admin-library__content">
                        <div class="admin-library__heading">
                            <p class="admin-library__title">{{ $page->title }}</p>
                            <span class="admin-chip">{{ $page->template ?: 'standard' }}</span>
                            @if(in_array($page->slug, ['home', 'about', 'contact', 'blog', 'services'], true))
                                <span class="admin-chip">Public page</span>
                            @endif
                        </div>
                        <p class="admin-library__description">{{ $pageDescription }}</p>
                        <p class="admin-library__meta">
                            <span>/{{ $page->slug }}</span>
                            <span class="admin-library__meta-divider">&middot;</span>
                            <span>Updated {{ $page->updated_at->diffForHumans() }}</span>
                        </p>
                    </div>
                    <div class="admin-library__actions">
                        @if($publicUrl)
                            <a href="{{ $publicUrl }}" class="admin-button admin-button--ghost" target="_blank" rel="noopener">View</a>
                        @endif
                        <a href="{{ route('admin.pages.edit', $page) }}" class="admin-button admin-button--secondary">Edit</a>
                        @if($page->slug === 'services')
                            <a href="{{ route('admin.services.index') }}" class="admin-button admin-button--secondary">Manage services</a>
                        @endif
                        <form method="POST" action="{{ route('admin.pages.destroy', $page) }}" onsubmit="return confirm('Delete this page?')">
                            @csrf
                            @method('DELETE')
                            <button type="submit" class="admin-button admin-button--danger">Delete</button>
                        </form>
                    </div>
                </article>
            @empty
                <div class="admin-empty">No pages yet. Create the first CMS page to start building the site.</div>
            @endforelse
        </div>
    </div>
@endsection
