<?php
require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/../includes/auth.php';
require_once __DIR__ . '/../includes/module_hub_helpers.php';
require_once __DIR__ . '/functions.php';

$db = mkt_db();
$campaignId = (int)($_GET['campaign_id'] ?? 0);
$page_title = 'سجل محرك التسويق';
$logs = mkt_fetch_logs($db, $campaignId, 200);
$runs = mkt_fetch_runs($db, $campaignId, 100);
$campaign = $campaignId > 0 ? mkt_fetch_campaign($db, $campaignId) : null;

require_once __DIR__ . '/../includes/header.php';
require_once __DIR__ . '/../includes/sidebar.php';
?>
<div class="nay-dashboard-page"><div class="nay-dashboard-main">
    <div class="nay-dashboard-hero"><div><h1>سجل محرك التسويق</h1><p><?= $campaign ? mkt_h($campaign['name']) : 'كل الحملات' ?></p></div><div class="nay-actions"><a class="nay-btn" href="index.php">رجوع</a></div></div>
    <?php if (!mkt_schema_ready($db)): ?><div class="flash flash-error" style="margin-bottom:16px">نفّذ SQL: <strong>/app/admin/sql/marketing_engine_stage2b_patch.sql</strong></div><?php endif; ?>
    <div class="nay-card">
        <div class="nay-card-header-inline"><h3>تشغيلات الحملات</h3><span><?= count($runs) ?> تشغيل</span></div>
        <?php if (!$runs): ?><div class="nay-empty">ماكو تشغيلات.</div><?php else: ?><div class="nay-table-wrap"><table class="nay-table"><thead><tr><th>الحملة</th><th>نوع التشغيل</th><th>بدأت</th><th>انتهت</th><th>المطابقون</th><th>تم التنفيذ</th><th>تم التخطي</th><th>فشل</th></tr></thead><tbody><?php foreach ($runs as $row): ?><tr><td><?= mkt_h($row['campaign_name'] ?? ('#'.$row['campaign_id'])) ?></td><td><?= mkt_h(mkt_run_mode_types()[$row['run_type']] ?? ($row['run_type'] ?? 'manual')) ?></td><td><?= mkt_h($row['started_at']) ?></td><td><?= mkt_h((string)($row['finished_at'] ?: '—')) ?></td><td><?= (int)$row['total_matched'] ?></td><td><?= (int)$row['total_executed'] ?></td><td><?= (int)$row['total_skipped'] ?></td><td><?= (int)$row['total_failed'] ?></td></tr><?php endforeach; ?></tbody></table></div><?php endif; ?>
    </div>
    <div class="nay-card" style="margin-top:16px">
        <div class="nay-card-header-inline"><h3>نتائج الزبائن</h3><span><?= count($logs) ?> سجل</span></div>
        <?php if (!$logs): ?><div class="nay-empty">ماكو نتائج بعد.</div><?php else: ?><div class="nay-table-wrap"><table class="nay-table"><thead><tr><th>الحملة</th><th>الزبون</th><th>نوع التشغيل</th><th>الحالة</th><th>سبب القرار</th><th>تهدئة</th><th>الوقت</th></tr></thead><tbody><?php foreach ($logs as $row): ?><tr><td><?= mkt_h($row['campaign_name'] ?? ('#'.$row['campaign_id'])) ?></td><td><?= mkt_h($row['full_name'] ?? ('#'.$row['customer_id'])) ?><div class="muted"><?= mkt_h($row['phone'] ?? '') ?></div></td><td><?= mkt_h(mkt_run_mode_types()[$row['run_type']] ?? ($row['run_type'] ?? 'manual')) ?></td><td><?= mkt_h(mkt_log_status_label((string)$row['status_text'])) ?></td><td><?= mkt_h($row['matched_reason'] ?: ($row['failure_reason'] ?: ($row['skip_reason'] ?: '—'))) ?></td><td><?= (int)($row['cooldown_applied'] ?? 0) === 1 ? 'نعم' : '—' ?></td><td><?= mkt_h($row['executed_at']) ?></td></tr><?php endforeach; ?></tbody></table></div><?php endif; ?>
    </div>
</div></div>
<?php require_once __DIR__ . '/../includes/footer.php'; ?>
