<?php
// data_owner/my_analytics.php
session_start();
require_once '../config.php';
require_once '../includes/auth.php';
require_once '../classes/DataSource.php';

// Ensure only Data Owners can access this page
redirect_if_not_role('Data Owner');

$data_source_manager = new DataSource($pdo);
$user_id = $_SESSION['user_id'];
$owner_person_id = $_SESSION['person_id'];
$username = $_SESSION['username'];

// Fetch analytics data (placeholders for now)
// You would typically query dsps_tbl_datasource_used and dsps_tbl_anonymous
// filtered by data sources owned by $owner_person_id.

// Example: Total downloads for my data sources
$total_downloads = 0;
try {
    $stmt = $pdo->prepare("
        SELECT COUNT(dsu.pkdspsdspused_id)
        FROM dsps_tbl_datasource_used dsu
        JOIN dsps_tbl_datasource ds ON dsu.fkdspsdsused_id = ds.pkdspsds_id
        WHERE ds.fkisp_id_of = :owner_person_id AND dsu.dspsdspused_action = 'Downloaded'
    ");
    $stmt->execute(['owner_person_id' => $owner_person_id]);
    $total_downloads = $stmt->fetchColumn();
} catch (PDOException $e) {
    error_log("Error fetching total downloads: " . $e->getMessage());
}

// Example: Most viewed data sources (from anonymous views or usage logs)
$most_viewed_datasources = [];
try {
    $stmt = $pdo->prepare("
        SELECT ds.dspsds_title_en, COUNT(da.pkdspsano_id) AS view_count
        FROM dsps_tbl_anonymous da
        JOIN dsps_tbl_datasource ds ON da.fkdspsds_id = ds.pkdspsds_id
        WHERE ds.fkisp_id_of = :owner_person_id
        GROUP BY ds.pkdspsds_id, ds.dspsds_title_en
        ORDER BY view_count DESC
        LIMIT 5
    ");
    $stmt->execute(['owner_person_id' => $owner_person_id]);
    $most_viewed_datasources = $stmt->fetchAll();
} catch (PDOException $e) {
    error_log("Error fetching most viewed data sources: " . $e->getMessage());
}

?>
<!DOCTYPE html>
<html lang="en">
<!-- Header -->
<?php
// Include header file for admin pages
include_once("../includes/header_owner.php");
?>
<body>
    <div class="wrapper">
        <!-- Sidebar -->
        <?php
        // Include header file for admin pages
        include_once("../includes/nav_owner.php");
        ?>

        <!-- Page Content -->
        <div class="main-content">
            <nav class="navbar navbar-expand-lg navbar-light bg-light mb-4 rounded-3">
                <div class="container-fluid">
                    <a class="navbar-brand" href="#"> My Data Analytics</a>
                    <div class="d-flex">
                        <span class="navbar-text me-3">
                            Welcome, <?php echo htmlspecialchars($_SESSION['username']); ?>!
                        </span>
                    </div>
                </div>
            </nav>

            <?php if (isset($_SESSION['message'])): ?>
                <div class="alert alert-<?= $_SESSION['message_type'] ?> alert-dismissible fade show rounded" role="alert">
                    <?= $_SESSION['message'] ?>
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
                <?php
                unset($_SESSION['message']);
                unset($_SESSION['message_type']);
                ?>
            <?php endif; ?>

            <div class="row g-4 mb-5">
                <div class="col-md-6">
                    <div class="card shadow-sm rounded">
                        <div class="card-body">
                            <h5 class="card-title text-primary"><i class="fas fa-download me-2"></i> Total Downloads of My Data</h5>
                            <p class="card-text fs-2"><?= $total_downloads ?></p>
                        </div>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="card shadow-sm rounded">
                        <div class="card-body">
                            <h5 class="card-title text-info"><i class="fas fa-eye me-2"></i> Total Views of My Data Introductions</h5>
                            <p class="card-text fs-2">1500</p> <!-- Placeholder, implement actual count from dsps_tbl_anonymous -->
                        </div>
                    </div>
                </div>
            </div>

            <div class="card shadow-sm rounded mb-5">
                <div class="card-header bg-light rounded-top">
                    <h5 class="mb-0">Most Viewed Data Sources (Top 5)</h5>
                </div>
                <div class="card-body">
                    <?php if (!empty($most_viewed_datasources)): ?>
                        <ul class="list-group list-group-flush">
                            <?php foreach ($most_viewed_datasources as $ds): ?>
                                <li class="list-group-item d-flex justify-content-between align-items-center">
                                    <?= htmlspecialchars($ds['dspsds_title_en']) ?>
                                    <span class="badge bg-primary rounded"><?= htmlspecialchars($ds['view_count']) ?> Views</span>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                    <?php else: ?>
                        <div class="alert alert-info rounded mb-0">No view data available for your sources yet.</div>
                    <?php endif; ?>
                </div>
            </div>

        </div>
    </div>
<!-- Footer -->
<?php
// Include Footer file for owner pages
include_once("../includes/footer_owner.php");
?>   
</body>
</html>