<?php
session_start();

$role = $_SESSION['user_status'] ?? null;
$homeMap = [
    'DAC Staff' => 'admin/dashboard.php',
    'Data Owner' => 'data_owner/dashboard.php',
    'Data Contributor' => 'data_hybrid/dashboard.php',
    'Data User' => 'data_user/dashboard.php',
];
$homeLink = $homeMap[$role] ?? 'index.php';
$username = $_SESSION['username'] ?? null;
$isDacStaff = ($role === 'DAC Staff');
?>
<!DOCTYPE html>
<html lang="en">
<?php include_once("includes/header_public.php"); ?>
<body>
    <nav class="navbar navbar-expand-lg navbar-light bg-white fixed-top">
        <div class="container">
            <a class="navbar-brand d-flex align-items-center" href="<?php echo htmlspecialchars($homeLink); ?>">
                <img src="assets/images/niphlogo.png" alt="NIPH Logo" height="42" class="me-2">
                <span class="fw-semibold text-success">NIPH DSP</span>
            </a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#guideNavbar" aria-controls="guideNavbar" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse justify-content-end" id="guideNavbar">
                <ul class="navbar-nav align-items-lg-center">
                    <?php if ($username): ?>
                        <li class="nav-item me-lg-3 text-muted small">Signed in as <strong><?php echo htmlspecialchars($username); ?></strong></li>
                    <?php endif; ?>
                    <li class="nav-item">
                        <a class="btn btn-outline-primary rounded-pill" href="<?php echo htmlspecialchars($homeLink); ?>">
                            <i class="fas fa-arrow-left me-2"></i>Back to Portal
                        </a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <main class="container my-5">
        <header class="mb-5">
            <span class="badge bg-success-subtle text-success rounded-pill mb-2">User Guide</span>
            <h1 class="fw-bold">NIPH Data Sharing Platform</h1>
            <p class="text-muted lead">
                This guide walks you through the core workflows and responsibilities for all roles on the platform.
                Use the navigation bookmarks below to jump to the section that best matches your access level.
            </p>
        </header>

        <section class="mb-5">
            <h2 class="h5 text-uppercase text-muted">Quick Navigation</h2>
            <div class="row row-cols-1 row-cols-md-2 row-cols-lg-3 g-3">
                <div class="col">
                    <a href="#getting-started" class="card h-100 shadow-sm text-decoration-none">
                        <div class="card-body">
                            <h3 class="card-title h6 text-success"><i class="fas fa-compass me-2"></i>Getting Started</h3>
                            <p class="card-text text-muted mb-0">Account set-up, password resets, and general navigation.</p>
                        </div>
                    </a>
                </div>
                <div class="col">
                    <a href="#dac-staff" class="card h-100 shadow-sm text-decoration-none">
                        <div class="card-body">
                            <h3 class="card-title h6 text-primary"><i class="fas fa-user-shield me-2"></i>DAC Staff Workflow</h3>
                            <p class="card-text text-muted mb-0">Managing users, data sources, permissions, and content.</p>
                        </div>
                    </a>
                </div>
                <div class="col">
                    <a href="#data-owner" class="card h-100 shadow-sm text-decoration-none">
                        <div class="card-body">
                            <h3 class="card-title h6 text-info"><i class="fas fa-database me-2"></i>Data Owner Workflow</h3>
                            <p class="card-text text-muted mb-0">Publishing datasets and reviewing contributor requests.</p>
                        </div>
                    </a>
                </div>
                <div class="col">
                    <a href="#data-contributor" class="card h-100 shadow-sm text-decoration-none">
                        <div class="card-body">
                            <h3 class="card-title h6 text-warning"><i class="fas fa-chart-line me-2"></i>Data Contributor Workflow</h3>
                            <p class="card-text text-muted mb-0">Hybrid role balancing owner uploads with user-level data access.</p>
                        </div>
                    </a>
                </div>
                <div class="col">
                    <a href="#data-user" class="card h-100 shadow-sm text-decoration-none">
                        <div class="card-body">
                            <h3 class="card-title h6 text-danger"><i class="fas fa-folder-open me-2"></i>Data User Workflow</h3>
                            <p class="card-text text-muted mb-0">Browsing catalogues and working with approved files.</p>
                        </div>
                    </a>
                </div>
                <div class="col">
                    <a href="#r-in-jupyterhub" class="card h-100 shadow-sm text-decoration-none">
                        <div class="card-body">
                            <h3 class="card-title h6 text-secondary"><i class="fas fa-flask me-2"></i>R in JupyterHub</h3>
                            <p class="card-text text-muted mb-0">Running reproducible analyses inside the embedded notebook.</p>
                        </div>
                    </a>
                </div>
            </div>
        </section>

        <section id="getting-started" class="mb-5">
            <h2 class="h4 mb-3">Getting Started</h2>
            <div class="card shadow-sm border-0">
                <div class="card-body">
                    <ol class="mb-0">
                        <li class="mb-3"><strong>Log in</strong> with the credentials issued by the Data Access Committee. Password resets are handled by DAC Staff from <em>Manage Users</em>.</li>
                        <li class="mb-3"><strong>Review your dashboard</strong>. Every role receives a dedicated summary of tasks, quick actions, and usage trends.</li>
                        <li class="mb-3"><strong>Use the left sidebar</strong> to switch between data management activities (such as permissions or uploads) and content pages (announcements, FAQ).</li>
                        <li class="mb-0"><strong>Need assistance?</strong> Use the <em>Contact Us</em> administration page or the feedback tools to reach the support team.</li>
                    </ol>
                </div>
            </div>
        </section>


        <section id="dac-staff" class="mb-5">
            <h2 class="h4 mb-3 text-primary">DAC Staff Workflow</h2>
            <div class="card shadow-sm border-0 mb-4">
                <div class="card-body">
                    <h3 class="h6 text-uppercase text-muted">User Onboarding</h3>
                    <ul class="mb-0">
                        <li>Navigate to <strong>Manage Users</strong> &gt; <em>Add New User</em>. Capture personal details, institutional affiliation, and select the appropriate role.</li>
                        <li>Assign <strong>R in JupyterHub</strong> access if analytical notebooks should be available to the account and confirm storage limits.</li>
                        <li>Record the requester or approving authority in the notes field to maintain an audit trail for each new account.</li>
                        <li>Reset passwords via the <em>Reset Password</em> action whenever necessary and confirm via email or phone for security.</li>
                    </ul>
                </div>
            </div>
            <div class="card shadow-sm border-0 mb-4">
                <div class="card-body">
                    <h3 class="h6 text-uppercase text-muted">Data Source Oversight</h3>
                    <ul class="mb-0">
                        <li>Use <strong>Manage Data Sources</strong> to view every dataset, inspect uploaded files, adjust statuses, or delete outdated content.</li>
                        <li>Curate classifications (data types, categories) in <strong>Manage Classifications</strong> so owners have consistent taxonomy options.</li>
                        <li>Review pending permission requests and audit usage from <strong>Manage Permissions</strong>. Tag sensitive requests for additional review.</li>
                        <li>Periodically export data source inventories as CSV/Excel for offline review with governance teams.</li>
                    </ul>
                </div>
            </div>
            <div class="card shadow-sm border-0">
                <div class="card-body">
                    <h3 class="h6 text-uppercase text-muted">Content &amp; Communication</h3>
                    <ul class="mb-0">
                        <li>Announcements, FAQs, slides, About Us copy, Contact Us responses, and (optionally) this user guide are edited from the <em>Content Management</em> section.</li>
                        <li>Utilize analytics on the admin dashboard to understand which owners and contributors are most active.</li>
                        <li>Create quarterly reports by combining dashboard analytics, download logs, and request approvals to support stewardship reviews.</li>
                    </ul>
                </div>
            </div>
            <div class="card shadow-sm border-0 mt-4">
                <div class="card-body">
                    <h3 class="h6 text-uppercase text-muted">Governance &amp; Compliance</h3>
                    <ul class="mb-0">
                        <li>Ensure mandatory data sharing agreements are uploaded to the <strong>Documents</strong> repository and linked to relevant owners.</li>
                        <li>Monitor inactivity alerts to deactivate accounts that have not logged in within policy-defined windows.</li>
                        <li>Coordinate security reviews with IT—for example, confirming backups and retention policies align with institutional standards.</li>
                    </ul>
                </div>
            </div>
        </section>

        <section id="data-owner" class="mb-5">
            <h2 class="h4 mb-3 text-info">Data Owner Workflow</h2>
            <div class="card shadow-sm border-0">
                <div class="card-body">
                    <ol class="mb-0">
                        <li class="mb-3"><strong>Add or edit data sources</strong> from <em>My Data Sources</em>. Upload multiple supporting documents (questionnaires, protocols, etc.) and manage statuses.</li>
                        <li class="mb-3"><strong>Monitor permissions</strong> in <em>Data Permissions</em>. Approve, reject, or revoke contributor access. All decisions are logged for auditing.</li>
                        <li class="mb-3"><strong>Analyze usage</strong> using the owner dashboard’s cards and R in JupyterHub workspace. Only approved files are synchronized to your notebook folder.</li>
                        <li class="mb-0"><strong>Keep metadata current</strong> so contributors can quickly understand dataset contents and documentation.</li>
                    </ol>
                </div>
            </div>
            <div class="card shadow-sm border-0 mt-4">
                <div class="card-body">
                    <h3 class="h6 text-uppercase text-muted">Before Publishing</h3>
                    <ul class="mb-0">
                        <li>Review data dictionaries for completeness and include variable-level descriptions where possible.</li>
                        <li>Confirm that personally identifiable information (PII) is removed or obfuscated consistent with ethics approvals.</li>
                        <li>Attach governance documents (IRB approvals, data sharing agreements) so contributors can quickly assess compliance requirements.</li>
                    </ul>
                </div>
            </div>
            <div class="card shadow-sm border-0 mt-4">
                <div class="card-body">
                    <h3 class="h6 text-uppercase text-muted">Ongoing Stewardship</h3>
                    <ul class="mb-0">
                        <li>Set reminders to review each active dataset quarterly, checking for updated versions or new documentation.</li>
                        <li>Collaborate with DAC Staff to respond to contributor questions logged through the platform.</li>
                        <li>Track downstream publications using your data and log citations in the dataset description for future users.</li>
                    </ul>
                </div>
            </div>
        </section>

        <section id="data-contributor" class="mb-5">
            <h2 class="h4 mb-3 text-warning">Data Contributor Workflow</h2>
            <p class="text-muted">Data Contributors operate as a hybrid between Data Owners and Data Users—they can publish datasets when delegated while also requesting, downloading, and analyzing data like a user.</p>
            <div class="card shadow-sm border-0">
                <div class="card-body">
                    <ol class="mb-0">
                        <li class="mb-3"><strong>Browse or upload data</strong> depending on your remit. When uploading, include questionnaires, protocols, and supporting documents to aid reviewers.</li>
                        <li class="mb-3"><strong>Track permission requests</strong> from <em>My data Request</em>. Status changes (Approved, Pending, Rejected) are visible at a glance.</li>
                        <li class="mb-3"><strong>Review downloads</strong> in <em>My Downloads</em> to document how datasets are being used.</li>
                        <li class="mb-0"><strong>Leverage R in JupyterHub</strong> when enabled. Approved files appear under <code>datasources/user_{your_id}</code> in the notebook workspace.</li>
                    </ol>
                </div>
            </div>
            <div class="card shadow-sm border-0 mt-4">
                <div class="card-body">
                    <h3 class="h6 text-uppercase text-muted">Acting as a Publisher</h3>
                    <ul class="mb-0">
                        <li>Coordinate with the designated Data Owner to ensure uploads follow agreed quality standards and naming conventions.</li>
                        <li>Use the <strong>Version Notes</strong> field to capture what changed with each upload so that reviewers understand the dataset evolution.</li>
                        <li>Flag sensitive variables in the metadata so DAC Staff can apply additional access controls if necessary.</li>
                    </ul>
                </div>
            </div>
            <div class="card shadow-sm border-0 mt-4">
                <div class="card-body">
                    <h3 class="h6 text-uppercase text-muted">Acting as a Data User</h3>
                    <ul class="mb-0">
                        <li>Prepare clear justifications when requesting access—describe the intended analysis, timelines, and expected outputs.</li>
                        <li>Maintain personal notes in <em>My data Request</em> to track correspondence or follow-up tasks for each dataset.</li>
                        <li>Document analytical steps in shared notebooks or reports so owners can understand how their data is being used.</li>
                    </ul>
                </div>
            </div>
        </section>

        <section id="data-user" class="mb-5">
            <h2 class="h4 mb-3 text-danger">Data User Workflow</h2>
            <div class="card shadow-sm border-0">
                <div class="card-body">
                    <ul class="mb-0">
                        <li>Use <strong>Browse All Data</strong> to inspect catalogues, metadata, and available documentation before submitting requests.</li>
                        <li>Submit requests through <strong>My data Request</strong> and monitor approvals. Attach research proposals or ethics approvals when required.</li>
                        <li>Downloaders are tracked in <strong>My Downloads</strong> to aid reproducibility and reporting.</li>
                        <li>When notebook access is granted, approved datasets synchronize to the R in JupyterHub workspace automatically.</li>
                        <li>Share summaries or publications with Data Owners so they can evidence impact and update dataset narratives.</li>
                    </ul>
                </div>
            </div>
            <div class="card shadow-sm border-0 mt-4">
                <div class="card-body">
                    <h3 class="h6 text-uppercase text-muted">Responsible Use</h3>
                    <ul class="mb-0">
                        <li>Restrict downloads to secure, institutionally approved devices and storage locations.</li>
                        <li>Respect embargo periods or usage limitations noted in the dataset metadata.</li>
                        <li>Notify DAC Staff immediately if you suspect unauthorized access or data leakage.</li>
                    </ul>
                </div>
            </div>
        </section>

        <section id="r-in-jupyterhub" class="mb-5">
            <h2 class="h4 mb-3 text-secondary">Working with R in JupyterHub</h2>
            <div class="card shadow-sm border-0">
                <div class="card-body">
                    <ol class="mb-0">
                        <li class="mb-3">Launch the notebook from the sidebar entry. The interface opens inside the portal and can also be opened in a new tab.</li>
                        <li class="mb-3">Approved files are mirrored (via symlink/copy) into your personal <code>datasources/user_{id}</code> directory. Review <code>manifest.json</code> to see which files were synced.</li>
                        <li class="mb-3">Create new R notebooks via the Launcher, install libraries as needed, and export results back to the platform if required.</li>
                        <li class="mb-0">Contact DAC Staff if additional packages or larger storage quotas are required for analysis.</li>
                    </ol>
                </div>
            </div>
            <div class="card shadow-sm border-0 mt-4">
                <div class="card-body">
                    <h3 class="h6 text-uppercase text-muted">Notebook Best Practices</h3>
                    <ul class="mb-0">
                        <li>Create a dedicated project folder per study and keep raw data read-only to avoid accidental overwrites.</li>
                        <li>Use <code>renv</code> or <code>packrat</code> to lock package versions for reproducibility.</li>
                        <li>Export cleaned datasets or figures back to the platform using the upload tools so collaborators can access them.</li>
                        <li>Set up scheduled clean-up of temporary files to stay within storage limits and keep the workspace responsive.</li>
                    </ul>
                </div>
            </div>
        </section>

        <section class="mb-5">
            <h2 class="h4 mb-3 text-success">Data Lifecycle Overview</h2>
            <div class="card shadow-sm border-0">
                <div class="card-body">
                    <ol class="mb-0">
                        <li class="mb-3"><strong>Plan</strong> – Identify required datasets, confirm governance approvals, and align timelines across owners and contributors.</li>
                        <li class="mb-3"><strong>Publish</strong> – Owners or contributors upload data, finalize metadata, and set initial access rules.</li>
                        <li class="mb-3"><strong>Review &amp; Approve</strong> – DAC Staff validate requests, confirm consent documents, and document decisions.</li>
                        <li class="mb-3"><strong>Analyze</strong> – Users download, analyze in R in JupyterHub, and generate outputs while adhering to policy.</li>
                        <li class="mb-0"><strong>Report</strong> – Teams share findings, update dataset records with impact summaries, and archive superseded files.</li>
                    </ol>
                </div>
            </div>
        </section>

        <section class="mb-5">
            <h2 class="h5 text-muted text-uppercase">Support &amp; Additional Resources</h2>
            <div class="row g-3">
                <div class="col-md-6">
                    <div class="card shadow-sm border-0 h-100">
                        <div class="card-body">
                            <h3 class="h6 text-primary"><i class="fas fa-book me-2"></i>User Guide</h3>
                            <p class="text-muted mb-0">Jump back to the top of this guide for role walkthroughs and checklists.</p>
                        </div>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="card shadow-sm border-0 h-100">
                        <div class="card-body">
                            <h3 class="h6 text-success"><i class="fas fa-headset me-2"></i>Need help?</h3>
                            <p class="text-muted mb-0">Use the Contact Us administration page or reach out to the Data Access Committee via the official communication channels.</p>
                        </div>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="card shadow-sm border-0 h-100">
                        <div class="card-body">
                            <h3 class="h6 text-primary"><i class="fas fa-shield-alt me-2"></i>Security &amp; Compliance</h3>
                            <p class="text-muted mb-0">Follow institutional policies when handling restricted datasets. Audit trails (downloads, approvals) are logged for every action.</p>
                        </div>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="card shadow-sm border-0 h-100">
                        <div class="card-body">
                            <h3 class="h6 text-warning"><i class="fas fa-lightbulb me-2"></i>Training &amp; Knowledge Base</h3>
                            <p class="text-muted mb-0">Access recorded webinars, SOPs, and quick-reference guides from the <em>Resources</em> page in the portal.</p>
                        </div>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="card shadow-sm border-0 h-100">
                        <div class="card-body">
                            <h3 class="h6 text-danger"><i class="fas fa-exclamation-triangle me-2"></i>Incident Response</h3>
                            <p class="text-muted mb-0">For suspected breaches or data quality issues, escalate immediately through the <strong>Incident Report</strong> form so the DAC can coordinate remediation.</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <?php include_once("includes/footer_public.php"); ?>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
