<div class="content-area container-xl">
    <div class="page-title">
        <h2><i class="bi bi-bar-chart"></i> My Reports</h2>
    </div>

    <div class="row mb-4">
        <div class="col-md-4">
            <div class="stat-card primary">
                <h6 class="text-muted">Total Messages</h6>
                <h3><?= number_format($activity_stats->total ?? 0) ?></h3>
            </div>
        </div>
        <div class="col-md-4">
            <div class="stat-card success">
                <h6 class="text-muted">Successful</h6>
                <h3 class="text-success"><?= number_format($activity_stats->sent ?? 0) ?></h3>
            </div>
        </div>
        <div class="col-md-4">
            <div class="stat-card danger">
                <h6 class="text-muted">Failed</h6>
                <h3 class="text-danger"><?= number_format($activity_stats->failed ?? 0) ?></h3>
            </div>
        </div>
    </div>

    <div class="alert alert-warning">
        <i class="bi bi-info-circle"></i> <strong>Note:</strong> You can only view your reports. Data download is restricted for security purposes.
    </div>

    <div class="panel">
        <div class="panel-header">
            <div class="panel-title"><i class="bi bi-list"></i> Campaign-wise Breakdown</div>
            <div class="panel-actions"><a href="<?= base_url('user/reports_export') ?>" class="view-all-btn">Export CSV</a></div>
        </div>
        <div class="panel-body">
            <div class="panel-inner">
                <?php if (!empty($campaigns)): ?>
                    <div class="row g-3">
                        <?php foreach ($campaigns as $campaign):
                            $assigned = intval($campaign->my_assigned ?? 0);
                            $sent = intval($campaign->my_sent ?? 0);
                            $failed = intval($campaign->my_failed ?? 0);
                            $remaining = intval($campaign->my_remaining ?? 0);
                            $success_rate = $assigned > 0 ? min(100, round(($sent / $assigned) * 100, 1)) : 0;
                        ?>
                        <div class="col-12 col-md-6 col-lg-4">
                            <div class="campaign-card">
                                <div class="card-top">
                                    <div>
                                        <div class="card-title"><?= htmlspecialchars($campaign->name) ?></div>
                                        <div style="margin-top:6px; display:flex; gap:8px; align-items:center;">
                                            <span class="type-badge"><?= htmlspecialchars($campaign->type) ?></span>
                                            <?php if ($campaign->status === 'sent' || $campaign->status === 'completed'): ?>
                                                <span class="status-badge badge-status-sent">Sent</span>
                                            <?php elseif ($campaign->status === 'failed'): ?>
                                                <span class="status-badge badge-status-failed">Failed</span>
                                            <?php else: ?>
                                                <span class="status-badge badge-status-pending"><?= ucfirst(htmlspecialchars($campaign->status ?? 'pending')) ?></span>
                                            <?php endif; ?>
                                        </div>
                                    </div>
                                    <div class="text-end">
                                        <small class="text-muted">Updated: <?= htmlspecialchars(date('Y-m-d', strtotime($campaign->updated_at ?? $campaign->created_at ?? 'now'))) ?></small>
                                    </div>
                                </div>

                                <div class="metrics">
                                    <div class="metric">
                                        <div class="label">Assigned</div>
                                        <div class="value"><?= number_format($assigned) ?></div>
                                    </div>
                                    <div class="metric">
                                        <div class="label">Delivered</div>
                                        <div class="value text-success"><?= number_format($sent) ?></div>
                                    </div>
                                    <div class="metric">
                                        <div class="label">Failed</div>
                                        <div class="value text-danger"><?= number_format($failed) ?></div>
                                    </div>
                                </div>

                                <div class="progress-wrap">
                                    <div class="d-flex align-items-center justify-content-between mb-2">
                                        <small class="text-secondary">Success Rate</small>
                                        <small class="fw-bold"><?= number_format($success_rate, 1) ?>%</small>
                                    </div>
                                    <div class="progress" style="height:12px; border-radius:8px;">
                                        <div class="progress-bar" role="progressbar" style="width: <?= $success_rate ?>%;" aria-valuenow="<?= $success_rate ?>" aria-valuemin="0" aria-valuemax="100"></div>
                                    </div>
                                </div>

                                <div class="actions">
                                    <a href="<?= base_url('user/campaign_view/' . ($campaign->id ?? 0)) ?>" class="btn btn-gradient">View</a>
                                    <a href="<?= base_url('user/campaign_logs/' . ($campaign->id ?? 0)) ?>" class="btn btn-outline">Logs</a>
                                </div>
                            </div>
                        </div>
                        <?php endforeach; ?>
                    </div>
                <?php else: ?>
                    <div class="text-center py-4">
                        <p class="text-muted">No campaigns data available</p>
                        <a href="<?= base_url('user/create_campaign') ?>" class="btn btn-primary">Create Campaign</a>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>
