@extends('back.layout.pages-layout')
@section('pageTitle', isset($pageTitle) ? $pageTitle : 'Page Title here')
@section('content')
    <div class="container-fluid py-4">

        {{-- Überschrift, falls du sie brauchst --}}
        <div class="mb-4">
            <h1 class="h3 mb-0">{{ $pageTitle ?? 'Dashboard' }}</h1>
        </div>

        {{-- KPI-Karten: Tags / Posts / Subscriber --}}
        <div class="row g-3 mb-4">

            {{-- Tags (deine Zähl-Logik) --}}
            <div class="col-12 col-md-4">
                <div class="card h-100 shadow-sm">
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-start mb-2">
                            <small class="text-muted text-uppercase fw-semibold">Tags</small>
                            <span class="badge bg-primary-subtle text-primary border border-primary-subtle">
                                Stückanzahl
                            </span>
                        </div>

                        <h3 class="fw-semibold mb-1">
                            {{ $tagsCount ?? 0 }}
                        </h3>
                        <small class="text-muted">Gesamtzahl (laut Feld <code>tags</code>)</small>
                    </div>
                </div>
            </div>

            {{-- Posts --}}
            <div class="col-12 col-md-4">
                <div class="card h-100 shadow-sm">
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-start mb-2">
                            <small class="text-muted text-uppercase fw-semibold">Posts</small>
                            <span class="badge bg-success-subtle text-success border border-success-subtle">
                                Stückanzahl
                            </span>
                        </div>

                        <h3 class="fw-semibold mb-1">
                            {{ $postsCount ?? 0 }}
                        </h3>
                        <small class="text-muted">Gesamtzahl der Posts</small>
                    </div>
                </div>
            </div>

            {{-- Subscriber --}}
            <div class="col-12 col-md-4">
                <div class="card h-100 shadow-sm">
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-start mb-2">
                            <small class="text-muted text-uppercase fw-semibold">Subscriber</small>
                            <span class="badge bg-info-subtle text-info border border-info-subtle">
                                Stückanzahl
                            </span>
                        </div>

                        <h3 class="fw-semibold mb-1">
                            {{ $subscribersCount ?? 0 }}
                        </h3>
                        <small class="text-muted">Gesamtzahl der Abonnenten</small>
                    </div>
                </div>
            </div>
        </div>

        {{-- SEO / Content-Checks: KPIs --}}
        <div class="row g-3 mb-4">

            {{-- Posts ohne Meta-Beschreibung --}}
            <div class="col-12 col-md-4">
                <div class="card h-100 shadow-sm border-warning-subtle">
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-start mb-2">
                            <small class="text-muted text-uppercase fw-semibold">
                                Posts ohne Meta-Beschreibung
                            </small>
                            <span
                                class="badge
                                {{ ($postsWithoutMetaDescriptionCount ?? 0) > 0
                                    ? 'bg-warning-subtle text-warning border border-warning-subtle'
                                    : 'bg-success-subtle text-success border border-success-subtle' }}">
                                {{ ($postsWithoutMetaDescriptionCount ?? 0) > 0 ? 'Optimieren' : 'OK' }}
                            </span>
                        </div>

                        <h3 class="fw-semibold mb-1">
                            {{ $postsWithoutMetaDescriptionCount ?? 0 }}
                        </h3>
                        <small class="text-muted">Anzahl Beiträge ohne Meta-Description</small>
                    </div>
                </div>
            </div>

            {{-- Posts ohne Meta-Keywords --}}
            <div class="col-12 col-md-4">
                <div class="card h-100 shadow-sm border-warning-subtle">
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-start mb-2">
                            <small class="text-muted text-uppercase fw-semibold">
                                Posts ohne Meta-Keywords
                            </small>
                            <span
                                class="badge
                                {{ ($postsWithoutMetaKeywordsCount ?? 0) > 0
                                    ? 'bg-warning-subtle text-warning border border-warning-subtle'
                                    : 'bg-success-subtle text-success border border-success-subtle' }}">
                                {{ ($postsWithoutMetaKeywordsCount ?? 0) > 0 ? 'Optimieren' : 'OK' }}
                            </span>
                        </div>

                        <h3 class="fw-semibold mb-1">
                            {{ $postsWithoutMetaKeywordsCount ?? 0 }}
                        </h3>
                        <small class="text-muted">Anzahl Beiträge ohne Meta-Keywords</small>
                    </div>
                </div>
            </div>

            {{-- Posts ohne Tags --}}
            <div class="col-12 col-md-4">
                <div class="card h-100 shadow-sm border-warning-subtle">
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-start mb-2">
                            <small class="text-muted text-uppercase fw-semibold">
                                Posts ohne Tags
                            </small>
                            <span
                                class="badge
                                {{ ($postsWithoutTagsCount ?? 0) > 0
                                    ? 'bg-warning-subtle text-warning border border-warning-subtle'
                                    : 'bg-success-subtle text-success border border-success-subtle' }}">
                                {{ ($postsWithoutTagsCount ?? 0) > 0 ? 'Optimieren' : 'OK' }}
                            </span>
                        </div>

                        <h3 class="fw-semibold mb-1">
                            {{ $postsWithoutTagsCount ?? 0 }}
                        </h3>
                        <small class="text-muted">Beiträge ohne Eintrag im Feld <code>tags</code></small>
                    </div>
                </div>
            </div>
        </div>

        {{-- Letzte 5 Beiträge --}}
        <div class="card shadow-sm mb-4">
            <div class="card-header d-flex justify-content-between align-items-center">
                <h6 class="card-title mb-0">Letzte 5 Beiträge</h6>

                @if (Route::has('posts.index'))
                    <a href="{{ route('posts.index') }}" class="small text-decoration-none">
                        Alle anzeigen
                    </a>
                @endif
            </div>

            <div class="table-responsive">
                <table class="table table-sm align-middle mb-0">
                    <thead class="table-light">
                        <tr>
                            <th>Titel</th>
                            <th>Status</th>
                            <th>SEO</th>
                            <th>Erstellt am</th>
                            <th class="text-end">Aktionen</th>
                        </tr>
                    </thead>

                    <tbody>
                        @forelse($latestPosts as $post)
                            @php
                                $needsOptimization =
                                    empty($post->meta_keywords) || empty($post->tags) || empty($post->meta_description); // ggf. Feldname anpassen (z.B. meta_title)
                            @endphp
                            <tr>
                                <td>
                                    @if (Route::has('posts.edit'))
                                        <a href="{{ route('posts.edit', $post) }}" class="text-decoration-none">
                                            {{ $post->title }}
                                        </a>
                                    @else
                                        {{ $post->title }}
                                    @endif
                                </td>

                                <td>
                                    @if ($post->visibility == 1)
                                        <span class="badge bg-success-subtle text-success border border-success-subtle">
                                            Veröffentlicht
                                        </span>
                                    @else
                                        <span
                                            class="badge bg-secondary-subtle text-secondary border border-secondary-subtle">
                                            Entwurf
                                        </span>
                                    @endif
                                </td>

                                <td>
                                    @if ($needsOptimization)
                                        <span class="badge bg-warning-subtle text-warning border border-warning-subtle">
                                            Optimieren
                                        </span>
                                    @else
                                        <span class="badge bg-success-subtle text-success border border-success-subtle">
                                            OK
                                        </span>
                                    @endif
                                </td>
                                <td>
                                    <small class="text-muted">
                                        {{ $post->created_at->format('d.m.Y H:i') }}
                                    </small>
                                </td>
                                <td class="text-end">
                                    {{-- Anschauen --}}
                                    @if (Route::has('read_post'))
                                        <a href="{{ route('read_post', $post->slug) }}"
                                            class="btn btn-sm btn-outline-secondary">
                                            Anschauen
                                        </a>
                                    @endif

                                    {{-- Bearbeiten --}}
                                    @if (Route::has('admin.edit_post'))
                                        <a href="{{ route('admin.edit_post', $post) }}"
                                            class="btn btn-sm btn-primary ms-1">
                                            Bearbeiten
                                        </a>
                                    @endif
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="3" class="text-center text-muted py-4">
                                    Keine Beiträge vorhanden.
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>

        {{-- Detail-Listen: Posts ohne Meta / Tags --}}
        <div class="card shadow-sm">
            <div class="card-header">
                <h6 class="card-title mb-0">Beiträge mit Optimierungsbedarf</h6>
            </div>
            <div class="card-body">
                <div class="row g-3">

                    {{-- Ohne Meta-Beschreibung --}}
                    <div class="col-12 col-md-4">
                        <h6 class="small text-uppercase text-muted mb-2">
                            Ohne Meta-Beschreibung
                        </h6>
                        <ul class="list-unstyled mb-0 small">
                            @forelse($postsWithoutMetaDescription as $post)
                                <li class="mb-2">
                                    @if (Route::has('posts.edit'))
                                        <a href="{{ route('posts.edit', $post) }}" class="text-decoration-none">
                                            {{ $post->title }}
                                        </a>
                                    @else
                                        {{ $post->title }}
                                    @endif
                                    <br>
                                    <span class="text-muted">
                                        {{ $post->created_at->format('d.m.Y') }}
                                    </span>
                                </li>
                            @empty
                                <li class="text-muted">Alles erledigt.</li>
                            @endforelse
                        </ul>
                    </div>

                    {{-- Ohne Meta-Keywords --}}
                    <div class="col-12 col-md-4">
                        <h6 class="small text-uppercase text-muted mb-2">
                            Ohne Meta-Keywords
                        </h6>
                        <ul class="list-unstyled mb-0 small">
                            @forelse($postsWithoutMetaKeywords as $post)
                                <li class="mb-2">
                                    @if (Route::has('posts.edit'))
                                        <a href="{{ route('posts.edit', $post) }}" class="text-decoration-none">
                                            {{ $post->title }}
                                        </a>
                                    @else
                                        {{ $post->title }}
                                    @endif
                                    <br>
                                    <span class="text-muted">
                                        {{ $post->created_at->format('d.m.Y') }}
                                    </span>
                                </li>
                            @empty
                                <li class="text-muted">Alles erledigt.</li>
                            @endforelse
                        </ul>
                    </div>

                    {{-- Ohne Tags --}}
                    <div class="col-12 col-md-4">
                        <h6 class="small text-uppercase text-muted mb-2">
                            Ohne Tags
                        </h6>
                        <ul class="list-unstyled mb-0 small">
                            @forelse($postsWithoutTags as $post)
                                <li class="mb-2">
                                    @if (Route::has('posts.edit'))
                                        <a href="{{ route('posts.edit', $post) }}" class="text-decoration-none">
                                            {{ $post->title }}
                                        </a>
                                    @else
                                        {{ $post->title }}
                                    @endif
                                    <br>
                                    <span class="text-muted">
                                        {{ $post->created_at->format('d.m.Y') }}
                                    </span>
                                </li>
                            @empty
                                <li class="text-muted">Alles erledigt.</li>
                            @endforelse
                        </ul>
                    </div>

                </div>
            </div>
        </div>

    </div>
@endsection
