<?php
/**
 * Public Landing Page - Survey Discovery & Transparency
 * Research Survey Management System
 *
 * This is the dedicated landing page for public visitors to
 * browse available surveys and learn about research practices.
 */

define('PAGE_TITLE', 'Home');
require_once __DIR__ . '/includes/functions.php';

$pdo = getDBConnection();

// Get available surveys (active status, currently accepting responses)
$stmt = $pdo->prepare("
    SELECT s.*, u.full_name as creator_name,
           COUNT(DISTINCT sq.question_id) as question_count,
           COUNT(DISTINCT sr.response_id) as response_count
    FROM surveys s
    JOIN users u ON s.created_by = u.user_id
    LEFT JOIN survey_questions sq ON s.survey_id = sq.survey_id
    LEFT JOIN survey_responses sr ON s.survey_id = sr.survey_id
    WHERE s.status = 'active'
    GROUP BY s.survey_id
    ORDER BY s.created_at DESC
");
$stmt->execute();
$availableSurveys = $stmt->fetchAll();

// Get survey statistics for transparency section
$stmt = $pdo->prepare("SELECT COUNT(*) FROM surveys WHERE status = 'active'");
$stmt->execute();
$activeSurveyCount = $stmt->fetchColumn();

$stmt = $pdo->prepare("SELECT COUNT(*) FROM survey_responses WHERE status = 'completed'");
$stmt->execute();
$totalResponses = $stmt->fetchColumn();

$stmt = $pdo->prepare("SELECT COUNT(DISTINCT user_id) FROM users WHERE is_active = 1");
$stmt->execute();
$activeResearchers = $stmt->fetchColumn();

require_once __DIR__ . '/includes/header.php';
?>

<!-- Hero Section - Matches LMS home.php hero -->
<section class="hero-section">
    <div class="container">
        <div class="row justify-content-center text-center">
            <div class="col-lg-8">
                <h1>
                    <i class="fas fa-poll-h me-2"></i>
                    <?php echo APP_NAME; ?>
                </h1>
                <p class="lead">
                    Participate in ongoing research surveys and contribute to impactful studies.
                    Your insights help advance knowledge and improve decision-making.
                </p>
                <a href="#surveys" class="btn btn-light btn-lg px-4">
                    <i class="fas fa-list me-2"></i> Browse Available Surveys
                </a>
            </div>
        </div>
    </div>
</section>

<!-- Available Surveys Section -->
<section id="surveys" class="py-5">
    <div class="container">
        <div class="section-header">
            <h2>
                <i class="fas fa-clipboard-list me-2 text-primary"></i>
                Available Surveys
            </h2>
            <p>Active surveys: <?php echo count($availableSurveys); ?></p>
        </div>

        <?php if (empty($availableSurveys)): ?>
        <div class="empty-state">
            <i class="fas fa-inbox"></i>
            <h5>No surveys currently available</h5>
            <p>Check back soon for new research opportunities.</p>
        </div>
        <?php else: ?>
        <div class="row g-4">
            <?php foreach ($availableSurveys as $survey): ?>
            <div class="col-md-6 col-lg-4">
                <div class="card border-0 shadow-sm h-100 survey-card">
                    <div class="card-body d-flex flex-column">
                        <div class="d-flex justify-content-between align-items-start mb-2">
                            <h5 class="card-title fw-bold mb-0">
                                <?php echo htmlspecialchars($survey['title']); ?>
                            </h5>
                            <?php echo getStatusBadge($survey['status']); ?>
                        </div>

                        <?php if ($survey['description']): ?>
                        <p class="card-text text-muted small mb-2 flex-grow-0">
                            <?php echo htmlspecialchars(substr($survey['description'], 0, 120)); ?>
                            <?php if (strlen($survey['description']) > 120): ?>...<?php endif; ?>
                        </p>
                        <?php else: ?>
                        <p class="card-text text-muted small mb-2">No description provided.</p>
                        <?php endif; ?>

                        <div class="mt-3 mb-3">
                            <span class="badge bg-info text-capitalize">
                                <i class="fas fa-tag me-1"></i><?php echo $survey['survey_type']; ?>
                            </span>
                            <span class="badge bg-secondary">
                                <i class="fas fa-question-circle me-1"></i><?php echo $survey['question_count']; ?> questions
                            </span>
                            <span class="badge bg-success">
                                <i class="fas fa-reply me-1"></i><?php echo $survey['response_count']; ?> responses
                            </span>
                        </div>

                        <div class="d-flex justify-content-between align-items-center mt-auto pt-2 border-top">
                            <small class="text-muted">
                                <i class="fas fa-user me-1"></i><?php echo htmlspecialchars($survey['creator_name']); ?>
                            </small>
                            <a href="<?php echo BASE_URL; ?>/pages/take_survey.php?id=<?php echo $survey['survey_id']; ?>"
                               class="btn btn-primary btn-sm">
                                <i class="fas fa-play me-1"></i> Start Survey
                            </a>
                        </div>
                    </div>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
        <?php endif; ?>
    </div>
</section>

<!-- Transparency Section -->
<section id="transparency" class="py-5" style="background: #f8fafc;">
    <div class="container">
        <div class="section-header">
            <h2>
                <i class="fas fa-shield-alt me-2 text-primary"></i>
                Research Transparency & Data Privacy
            </h2>
            <p>Building trust through open and ethical research practices</p>
        </div>

        <div class="row g-4">
            <!-- How Surveys Are Administered -->
            <div class="col-md-4">
                <div class="card border-0 shadow-sm h-100">
                    <div class="card-body text-center p-4">
                        <div class="bg-primary bg-opacity-10 rounded-circle d-inline-flex align-items-center justify-content-center mb-3" style="width: 60px; height: 60px;">
                            <i class="fas fa-cog fa-2x text-primary"></i>
                        </div>
                        <h5 class="card-title">Survey Administration</h5>
                        <p class="card-text small text-muted">
                            All surveys are created and managed by verified researchers.
                            Each survey undergoes a review process before publication to ensure
                            ethical standards and research integrity.
                        </p>
                    </div>
                </div>
            </div>

            <!-- Data Privacy Measures -->
            <div class="col-md-4">
                <div class="card border-0 shadow-sm h-100">
                    <div class="card-body text-center p-4">
                        <div class="bg-success bg-opacity-10 rounded-circle d-inline-flex align-items-center justify-content-center mb-3" style="width: 60px; height: 60px;">
                            <i class="fas fa-lock fa-2x text-success"></i>
                        </div>
                        <h5 class="card-title">Data Privacy</h5>
                        <p class="card-text small text-muted">
                            Your responses are encrypted in transit and at rest. Data is stored
                            securely and used solely for research purposes. Anonymous surveys
                            collect no personally identifiable information.
                        </p>
                    </div>
                </div>
            </div>

            <!-- Research Purpose -->
            <div class="col-md-4">
                <div class="card border-0 shadow-sm h-100">
                    <div class="card-body text-center p-4">
                        <div class="bg-info bg-opacity-10 rounded-circle d-inline-flex align-items-center justify-content-center mb-3" style="width: 60px; height: 60px;">
                            <i class="fas fa-bullseye fa-2x text-info"></i>
                        </div>
                        <h5 class="card-title">Research Purpose</h5>
                        <p class="card-text small text-muted">
                            Surveys on this platform support academic and institutional research.
                            Results contribute to publications, policy recommendations, and
                            evidence-based decision making across various fields.
                        </p>
                    </div>
                </div>
            </div>
        </div>

        <!-- Detailed Privacy Information -->
        <div class="row g-4 mt-2">
            <div class="col-12">
                <div class="card border-0 shadow-sm">
                    <div class="card-body">
                        <h5 class="mb-3">
                            <i class="fas fa-file-contract me-2 text-primary"></i>
                            Privacy Commitment
                        </h5>
                        <ul class="list-unstyled">
                            <li class="mb-2">
                                <i class="fas fa-check text-success me-2"></i>
                                <strong>No sale of data:</strong> Your responses are never sold or shared with third parties for marketing.
                            </li>
                            <li class="mb-2">
                                <i class="fas fa-check text-success me-2"></i>
                                <strong>Transparent use:</strong> All data usage is described in each survey's consent information.
                            </li>
                            <li class="mb-2">
                                <i class="fas fa-check text-success me-2"></i>
                                <strong>Your rights:</strong> You may withdraw your consent and request data deletion at any time by contacting the research team.
                            </li>
                            <li class="mb-2">
                                <i class="fas fa-check text-success me-2"></i>
                                <strong>Secure storage:</strong> All data is stored using industry-standard encryption and access controls.
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

<!-- Platform Statistics -->
<section class="py-5">
    <div class="container">
        <div class="section-header">
            <h2>
                <i class="fas fa-chart-bar me-2 text-primary"></i>
                Platform Statistics
            </h2>
            <p>Trusted by researchers and participants worldwide</p>
        </div>

        <div class="row g-4 text-center">
            <div class="col-4">
                <div class="stat-card">
                    <i class="fas fa-poll fa-3x text-primary mb-3"></i>
                    <div class="stat-number text-primary"><?php echo $activeSurveyCount; ?></div>
                    <div class="stat-label">Active Surveys</div>
                </div>
            </div>
            <div class="col-4">
                <div class="stat-card">
                    <i class="fas fa-reply-all fa-3x text-success mb-3"></i>
                    <div class="stat-number text-success"><?php echo $totalResponses; ?></div>
                    <div class="stat-label">Total Responses</div>
                </div>
            </div>
            <div class="col-4">
                <div class="stat-card">
                    <i class="fas fa-users fa-3x" style="color: #6366f1;"></i>
                    <div class="stat-number" style="color: #6366f1;"><?php echo $activeResearchers; ?></div>
                    <div class="stat-label">Active Researchers</div>
                </div>
            </div>
        </div>
    </div>
</section>

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