@extends('layouts.panel')

@section('title', $website->name)

@section('content')
    <section class="hero">
        <div class="footer-actions">
            <div>
                <p class="eyebrow">Website Detail</p>
                <h1>{{ $website->name }}</h1>
                <p class="muted">{{ $website->domain }} | {{ $website->api_endpoint }}</p>
            </div>

            <div class="button-row">
                <form method="post" action="{{ route('websites.sync', $website) }}">
                    @csrf
                    <button class="button secondary" type="submit">Sync Agent</button>
                </form>

                <form method="post" action="{{ route('backups.store', $website) }}">
                    @csrf
                    <button class="button ghost" type="submit">Create Backup</button>
                </form>

                <a class="button primary" href="{{ route('websites.edit', $website) }}">Edit Website</a>
            </div>
        </div>

        <div class="stat-strip">
            <div class="stat-pill">
                <p class="eyebrow">Status</p>
                <x-status-badge :value="$website->status" />
            </div>
            <div class="stat-pill">
                <p class="eyebrow">Health</p>
                <x-status-badge :value="$website->health_status" />
            </div>
            <div class="stat-pill">
                <p class="eyebrow">Databases</p>
                <strong>{{ $website->database_count }}</strong>
            </div>
            <div class="stat-pill">
                <p class="eyebrow">Tracked Tables</p>
                <strong>{{ $website->table_count }}</strong>
            </div>
            <div class="stat-pill">
                <p class="eyebrow">Connection</p>
                <strong>{{ str($website->connection_type)->replace('_', ' ')->headline() }}</strong>
            </div>
        </div>
    </section>

    <nav class="tab-nav">
        @foreach (['overview', 'database', 'backups', 'logs', 'agent status', 'settings'] as $tab)
            @php($slug = str($tab)->slug()->toString())
            <a class="{{ $activeTab === $slug ? 'active' : '' }}" href="{{ route('websites.show', ['website' => $website, 'tab' => $slug]) }}">
                {{ str($tab)->headline() }}
            </a>
        @endforeach
    </nav>

    @if ($activeTab === 'overview')
        <section class="detail-grid">
            <article class="card">
                <p class="eyebrow">Overview</p>
                <div class="summary-list">
                    <div class="summary-row"><span>Last Seen</span><strong>{{ $website->last_seen_at?->diffForHumans() ?? 'Never' }}</strong></div>
                    <div class="summary-row"><span>Last Synced</span><strong>{{ $website->last_synced_at?->diffForHumans() ?? 'Never' }}</strong></div>
                    <div class="summary-row"><span>Response Time</span><strong>{{ $website->response_time_ms ? $website->response_time_ms.' ms' : 'N/A' }}</strong></div>
                    <div class="summary-row"><span>Agent Version</span><strong>{{ $website->agent?->version ?? 'Pending' }}</strong></div>
                    <div class="summary-row"><span>Connection Type</span><strong>{{ str($website->connection_type)->replace('_', ' ')->headline() }}</strong></div>
                </div>
            </article>

            <article class="card">
                <p class="eyebrow">Latest Health Check</p>
                @if ($website->siteHealthLogs->isNotEmpty())
                    <div class="summary-list">
                        <div class="summary-row"><span>Status</span><x-status-badge :value="$website->siteHealthLogs->first()->status" /></div>
                        <div class="summary-row"><span>Health</span><x-status-badge :value="$website->siteHealthLogs->first()->health_status" /></div>
                        <div class="summary-row"><span>Checked</span><strong>{{ $website->siteHealthLogs->first()->checked_at?->diffForHumans() ?? 'N/A' }}</strong></div>
                        <div class="summary-row"><span>Message</span><strong>{{ $website->siteHealthLogs->first()->message ?? 'Health check completed.' }}</strong></div>
                    </div>
                @else
                    <p class="muted">No health checks have been recorded yet.</p>
                @endif
            </article>
        </section>

        <section class="card">
            <div class="page-header">
                <p class="eyebrow">Recent Backup</p>
                <h1>Latest backup state</h1>
            </div>

            @if ($recentBackup)
                <div class="summary-list">
                    <div class="summary-row"><span>Status</span><x-status-badge :value="$recentBackup->status" /></div>
                    <div class="summary-row"><span>Type</span><strong>{{ str($recentBackup->type)->headline() }}</strong></div>
                    <div class="summary-row"><span>Started</span><strong>{{ $recentBackup->started_at?->diffForHumans() ?? 'N/A' }}</strong></div>
                    <div class="summary-row"><span>Restore</span><strong>Deferred to Phase 2</strong></div>
                </div>
            @else
                <p class="muted">No backups recorded for this website yet.</p>
            @endif
        </section>

        <section class="card">
            <div class="page-header">
                <p class="eyebrow">Recent Logs</p>
                <h1>Latest activity for {{ $website->name }}</h1>
            </div>

            <div class="table-wrap">
                <table>
                    <thead>
                        <tr>
                            <th>Action</th>
                            <th>User</th>
                            <th>When</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse ($recentLogs as $log)
                            <tr>
                                <td>
                                    <strong>{{ str($log->action)->headline() }}</strong>
                                    <div class="muted">{{ $log->description }}</div>
                                </td>
                                <td>{{ $log->user?->name ?? 'System' }}</td>
                                <td>{{ $log->occurred_at->diffForHumans() }}</td>
                            </tr>
                        @empty
                            <tr><td colspan="3" class="muted">No website activity yet.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </section>
    @elseif ($activeTab === 'database')
        <section class="two-column">
            <article class="card">
                <div class="page-header">
                    <p class="eyebrow">Database Explorer</p>
                    <h1>Cached table metadata</h1>
                    <p class="muted">Phase 1 keeps this area read-only. Live SQL tools remain deferred while agent sync continues to cache table metadata.</p>
                </div>

                <div class="table-wrap">
                    <table>
                        <thead>
                            <tr>
                                <th>Table</th>
                                <th>Rows</th>
                                <th>Size</th>
                                <th>Last Updated</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse ($tableSnapshots as $snapshot)
                                <tr>
                                    <td>{{ $snapshot->table_name }}</td>
                                    <td>{{ number_format($snapshot->row_count) }}</td>
                                    <td>{{ \Illuminate\Support\Number::fileSize($snapshot->size_bytes) }}</td>
                                    <td>{{ $snapshot->last_updated_at?->diffForHumans() ?? 'N/A' }}</td>
                                </tr>
                            @empty
                                <tr><td colspan="4" class="muted">No table snapshots have been synced yet.</td></tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>

                {{ $tableSnapshots?->links() }}
            </article>

            <article class="card">
                <p class="eyebrow">Deferred Modules</p>
                <h1>Advanced tooling is intentionally paused.</h1>
                <p class="muted">File Manager, SQL Console, Backup Restore, Queue Monitor, and AI-powered tools are not part of Phase 1.</p>
                <div class="summary-list">
                    @foreach (['File Manager', 'SQL Console', 'Backup Restore', 'Queue Monitor', 'AI Modules'] as $module)
                        <div class="summary-row">
                            <span>{{ $module }}</span>
                            <x-status-badge value="deferred" />
                        </div>
                    @endforeach
                </div>
            </article>
        </section>
    @elseif ($activeTab === 'backups')
        <section class="card">
            <div class="page-header">
                <p class="eyebrow">Backup Center</p>
                <h1>Backup history</h1>
            </div>

            <div class="table-wrap">
                <table>
                    <thead>
                        <tr>
                            <th>Status</th>
                            <th>Type</th>
                            <th>Size</th>
                            <th>Started</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse ($backups as $backup)
                            <tr>
                                <td><x-status-badge :value="$backup->status" /></td>
                                <td>{{ str($backup->type)->headline() }}</td>
                                <td>{{ $backup->size_bytes ? \Illuminate\Support\Number::fileSize($backup->size_bytes) : 'Pending' }}</td>
                                <td>{{ $backup->started_at?->diffForHumans() ?? 'N/A' }}</td>
                                <td>
                                    <div class="inline-actions">
                                        @if ($backup->file_path)
                                            <a class="button ghost" href="{{ route('backups.download', $backup) }}">Download</a>
                                        @endif
                                        <form class="inline-form" method="post" action="{{ route('backups.destroy', $backup) }}" onsubmit="return confirm('Delete this backup record?')">
                                            @csrf
                                            @method('delete')
                                            <button class="button danger" type="submit">Delete</button>
                                        </form>
                                    </div>
                                    <div class="muted" style="margin-top: 0.5rem;">Restore is deferred to Phase 2.</div>
                                </td>
                            </tr>
                        @empty
                            <tr><td colspan="5" class="muted">No backups recorded.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            {{ $backups?->links() }}
        </section>
    @elseif ($activeTab === 'logs')
        <section class="card">
            <div class="table-wrap">
                <table>
                    <thead>
                        <tr>
                            <th>Action</th>
                            <th>User</th>
                            <th>IP</th>
                            <th>When</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse ($activityLogs as $log)
                            <tr>
                                <td>
                                    <strong>{{ str($log->action)->headline() }}</strong>
                                    <div class="muted">{{ $log->description }}</div>
                                </td>
                                <td>{{ $log->user?->name ?? 'System' }}</td>
                                <td>{{ $log->ip_address ?? 'N/A' }}</td>
                                <td>{{ $log->occurred_at->format('M d, Y H:i') }}</td>
                            </tr>
                        @empty
                            <tr><td colspan="4" class="muted">No activity logs yet.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            {{ $activityLogs?->links() }}
        </section>
    @elseif ($activeTab === 'agent-status')
        <section class="detail-grid">
            <article class="card">
                <p class="eyebrow">Agent Dashboard</p>
                <div class="summary-list">
                    <div class="summary-row"><span>Version</span><strong>{{ $website->agent?->version ?? 'Pending' }}</strong></div>
                    <div class="summary-row"><span>Status</span><x-status-badge :value="$website->agent?->status ?? 'pending'" /></div>
                    <div class="summary-row"><span>Last Heartbeat</span><strong>{{ $website->agent?->last_heartbeat_at?->diffForHumans() ?? 'Never' }}</strong></div>
                    <div class="summary-row"><span>Latency</span><strong>{{ $website->agent?->latency_ms ? $website->agent->latency_ms.' ms' : 'N/A' }}</strong></div>
                    <div class="summary-row"><span>Authentication</span><x-status-badge :value="$website->agent?->authentication_status ?? 'pending'" /></div>
                    <div class="summary-row"><span>Sync</span><x-status-badge :value="$website->agent?->sync_status ?? 'pending'" /></div>
                </div>
            </article>

            <article class="card">
                <p class="eyebrow">Capabilities</p>
                <div class="summary-list">
                    @forelse ($website->agent?->capabilities ?? [] as $capability)
                        <div class="summary-row">
                            <span>{{ str($capability)->replace('-', ' ')->headline() }}</span>
                            <x-status-badge value="ready" />
                        </div>
                    @empty
                        <p class="muted">No capability payload received yet.</p>
                    @endforelse
                </div>
            </article>

            <article class="card" style="grid-column: 1 / -1;">
                <div class="page-header">
                    <p class="eyebrow">API Agent Logs</p>
                    <h1>Latest agent communication</h1>
                </div>

                <div class="table-wrap">
                    <table>
                        <thead>
                            <tr>
                                <th>Method</th>
                                <th>Endpoint</th>
                                <th>Status</th>
                                <th>Latency</th>
                                <th>When</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse ($website->apiAgentLogs as $log)
                                <tr>
                                    <td>{{ $log->method }}</td>
                                    <td>
                                        <strong>{{ $log->message }}</strong>
                                        <div class="muted">{{ $log->endpoint }}</div>
                                    </td>
                                    <td>
                                        <x-status-badge :value="$log->was_successful ? 'success' : 'failed'" />
                                        <div class="muted">{{ $log->http_status ? 'HTTP '.$log->http_status : 'No response code' }}</div>
                                    </td>
                                    <td>{{ $log->latency_ms ? $log->latency_ms.' ms' : 'N/A' }}</td>
                                    <td>{{ $log->logged_at?->diffForHumans() ?? 'N/A' }}</td>
                                </tr>
                            @empty
                                <tr><td colspan="5" class="muted">No API agent calls have been logged yet.</td></tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </article>
        </section>
    @else
        <section class="detail-grid">
            <article class="card">
                <p class="eyebrow">Connection Settings</p>
                <div class="summary-list">
                    <div class="summary-row"><span>Connection Type</span><strong>{{ str($website->connection_type)->replace('_', ' ')->headline() }}</strong></div>
                    <div class="summary-row"><span>API Endpoint</span><strong>{{ $website->api_endpoint }}</strong></div>
                    <div class="summary-row"><span>Encrypted API Key</span><strong>Stored securely</strong></div>
                    <div class="summary-row"><span>Signing Secret</span><strong>{{ $website->signing_secret ? 'Configured' : 'Not set' }}</strong></div>
                    <div class="summary-row"><span>Description</span><strong>{{ $website->description ?: 'None provided' }}</strong></div>
                </div>
            </article>

            <article class="card">
                <p class="eyebrow">Phase Boundary</p>
                <div class="summary-list">
                    @foreach (['File Manager', 'SQL Console', 'Backup Restore', 'Queue Monitor', 'AI Modules'] as $module)
                        <div class="summary-row">
                            <span>{{ $module }}</span>
                            <x-status-badge value="deferred" />
                        </div>
                    @endforeach
                </div>
            </article>
        </section>
    @endif
@endsection
