<?php
require_once '../includes/constants.php';
require_once '../includes/session.php';
require_once '../includes/functions.php';

if (!isset($_SESSION['admin_id']) || !in_array($_SESSION['admin_role'], ['manager','superadmin'])) {
    die("Access denied");
}

$offerId = $_GET['id'] ?? 0;
if (!$offerId) die("Missing offer ID");

// Date range
$start = $_GET['start'] ?? date('Y-m-01');
$end = $_GET['end'] ?? date('Y-m-d');

// Total clicks
$stmt = $pdo->prepare("SELECT COUNT(*) FROM partners_clicks WHERE offer_id = ? AND click_time BETWEEN ? AND ?");
$stmt->execute([$offerId, $start, $end]);
$totalClicks = $stmt->fetchColumn();

// Total conversions + revenue
$stmt = $pdo->prepare("SELECT COUNT(*) as convs, SUM(payout) as revenue FROM partners_conversions WHERE offer_id = ? AND conversion_time BETWEEN ? AND ?");
$stmt->execute([$offerId, $start, $end]);
$convData = $stmt->fetch(PDO::FETCH_ASSOC);

$conversionRate = $totalClicks ? round(($convData['convs'] / $totalClicks) * 100, 2) : 0;
$epc = $totalClicks ? round($convData['revenue'] / $totalClicks, 4) : 0;

// Affiliate earnings
$stmt = $pdo->prepare("SELECT c.affiliate_id, u.username, COUNT(*) as convs, SUM(c.payout) as revenue 
                       FROM partners_conversions c 
                       JOIN users u ON c.affiliate_id = u.id 
                       WHERE c.offer_id = ? AND c.conversion_time BETWEEN ? AND ?
                       GROUP BY c.affiliate_id ORDER BY revenue DESC");
$stmt->execute([$offerId, $start, $end]);
$byAffiliate = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html>
<head>
    <title><?php echo SITE_TITLE . ' | Offer Performance'; ?></title>
    <link rel="stylesheet" href="css/admin.css">
</head>
<body>
<div class="container">
<h2>📈 Offer Performance for #<?php echo htmlspecialchars($offerId); ?></h2>
<form method="get">
    <input type="hidden" name="id" value="<?php echo htmlspecialchars($offerId); ?>">
    Start: <input type="date" name="start" value="<?php echo htmlspecialchars($start); ?>">
    End: <input type="date" name="end" value="<?php echo htmlspecialchars($end); ?>">
    <button type="submit">Refresh</button>
</form>

<hr>
<p>Total Clicks: <strong><?php echo htmlspecialchars($totalClicks); ?></strong></p>
<p>Total Conversions: <strong><?php echo htmlspecialchars($convData['convs']); ?></strong></p>
<p>Total Revenue: <strong><?php echo htmlspecialchars($convData['revenue']); ?></strong></p>
<p>Conversion Rate: <strong><?php echo htmlspecialchars($conversionRate); ?>%</strong></p>
<p>EPC: <strong><?php echo htmlspecialchars($epc); ?></strong></p>

<hr><h3>💸 Affiliate Earnings</h3>
<table><tr><th>Affiliate</th><th>ID</th><th>Conversions</th><th>Revenue</th></tr>
<?php
foreach ($byAffiliate as $row) {
    echo "<tr>
        <td>" . htmlspecialchars($row['username']) . "</td>
        <td>" . htmlspecialchars($row['affiliate_id']) . "</td>
        <td>" . htmlspecialchars($row['convs']) . "</td>
        <td>" . htmlspecialchars($row['revenue']) . "</td>
    </tr>";
}
?>
</table>
</div>
</body>
</html>