@extends('layouts.app')

@section('title', 'Admin Dashboard | SEF Solution')

@section('content')
<div class="admin-page">
    <div class="admin-card">
        <div class="admin-top">
            <div>
                <span class="brand-badge">SEF Solution</span>
                <h1>Admin Dashboard</h1>
                <p>Website chat monitoring, active session insight, lead form tracking</p>
            </div>
            <div class="top-actions">
                <a href="{{ route('admin.chats') }}" class="admin-link">All Chats</a>
                <a href="{{ route('admin.forms') }}" class="admin-link">Lead Forms</a>
                <a href="{{ route('admin.prompts') }}" class="admin-link">Prompt Library</a>
                <form method="POST" action="{{ route('admin.logout') }}">@csrf<button class="admin-link admin-link-btn">Logout</button></form>
            </div>
        </div>

        <div class="stat-grid">
            <div class="stat-box"><span>Total Chats</span><strong>{{ $stats['total_sessions'] }}</strong></div>
            <div class="stat-box"><span>Returning Users</span><strong>{{ $stats['returning_sessions'] }}</strong></div>
            <div class="stat-box"><span>Lead Forms</span><strong>{{ $stats['lead_forms'] }}</strong></div>
            <div class="stat-box"><span>Total Active Minutes</span><strong>{{ $stats['chat_minutes'] }}</strong></div>
            <div class="stat-box"><span>Active Prompts</span><strong>{{ $stats['active_prompts'] }}</strong></div>
        </div>

        <div class="panel-grid">
            <section class="panel-box">
                <div class="panel-title-row"><h3>Recent Chats</h3><a href="{{ route('admin.chats') }}">View all</a></div>
                <div class="table-wrap">
                    <table class="lead-table">
                        <thead><tr><th>Phone</th><th>Messages</th><th>Active</th><th>Date</th><th></th></tr></thead>
                        <tbody>
                        @forelse($recentChats as $chat)
                            <tr>
                                <td>{{ $chat->phone ?: 'N/A' }}</td>
                                <td>{{ $chat->messages_count }}</td>
                                <td>{{ $chat->active_seconds >= 60 ? ceil($chat->active_seconds / 60).'m' : $chat->active_seconds.'s' }}</td>
                                <td>{{ $chat->last_activity_at?->format('d M Y h:i A') }}</td>
                                <td><a href="{{ route('admin.chats.show', $chat) }}">Open</a></td>
                            </tr>
                        @empty
                            <tr><td colspan="5" class="empty-state">No chats yet.</td></tr>
                        @endforelse
                        </tbody>
                    </table>
                </div>
            </section>
            <section class="panel-box">
                <div class="panel-title-row"><h3>Recent Form Submissions</h3><a href="{{ route('admin.forms') }}">View all</a></div>
                <div class="table-wrap">
                    <table class="lead-table">
                        <thead><tr><th>Name</th><th>Phone</th><th>Business</th><th>Date</th></tr></thead>
                        <tbody>
                        @forelse($recentForms as $form)
                            <tr>
                                <td>{{ $form->name ?: 'N/A' }}</td>
                                <td>{{ $form->phone ?: ($form->session->phone ?? 'N/A') }}</td>
                                <td>{{ $form->business_name ?: ($form->business_type ?: 'N/A') }}</td>
                                <td>{{ $form->created_at?->format('d M Y h:i A') }}</td>
                            </tr>
                        @empty
                            <tr><td colspan="4" class="empty-state">No forms yet.</td></tr>
                        @endforelse
                        </tbody>
                    </table>
                </div>
            </section>
        </div>
    </div>
</div>
@endsection
