<?php
if (!defined('ROOT_PATH')) { http_response_code(403); exit; }
require_once ROOT_PATH . '/admin/templates/header.php';
$isRtl = \Core\System\Language::direction() === 'rtl';
$statusColors = ['draft'=>'bg-secondary','pending'=>'bg-info text-dark','active'=>'bg-success','paused'=>'bg-warning text-dark','deleted'=>'bg-danger','error'=>'bg-danger'];
$targeting = $campaign['targeting'] ?? [];
?>
<div class="content-wrapper"><section class="content pt-3"><div class="container-fluid pb-5">

<div class="d-flex align-items-center justify-content-between mb-4">
    <div>
        <h4 class="fw-bold mb-1"><?= htmlspecialchars($campaign['name']) ?></h4>
        <span class="badge <?= $statusColors[$campaign['status']] ?? 'bg-secondary' ?> me-2"><?= ucfirst($campaign['status']) ?></span>
        <?php if($campaign['fb_campaign_id']): ?><code class="text-muted small">FB: <?= $campaign['fb_campaign_id'] ?></code><?php endif; ?>
    </div>
    <div class="d-flex gap-2">
        <?php if($campaign['status'] === 'active'): ?>
        <form method="post" action="/admin/facebook-ads/campaign/<?= $campaign['id'] ?>/pause" class="d-inline">
            <?= \Core\System\Csrf::field() ?>
            <button class="btn btn-sm btn-warning fw-semibold"><i class="bi bi-pause-fill me-1"></i><?= fbt('fb_ads.pause','Pause') ?></button>
        </form>
        <?php elseif($campaign['status'] === 'paused'): ?>
        <form method="post" action="/admin/facebook-ads/campaign/<?= $campaign['id'] ?>/resume" class="d-inline">
            <?= \Core\System\Csrf::field() ?>
            <button class="btn btn-sm btn-success fw-semibold"><i class="bi bi-play-fill me-1"></i><?= fbt('fb_ads.resume','Resume') ?></button>
        </form>
        <?php endif; ?>
        <?php if($campaign['fb_campaign_id']): ?>
        <a href="https://www.facebook.com/adsmanager/manage/campaigns" target="_blank" class="btn btn-sm btn-primary">
            <i class="bi bi-facebook me-1"></i><?= fbt('fb_ads.view_in_fb','View in Ads Manager') ?>
        </a>
        <?php endif; ?>
        <a href="/admin/facebook-ads/analytics/<?= $campaign['id'] ?>" class="btn btn-sm btn-outline-primary">
            <i class="bi bi-bar-chart me-1"></i><?= fbt('fb_ads.analytics','Analytics') ?>
        </a>
        <a href="/admin/facebook-ads/ai-advisor/<?= $campaign['id'] ?>" class="btn btn-sm fw-semibold"
           style="background:linear-gradient(135deg,#7c3aed,#a855f7);color:#fff;border:none">
            <i class="bi bi-stars me-1"></i>AI Advisor
        </a>
        <a href="/admin/facebook-ads" class="btn btn-sm btn-outline-secondary">
            <i class="bi bi-arrow-<?= $isRtl?'right':'left' ?> me-1"></i><?= fbt('fb_ads.back','Back') ?>
        </a>
    </div>
</div>

<?php if($campaign['status'] === 'error' && $campaign['error_message']): ?>
<div class="alert alert-danger border-0 shadow-sm mb-4">
    <div class="fw-bold mb-1"><i class="bi bi-exclamation-triangle-fill me-2"></i><?= fbt('fb_ads.api_error','API Error') ?></div>
    <div class="small font-monospace"><?= htmlspecialchars($campaign['error_message']) ?></div>
</div>
<?php endif; ?>

<div class="row g-4">
<div class="col-lg-8">

    <!-- Stats cards -->
    <div class="row g-3 mb-4">
    <?php foreach([
        ['bi-eye','primary',fbt('fb_ads.impressions','Impressions'),number_format($campaign['impressions'])],
        ['bi-cursor','success',fbt('fb_ads.clicks','Clicks'),number_format($campaign['clicks'])],
        ['bi-cash','warning',fbt('fb_ads.spend','Spend'),number_format($campaign['spend'],2).' DZD'],
        ['bi-percent','info',fbt('fb_ads.ctr','CTR'),
          $campaign['impressions']>0 ? round(($campaign['clicks']/$campaign['impressions'])*100,2).'%' : '—'],
    ] as [$icon,$color,$label,$val]): ?>
    <div class="col-6 col-md-3">
        <div class="card border-0 shadow-sm text-center py-3">
            <i class="bi <?= $icon ?> text-<?= $color ?> fs-3 mb-1"></i>
            <div class="fw-bold fs-5"><?= $val ?></div>
            <div class="text-muted small"><?= $label ?></div>
        </div>
    </div>
    <?php endforeach; ?>
    </div>

    <!-- Ad copy -->
    <div class="card border-0 shadow-sm mb-4">
        <div class="card-header bg-white border-bottom py-3"><h6 class="mb-0 fw-semibold"><i class="bi bi-file-text me-2 text-muted"></i><?= fbt('fb_ads.ad_copy','Ad Copy') ?></h6></div>
        <div class="card-body">
            <div class="mb-3"><label class="form-label small fw-semibold text-muted"><?= fbt('fb_ads.primary_text','Primary Text') ?></label><div class="p-3 bg-light rounded-3" style="white-space:pre-line"><?= htmlspecialchars($campaign['ad_text'] ?? '—') ?></div></div>
            <div class="row g-3">
                <div class="col-md-6"><label class="form-label small fw-semibold text-muted"><?= fbt('fb_ads.headline','Headline') ?></label><div class="p-2 bg-light rounded"><?= htmlspecialchars($campaign['ad_headline'] ?? '—') ?></div></div>
                <div class="col-md-6"><label class="form-label small fw-semibold text-muted"><?= fbt('fb_ads.description','Description') ?></label><div class="p-2 bg-light rounded"><?= htmlspecialchars($campaign['ad_description'] ?? '—') ?></div></div>
            </div>
        </div>
    </div>

    <!-- Targeting -->
    <div class="card border-0 shadow-sm">
        <div class="card-header bg-white border-bottom py-3"><h6 class="mb-0 fw-semibold"><i class="bi bi-people me-2 text-muted"></i><?= fbt('fb_ads.targeting','Targeting') ?></h6></div>
        <div class="card-body">
            <div class="row g-2 small">
                <div class="col-md-4"><span class="text-muted"><?= fbt('fb_ads.country','Country') ?>:</span> <?= implode(', ', $targeting['geo_locations']['countries'] ?? []) ?: '—' ?></div>
                <div class="col-md-4"><span class="text-muted"><?= fbt('fb_ads.age_range','Age') ?>:</span> <?= ($targeting['age_min']??'?').'-'.($targeting['age_max']??'?') ?></div>
                <div class="col-md-4"><span class="text-muted"><?= fbt('fb_ads.gender','Gender') ?>:</span>
                    <?php $genders = $targeting['genders'] ?? []; echo empty($genders) ? fbt('fb_ads.all_genders','All') : (in_array(1,$genders)?fbt('fb_ads.male','Male'):fbt('fb_ads.female','Female')); ?>
                </div>
            </div>
        </div>
    </div>

</div>
<div class="col-lg-4">
    <div class="card border-0 shadow-sm mb-4">
        <div class="card-header bg-white border-bottom py-3"><h6 class="mb-0 fw-semibold"><i class="bi bi-info-circle me-2 text-muted"></i><?= fbt('fb_ads.campaign_info','Campaign Info') ?></h6></div>
        <div class="card-body p-0">
            <?php foreach([
                [fbt('fb_ads.product','Product'), $campaign['product_title']??'—'],
                [fbt('fb_ads.objective','Objective'), $campaign['objective']],
                [fbt('fb_ads.daily_budget','Daily Budget'), number_format($campaign['daily_budget']/100).' DZD'],
                [fbt('fb_ads.duration','Duration'), $campaign['duration_days'].' '.__('fb_ads.days','days')],
                [fbt('fb_ads.created_at','Created'), date('Y/m/d', strtotime($campaign['created_at']))],
            ] as [$k,$v]): ?>
            <div class="d-flex justify-content-between align-items-center px-3 py-2 border-bottom">
                <span class="text-muted small"><?= $k ?></span>
                <span class="small fw-semibold"><?= htmlspecialchars($v) ?></span>
            </div>
            <?php endforeach; ?>
        </div>
    </div>
    <!-- Delete -->
    <div class="card border-0 shadow-sm" style="border-top:3px solid #dc2626 !important">
        <div class="card-body">
            <form method="post" action="/admin/facebook-ads/campaign/<?= $campaign['id'] ?>/delete"
                  onsubmit="return confirm('<?= fbt('fb_ads.confirm_delete','Delete this campaign?') ?>')">
                <?= \Core\System\Csrf::field() ?>
                <button class="btn btn-outline-danger btn-sm w-100"><i class="bi bi-trash me-1"></i><?= fbt('fb_ads.delete_campaign','Delete Campaign') ?></button>
            </form>
        </div>
    </div>
</div>
</div>

</div></section></div>
<?php require_once ROOT_PATH . '/admin/templates/footer.php'; ?>
