<?php
require_once __DIR__ . '/../src/bootstrap.php';
require_once __DIR__ . '/../src/functions.php';

$pdo = db();

$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($id <= 0) {
    flash('Geçersiz kampanya ID.', 'error');
    redirect('index.php');
}

$campaign = getCampaignById($pdo, $id);
if (!$campaign) {
    flash('Kampanya bulunamadı.', 'error');
    redirect('index.php');
}

$sends = getCampaignSends($pdo, $id);
$attachments = getCampaignAttachments($pdo, $id);

$successRateValue = successRate((int)$campaign['success_count'], (int)$campaign['total_count']);
$openRateValue = openedRate((int)$campaign['opened_count'], (int)$campaign['success_count']);

$statusMap = ['queued' => 'Kuyrukta', 'sending' => 'Gönderiliyor', 'completed' => 'Tamamlandı'];
$statusRaw = $campaign['status'] ?? 'queued';
$statusText = $statusMap[$statusRaw] ?? $statusRaw;
$statusClass = 'badge-status';
if ($statusRaw === 'completed') $statusClass = 'badge-status-completed';
elseif ($statusRaw === 'sending') $statusClass = 'badge-status-sending';
?>
<!doctype html>
<html lang="tr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Kampanya Detayı</title>
<style>
* { box-sizing: border-box; }
body { margin: 0; font-family: Arial, Helvetica, sans-serif; background: #f5f7fb; color: #111827; }
.container { max-width: 1240px; margin: 30px auto; padding: 0 16px; }
.topbar { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 20px; }
.topbar h1 { margin: 0; font-size: 28px; }
.actions { display: flex; gap: 10px; flex-wrap: wrap; }
.btn { display: inline-flex; align-items: center; justify-content: center; text-decoration: none; border: 0; border-radius: 10px; padding: 12px 18px; cursor: pointer; font-weight: 600; font-size: 14px; min-height: 44px; background: #e5e7eb; color: #111827; }
.btn-danger { background: #dc2626; color: #fff; }
.grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin-bottom: 20px; }
.card { background: #fff; border-radius: 16px; box-shadow: 0 10px 30px rgba(0,0,0,0.06); padding: 18px; margin-bottom: 20px; }
.stat-title { font-size: 13px; color: #6b7280; margin-bottom: 8px; }
.stat-value { font-size: 28px; font-weight: 700; }
.meta { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.meta-row { margin-bottom: 12px; }
.meta-label { font-size: 13px; color: #6b7280; margin-bottom: 6px; }
.meta-value { font-size: 15px; word-break: break-word; }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; min-width: 900px; }
th, td { text-align: left; padding: 12px 14px; border-bottom: 1px solid #e5e7eb; font-size: 14px; vertical-align: top; }
th { background: #f9fafb; color: #374151; }
.badge { display: inline-block; padding: 6px 10px; border-radius: 999px; font-size: 12px; font-weight: 700; }
.badge-success { background: #dcfce7; color: #166534; }
.badge-failed { background: #fee2e2; color: #991b1b; }
.badge-pending { background: #e5e7eb; color: #374151; }
.badge-opened { background: #dbeafe; color: #1d4ed8; }
.badge-status { background: #ede9fe; color: #6d28d9; }
.badge-status-completed { background: #dcfce7; color: #166534; }
.badge-status-sending { background: #fef3c7; color: #92400e; }
.html-frame { border: 1px solid #e5e7eb; border-radius: 12px; padding: 16px; background: #fff; overflow: auto; }
@media (max-width: 992px) { .grid { grid-template-columns: repeat(2, 1fr); } .meta { grid-template-columns: 1fr; } }
@media (max-width: 640px) { .grid { grid-template-columns: 1fr; } .topbar h1 { font-size: 22px; } .btn { width: 100%; } .actions { width: 100%; } }
</style>
</head>
<body>
<div class="container">
    <div class="topbar">
        <h1>Kampanya Detayı</h1>
        <div class="actions">
            <a href="index.php" class="btn">Panele Dön</a>
            <a href="runner.php?id=<?= (int)$campaign['id'] ?>" class="btn">Gönderim Takibi</a>
            <form method="post" action="delete_campaign.php" onsubmit="return confirm('Bu kampanyayı silmek istediğine emin misin?');" style="display:inline;">
                <input type="hidden" name="id" value="<?= (int)$campaign['id'] ?>">
                <button type="submit" class="btn btn-danger">Bu Kampanyayı Sil</button>
            </form>
        </div>
    </div>

    <div class="grid">
        <div class="card"><div class="stat-title">Toplam Gönderim</div><div class="stat-value"><?= (int)$campaign['total_count'] ?></div></div>
        <div class="card"><div class="stat-title">Başarılı</div><div class="stat-value"><?= (int)$campaign['success_count'] ?></div></div>
        <div class="card"><div class="stat-title">Başarısız</div><div class="stat-value"><?= (int)$campaign['failed_count'] ?></div></div>
        <div class="card"><div class="stat-title">Açılma</div><div class="stat-value"><?= (int)$campaign['opened_count'] ?></div></div>
    </div>

    <div class="grid">
        <div class="card"><div class="stat-title">Başarı Oranı</div><div class="stat-value">%<?= $successRateValue ?></div></div>
        <div class="card"><div class="stat-title">Açılma Oranı</div><div class="stat-value">%<?= $openRateValue ?></div></div>
        <div class="card"><div class="stat-title">Kampanya ID</div><div class="stat-value">#<?= (int)$campaign['id'] ?></div></div>
        <div class="card"><div class="stat-title">Durum</div><div class="stat-value" style="font-size:20px;"><span class="badge <?= e($statusClass) ?>"><?= e($statusText) ?></span></div></div>
    </div>

    <div class="card">
        <div class="meta">
            <div>
                <div class="meta-row"><div class="meta-label">Konu</div><div class="meta-value"><?= e($campaign['subject']) ?></div></div>
                <div class="meta-row"><div class="meta-label">Düz Metin İçerik</div><div class="meta-value"><?= nl2br(e($campaign['plain_body'] ?? '')) ?></div></div>
            </div>
            <div>
                <div class="meta-row"><div class="meta-label">Oluşturulma</div><div class="meta-value"><?= e($campaign['created_at']) ?></div></div>
                <div class="meta-row"><div class="meta-label">Başladı</div><div class="meta-value"><?= e($campaign['started_at'] ?? '-') ?></div></div>
                <div class="meta-row"><div class="meta-label">Tamamlandı</div><div class="meta-value"><?= e($campaign['completed_at'] ?? '-') ?></div></div>
            </div>
        </div>
    </div>

    <div class="card">
        <h2 style="margin-top:0;">Ek Dosyalar</h2>
        <?php if (!$attachments): ?>
            <p>Bu kampanyada ek dosya yok.</p>
        <?php else: ?>
            <div class="table-wrap">
                <table>
                    <thead><tr><th>Dosya Adı</th><th>Tür</th><th>Boyut</th><th>Tarih</th></tr></thead>
                    <tbody>
                    <?php foreach ($attachments as $attachment): ?>
                        <tr>
                            <td><?= e($attachment['original_name']) ?></td>
                            <td><?= e($attachment['mime_type'] ?? '-') ?></td>
                            <td><?= number_format(((int)$attachment['file_size']) / 1024, 2) ?> KB</td>
                            <td><?= e($attachment['created_at']) ?></td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>

    <div class="card">
        <h2 style="margin-top:0;">HTML Önizleme</h2>
        <div class="html-frame"><?= $campaign['html_body'] ?></div>
    </div>

    <div class="card">
        <h2 style="margin-top:0;">Gönderim Detayları</h2>
        <div class="table-wrap">
            <table>
                <thead>
                    <tr><th>ID</th><th>E-posta</th><th>Durum</th><th>Gönderildi</th><th>Açıldı</th><th>Abonelik</th><th>Hata</th></tr>
                </thead>
                <tbody>
                <?php if (!$sends): ?>
                    <tr><td colspan="7">Kayıt bulunamadı.</td></tr>
                <?php else: ?>
                    <?php foreach ($sends as $send): ?>
                        <tr>
                            <td>#<?= (int)$send['id'] ?></td>
                            <td><?= e($send['email']) ?></td>
                            <td>
                                <?php if ($send['status'] === 'success'): ?>
                                    <span class="badge badge-success">Başarılı</span>
                                <?php elseif ($send['status'] === 'failed'): ?>
                                    <span class="badge badge-failed">Başarısız</span>
                                <?php else: ?>
                                    <span class="badge badge-pending">Bekliyor</span>
                                <?php endif; ?>
                            </td>
                            <td><?= e($send['sent_at'] ?? '-') ?></td>
                            <td><?php if (!empty($send['opened_at'])): ?><span class="badge badge-opened"><?= e($send['opened_at']) ?></span><?php else: ?>-<?php endif; ?></td>
                            <td><?= (int)($send['unsubscribed'] ?? 0) === 1 ? 'Çıkmış' : 'Aktif' ?></td>
                            <td><?= e($send['error_message'] ?? '-') ?></td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
</body>
</html>
