<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Smart Sync Status</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 1400px;
            margin: 0 auto;
            background: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .header {
            border-bottom: 2px solid #007cba;
            padding-bottom: 20px;
            margin-bottom: 30px;
        }
        .header h1 {
            color: #007cba;
            margin: 0;
            font-size: 28px;
        }
        .timestamp {
            color: #666;
            font-size: 14px;
            margin-top: 5px;
        }
        .stats {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }
        .stat-card {
            background: #f8f9fa;
            padding: 20px;
            border-radius: 6px;
            border-left: 4px solid #007cba;
            text-align: center;
        }
        .stat-card h3 {
            margin: 0 0 10px 0;
            color: #495057;
            font-size: 14px;
            text-transform: uppercase;
            letter-spacing: 1px;
        }
        .stat-card .value {
            font-size: 32px;
            font-weight: bold;
            color: #007cba;
        }
        .section {
            margin-bottom: 40px;
        }
        .section h2 {
            color: #495057;
            border-bottom: 1px solid #dee2e6;
            padding-bottom: 10px;
            font-size: 20px;
            margin-bottom: 20px;
        }
        .table-container {
            overflow-x: auto;
            border: 1px solid #dee2e6;
            border-radius: 6px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            background: white;
        }
        th, td {
            padding: 12px;
            text-align: left;
            border-bottom: 1px solid #dee2e6;
            vertical-align: top;
        }
        th {
            background-color: #f8f9fa;
            font-weight: 600;
            color: #495057;
            position: sticky;
            top: 0;
        }
        tr:hover {
            background-color: #f8f9fa;
        }
        .priority-badge {
            display: inline-block;
            padding: 4px 8px;
            border-radius: 12px;
            font-size: 11px;
            font-weight: bold;
            text-transform: uppercase;
        }
        .priority-high {
            background-color: #f8d7da;
            color: #721c24;
        }
        .priority-normal {
            background-color: #d1ecf1;
            color: #0c5460;
        }
        .priority-low {
            background-color: #d4edda;
            color: #155724;
        }
        .action-badge {
            display: inline-block;
            padding: 4px 8px;
            border-radius: 12px;
            font-size: 11px;
            font-weight: bold;
            background-color: #e9ecef;
            color: #495057;
        }
        .metadata {
            background: #f8f9fa;
            padding: 8px;
            border-radius: 4px;
            font-family: 'Courier New', monospace;
            font-size: 11px;
            max-width: 300px;
            overflow: hidden;
            text-overflow: ellipsis;
        }
        .empty-state {
            text-align: center;
            padding: 40px;
            color: #6c757d;
        }
        .empty-state .icon {
            font-size: 48px;
            margin-bottom: 16px;
        }
        .actions {
            margin-top: 30px;
            padding-top: 20px;
            border-top: 1px solid #dee2e6;
        }
        .btn {
            display: inline-block;
            padding: 12px 24px;
            margin-right: 10px;
            margin-bottom: 10px;
            border: none;
            border-radius: 6px;
            text-decoration: none;
            font-weight: bold;
            cursor: pointer;
            transition: background-color 0.2s;
        }
        .btn-primary {
            background-color: #007cba;
            color: white;
        }
        .btn-primary:hover {
            background-color: #005a85;
        }
        .btn-secondary {
            background-color: #6c757d;
            color: white;
        }
        .btn-secondary:hover {
            background-color: #545b62;
        }
        .btn-success {
            background-color: #28a745;
            color: white;
        }
        .btn-success:hover {
            background-color: #218838;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>Smart Sync Status</h1>
            <div class="timestamp">Last updated: {{ $timestamp }}</div>
        </div>

        <div class="stats">
            <div class="stat-card">
                <h3>Pending Changes</h3>
                <div class="value">{{ count($pending_changes) }}</div>
            </div>
            <div class="stat-card">
                <h3>Model Changes</h3>
                <div class="value">{{ count($model_changes) }}</div>
            </div>
            <div class="stat-card">
                <h3>High Priority</h3>
                <div class="value">{{ $pending_changes->where('priority', 'high')->count() + $model_changes->where('priority', 'high')->count() }}</div>
            </div>
            <div class="stat-card">
                <h3>Total Pending</h3>
                <div class="value">{{ count($pending_changes) + count($model_changes) }}</div>
            </div>
        </div>

        <div class="section">
            <h2>Pending Membership Changes</h2>
            @if(count($pending_changes) > 0)
            <div class="table-container">
                <table>
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Priority</th>
                            <th>Action</th>
                            <th>User</th>
                            <th>Membership</th>
                            <th>Created</th>
                            <th>Metadata</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($pending_changes as $change)
                        <tr>
                            <td>{{ $change->id }}</td>
                            <td>
                                <span class="priority-badge priority-{{ $change->priority }}">
                                    {{ $change->priority }}
                                </span>
                            </td>
                            <td>
                                <span class="action-badge">{{ $change->action }}</span>
                            </td>
                            <td>
                                @if($change->user_id)
                                    {{ $change->user_id }}
                                @else
                                    <em>N/A</em>
                                @endif
                            </td>
                            <td>
                                @if($change->membership_id)
                                    {{ $change->membership_type }}: {{ $change->membership_id }}
                                @else
                                    <em>N/A</em>
                                @endif
                            </td>
                            <td>{{ $change->created_at->format('Y-m-d H:i:s') }}</td>
                            <td>
                                @if($change->metadata)
                                <div class="metadata">{{ substr(json_encode($change->metadata), 0, 100) }}{{ strlen(json_encode($change->metadata)) > 100 ? '...' : '' }}</div>
                                @else
                                <em>None</em>
                                @endif
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
            @else
            <div class="empty-state">
                <div class="icon">✓</div>
                <p>No pending membership changes</p>
            </div>
            @endif
        </div>

        <div class="section">
            <h2>Pending Model Changes</h2>
            @if(count($model_changes) > 0)
            <div class="table-container">
                <table>
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Priority</th>
                            <th>Action</th>
                            <th>Model Type</th>
                            <th>Model ID</th>
                            <th>Field</th>
                            <th>Created</th>
                            <th>Metadata</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($model_changes as $change)
                        <tr>
                            <td>{{ $change->id }}</td>
                            <td>
                                <span class="priority-badge priority-{{ $change->priority }}">
                                    {{ $change->priority }}
                                </span>
                            </td>
                            <td>
                                <span class="action-badge">{{ $change->action }}</span>
                            </td>
                            <td>{{ $change->model_type }}</td>
                            <td>{{ $change->model_id }}</td>
                            <td>{{ $change->field_name ?? 'N/A' }}</td>
                            <td>{{ $change->created_at->format('Y-m-d H:i:s') }}</td>
                            <td>
                                @if($change->metadata)
                                <div class="metadata">{{ substr(json_encode($change->metadata), 0, 100) }}{{ strlen(json_encode($change->metadata)) > 100 ? '...' : '' }}</div>
                                @else
                                <em>None</em>
                                @endif
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
            @else
            <div class="empty-state">
                <div class="icon">✓</div>
                <p>No pending model changes</p>
            </div>
            @endif
        </div>

        <div class="actions">
            <a href="{{ route('admin.smart-sync.trigger') }}" class="btn btn-success">Process All Changes</a>
            <a href="{{ route('admin.smart-sync.status') }}" class="btn btn-primary">Refresh Status</a>
            <a href="{{ url('/admin') }}" class="btn btn-secondary">Back to Admin</a>
        </div>
    </div>
</body>
</html>
