<?php
require_once 'includes/header.php';

// Agent stats
$stats = $pdo->prepare("
    SELECT
        COUNT(l.id) AS total_leads,
        SUM(CASE WHEN f.status='pending' AND f.followup_date >= NOW() THEN 1 ELSE 0 END) AS upcoming,
        SUM(CASE WHEN f.status='pending' AND f.followup_date < NOW()  THEN 1 ELSE 0 END) AS missed,
        SUM(CASE WHEN f.status='completed' THEN 1 ELSE 0 END) AS done_fups
    FROM leads l
    LEFT JOIN lead_followups f ON f.lead_id = l.id
    WHERE l.assigned_agent_id = ? AND l.is_junk = 0
");
$stats->execute([$agent_id]);
$s = $stats->fetch();

// Upcoming follow-ups (next 10)
$upcoming = $pdo->prepare("
    SELECT f.*, l.phone, l.name as lead_name, l.id as lead_id
    FROM lead_followups f
    JOIN leads l ON l.id = f.lead_id
    WHERE l.assigned_agent_id = ? AND f.status = 'pending' AND f.followup_date >= NOW()
    ORDER BY f.followup_date ASC LIMIT 10
");
$upcoming->execute([$agent_id]);
$followups = $upcoming->fetchAll();

// Recent leads (last 8)
$recent = $pdo->prepare("
    SELECT l.*, st.name as stage, st.color_code
    FROM leads l
    LEFT JOIN lead_statuses st ON l.status_id = st.id
    WHERE l.assigned_agent_id = ? AND l.is_junk = 0
    ORDER BY l.created_at DESC LIMIT 8
");
$recent->execute([$agent_id]);
$recent_leads = $recent->fetchAll();
?>
<script>document.getElementById('page-title').textContent='Dashboard';</script>

<div class="welcome-banner">
    <h2>Welcome Back, <?= htmlspecialchars($agent_name) ?>!</h2>
    <p>Manage your assigned leads and keep track of your follow-ups.</p>
</div>

<!-- KPI Row -->
<div class="stats-grid">
    <div class="stat-card">
        <div class="stat-icon" style="background:rgba(59, 130, 246, 0.1); color:#3b82f6;">
            <i class="fa-solid fa-users"></i>
        </div>
        <div class="stat-info">
            <h3>My Leads</h3>
            <p class="stat-value"><?=$s['total_leads']?></p>
        </div>
    </div>
    <div class="stat-card">
        <div class="stat-icon" style="background:rgba(245, 158, 11, 0.1); color:#f59e0b;">
            <i class="fa-solid fa-clock"></i>
        </div>
        <div class="stat-info">
            <h3>Upcoming</h3>
            <p class="stat-value"><?=$s['upcoming']?></p>
        </div>
    </div>
    <div class="stat-card">
        <div class="stat-icon" style="background:rgba(239, 68, 68, 0.1); color:#ef4444;">
            <i class="fa-solid fa-circle-exclamation"></i>
        </div>
        <div class="stat-info">
            <h3>Missed</h3>
            <p class="stat-value"><?=$s['missed']?></p>
        </div>
    </div>
    <div class="stat-card">
        <div class="stat-icon" style="background:rgba(34, 197, 94, 0.1); color:#22c55e;">
            <i class="fa-solid fa-circle-check"></i>
        </div>
        <div class="stat-info">
            <h3>Done Follow-ups</h3>
            <p class="stat-value"><?=$s['done_fups']?></p>
        </div>
    </div>
</div>

<div style="display:grid;grid-template-columns:1fr;gap:20px;">

    <!-- Upcoming Follow-ups -->
    <div class="ag-card">
        <h3 class="ag-card-title"><i class="fa-regular fa-calendar-check" style="color:var(--primary);margin-right:7px;"></i>Upcoming Follow-ups</h3>
        <?php if(empty($followups)): ?>
            <p style="color:#94a3b8;font-style:italic;font-size:0.88rem;">No upcoming follow-ups. Great job! 🎉</p>
        <?php else: ?>
            <?php foreach($followups as $f): ?>
            <div style="display:flex;justify-content:space-between;align-items:center;padding:10px 0;border-bottom:1px solid #f1f5f9;">
                <div>
                    <a href="lead_view.php?id=<?=$f['lead_id']?>" style="font-weight:700;color:#0f172a;text-decoration:none;font-size:0.9rem;"><?=htmlspecialchars($f['phone'])?></a>
                    <?php if(!empty($f['lead_name'])): ?><div style="font-size:0.75rem;color:#94a3b8;"><?=htmlspecialchars($f['lead_name'])?></div><?php endif; ?>
                    <?php if(!empty($f['notes'])): ?><div style="font-size:0.78rem;color:#64748b;margin-top:2px;"><?=htmlspecialchars(mb_strimwidth($f['notes'],0,50,'…'))?></div><?php endif; ?>
                </div>
                <div style="text-align:right;flex-shrink:0;margin-left:12px;">
                    <div style="font-size:0.78rem;font-weight:700;color:#f59e0b;"><?=date('d M',strtotime($f['followup_date']))?></div>
                    <div style="font-size:0.72rem;color:#94a3b8;"><?=date('h:i A',strtotime($f['followup_date']))?></div>
                </div>
            </div>
            <?php endforeach; ?>
            <a href="followups.php" style="display:inline-flex;align-items:center;gap:5px;margin-top:14px;font-size:0.82rem;font-weight:700;color:var(--primary);text-decoration:none;">View All <i class="fa-solid fa-arrow-right"></i></a>
        <?php endif; ?>
    </div>

    <!-- Recent Leads -->
    <div class="ag-card">
        <h3 class="ag-card-title" style="display:flex;justify-content:space-between;align-items:center;">
            <span><i class="fa-solid fa-users" style="color:var(--primary);margin-right:7px;"></i>Recent Leads</span>
            <a href="lead_add.php" class="btn-primary btn-sm"><i class="fa-solid fa-plus"></i> Add</a>
        </h3>
        <?php if(empty($recent_leads)): ?>
            <p style="color:#94a3b8;font-style:italic;font-size:0.88rem;">No leads assigned yet.</p>
        <?php else: ?>
            <?php foreach($recent_leads as $l): ?>
            <div style="display:flex;justify-content:space-between;align-items:center;padding:10px 0;border-bottom:1px solid #f1f5f9;">
                <div>
                    <a href="lead_view.php?id=<?=$l['id']?>" style="font-weight:700;color:#0f172a;text-decoration:none;font-size:0.9rem;"><?=htmlspecialchars($l['phone'])?></a>
                    <?php if(!empty($l['name'])): ?><div style="font-size:0.75rem;color:#94a3b8;"><?=htmlspecialchars($l['name'])?></div><?php endif; ?>
                </div>
                <?php if($l['stage']): ?>
                <span class="ag-badge" style="background:<?=htmlspecialchars($l['color_code'])?>20;color:<?=htmlspecialchars($l['color_code'])?>;border:1px solid <?=htmlspecialchars($l['color_code'])?>40;"><?=htmlspecialchars($l['stage'])?></span>
                <?php endif; ?>
            </div>
            <?php endforeach; ?>
            <a href="my_leads.php" style="display:inline-flex;align-items:center;gap:5px;margin-top:14px;font-size:0.82rem;font-weight:700;color:var(--primary);text-decoration:none;">View All <i class="fa-solid fa-arrow-right"></i></a>
        <?php endif; ?>
    </div>
</div>

<?php require_once 'includes/footer.php'; ?>
