<?php include 'header.php'; ?>

<div class="container py-3">
    <!-- Breadcrumb -->
    <nav aria-label="breadcrumb" class="mb-4">
        <ol class="breadcrumb mb-0 small">
            <li class="breadcrumb-item"><a href="/" class="text-success text-decoration-none"><?php echo __('home'); ?></a></li>
            <?php 
                $db = db_pdo();
                $catName = $db->query("SELECT name FROM categories WHERE id = " . ($apk['category_id'] ?: 0))->fetchColumn();
            ?>
            <li class="breadcrumb-item"><a href="/?category=<?php echo strtolower($catName); ?>" class="text-success text-decoration-none"><?php echo $catName ?: 'Apps'; ?></a></li>
            <li class="breadcrumb-item active text-muted" aria-current="page"><?php echo $apk['title']; ?></li>
        </ol>
    </nav>

    <div class="row g-4">
        <!-- Main Content -->
        <div class="col-lg-8">
            <div class="bg-white p-4 p-md-5 rounded-4 shadow-sm mb-4">
                <!-- Header Section -->
                <div class="d-flex align-items-start mb-4">
                    <?php 
                        $icon = !empty($apk['icon_path']) ? '/'.$apk['icon_path'] : $apk['image_url'];
                    ?>
                    <img src="<?php echo $icon; ?>" class="rounded-4 me-3 me-md-4 shadow-sm" width="100" height="100" alt="<?php echo $apk['title']; ?>" style="object-fit: cover; min-width: 100px;">
                    <div>
                        <h1 class="h2 fw-bold mb-1 d-flex align-items-center flex-wrap">
                            <?php echo $apk['title']; ?> 
                            <span class="badge bg-light text-muted fw-normal ms-2 fs-6">v<?php echo $apk['version']; ?></span>
                        </h1>
                        <p class="text-muted small mb-3"><?php echo __('official_version_text'); ?></p>
                        
                        <div class="d-flex flex-wrap gap-2 mb-0">
                            <span class="badge bg-success-subtle text-success border border-success-subtle px-2 py-1 fw-medium rounded">
                                <i class="bi bi-download me-1"></i> <?php echo number_format($apk['total_downloads']); ?> <?php echo __('downloads'); ?>
                            </span>
                            <span class="badge bg-info-subtle text-info border border-info-subtle px-2 py-1 fw-medium rounded">
                                <i class="bi bi-shield-check me-1"></i> <?php echo __('verified_safe'); ?>
                            </span>
                        </div>
                        <div class="mt-2 text-muted small">
                            <i class="bi bi-calendar3 me-1"></i> <?php echo date('M d, Y', strtotime($apk['created_at'])); ?>
                        </div>
                    </div>
                </div>

                <!-- Action Button -->
                <div class="mb-4">
                    <a href="/download/<?php echo $apk['slug']; ?>" target="_blank" class="btn btn-success btn-lg w-100 py-3 rounded-pill fw-bold shadow-sm mb-2">
                        <i class="bi bi-download me-2"></i> <?php echo __('download_now'); ?>
                    </a>
                    <p class="text-muted text-center x-small mt-2" style="font-size: 0.75rem;">
                        <?php echo __('agree_terms_text'); ?>
                    </p>
                </div>

                <!-- Description -->
                <div class="mb-5">
                    <h4 class="fw-bold h5 mb-3"><?php echo __('description'); ?></h4>
                    <div class="text-muted small lh-lg">
                        <?php echo nl2br(htmlspecialchars($apk['description'])); ?>
                    </div>
                </div>

                <!-- Features & Requirements Grid -->
                <div class="row g-3 mb-5">
                    <div class="col-md-6">
                        <div class="p-4 rounded-4 bg-light h-100 border-0">
                            <h6 class="fw-bold mb-3 d-flex align-items-center">
                                <span class="p-1 bg-white rounded shadow-sm me-2 d-flex align-items-center justify-content-center" style="width: 28px; height: 28px;">
                                    <i class="bi bi-star-fill text-warning" style="font-size: 0.8rem;"></i>
                                </span>
                                <?php echo __('main_features'); ?>
                            </h6>
                            <ul class="list-unstyled mb-0 text-muted small">
                                <li class="mb-2 d-flex align-items-center"><i class="bi bi-check-circle-fill text-success me-2"></i> <?php echo __('feature_original'); ?></li>
                                <li class="mb-2 d-flex align-items-center"><i class="bi bi-check-circle-fill text-success me-2"></i> <?php echo __('feature_no_extra'); ?></li>
                                <li class="mb-2 d-flex align-items-center"><i class="bi bi-check-circle-fill text-success me-2"></i> <?php echo __('feature_fast'); ?></li>
                                <li class="d-flex align-items-center"><i class="bi bi-check-circle-fill text-success me-2"></i> <?php echo __('feature_regular'); ?></li>
                            </ul>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="p-4 rounded-4 bg-light h-100 border-0">
                            <h6 class="fw-bold mb-3 d-flex align-items-center">
                                <span class="p-1 bg-white rounded shadow-sm me-2 d-flex align-items-center justify-content-center" style="width: 28px; height: 28px;">
                                    <i class="bi bi-gear-fill text-secondary" style="font-size: 0.8rem;"></i>
                                </span>
                                <?php echo __('system_requirements'); ?>
                            </h6>
                            <ul class="list-unstyled mb-0 text-muted small">
                                <li class="mb-2 d-flex align-items-center"><i class="bi bi-info-circle me-2"></i> <?php echo __('req_android'); ?></li>
                                <li class="mb-2 d-flex align-items-center"><i class="bi bi-memory me-2"></i> <?php echo __('req_ram'); ?></li>
                                <li class="mb-2 d-flex align-items-center"><i class="bi bi-hdd-network me-2"></i> <?php echo __('req_internet'); ?></li>
                                <li class="d-flex align-items-center"><i class="bi bi-cpu me-2"></i> <?php echo __('req_cpu'); ?></li>
                            </ul>
                        </div>
                    </div>
                </div>

                <!-- Safety Banner -->
                <div class="bg-success rounded-4 p-4 text-center">
                    <h6 class="fw-bold text-white mb-2"><?php echo __('safe_question'); ?></h6>
                    <p class="text-white text-opacity-75 small mb-0">
                        <?php echo __('safe_answer'); ?>
                    </p>
                </div>
            </div>
        </div>

        <!-- Sidebar / Additional Info -->
        <div class="col-lg-4">
            <div class="position-sticky" style="top: 5.5rem;">
                <div class="bg-white p-4 rounded-4 shadow-sm mb-4 text-center border-0 py-5">
                    <h5 class="fw-bold mb-3 h6"><?php echo __('share_earn'); ?></h5>
                    <p class="text-muted small mb-4 lh-sm"><?php echo __('share_earn_text'); ?></p>
                    <div class="input-group mb-3 border rounded-pill p-1">
                        <?php 
                            $ref = isset($_SESSION['user_id']) ? $db->query("SELECT referral_code FROM users WHERE id = ".$_SESSION['user_id'])->fetchColumn() : '';
                            $shareLink = 'http://'.$_SERVER['HTTP_HOST'].'/apk/'.$apk['slug'].($ref ? '?ref='.$ref : '');
                        ?>
                        <input type="text" class="form-control form-control-sm border-0 bg-transparent ps-3" id="shareLink" value="<?php echo $shareLink; ?>" readonly>
                        <button class="btn btn-success btn-sm rounded-pill px-3" type="button" onclick="copyShareLink()"><?php echo __('copy'); ?></button>
                    </div>
                    <?php if (!$ref): ?>
                        <a href="/login" class="small text-success text-decoration-none fw-medium"><?php echo __('login_to_earn'); ?></a>
                    <?php endif; ?>
                </div>

                <div class="bg-dark text-white p-4 rounded-4 shadow-sm text-center border-0">
                    <div class="bg-success bg-opacity-10 rounded-circle d-inline-flex p-3 mb-3">
                        <i class="bi bi-trophy text-success h4 mb-0"></i>
                    </div>
                    <h5 class="fw-bold mb-3 h6"><?php echo __('referral_program'); ?></h5>
                    <p class="small text-white-50 mb-4 lh-sm"><?php echo __('referral_program_text'); ?></p>
                    <a href="/register" class="btn btn-success fw-bold w-100 rounded-pill py-2 shadow-sm"><?php echo __('get_started'); ?></a>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
function copyShareLink() {
    var copyText = document.getElementById("shareLink");
    copyText.select();
    copyText.setSelectionRange(0, 99999);
    navigator.clipboard.writeText(copyText.value);
    
    // Simple toast or alert replacement
    const btn = event.target;
    const originalText = btn.innerText;
    btn.innerText = "<?php echo __('copy'); ?>ed!";
    btn.classList.replace('btn-success', 'btn-dark');
    setTimeout(() => {
        btn.innerText = originalText;
        btn.classList.replace('btn-dark', 'btn-success');
    }, 2000);
}
</script>

<?php include 'footer.php'; ?>