<?php
/**
 * Customer Downloads Page
 * Digital Product Store
 */

require_once __DIR__ . '/config/database.php';

// Get customer email
$customer_email = isset($_GET['email']) ? sanitize($_GET['email']) : (isset($_SESSION['customer_email']) ? $_SESSION['customer_email'] : '');

if (!$customer_email) {
    header('Location: ' . BASE_URL . '/account.php');
    exit;
}

// Get customer info
$stmt = $pdo->prepare("SELECT * FROM customers WHERE email = ?");
$stmt->execute([$customer_email]);
$customer = $stmt->fetch();

// Get all completed orders for this customer
$stmt = $pdo->prepare("SELECT * FROM orders WHERE (customer_email = ? OR customer_id = ?) AND payment_status = 'completed' ORDER BY created_at DESC");
$stmt->execute([$customer_email, $customer ? $customer['id'] : 0]);
$orders = $stmt->fetchAll();

// Collect all downloadable items
$downloadable_items = [];
foreach ($orders as $order) {
    $items = get_order_items($order['id']);
    foreach ($items as $item) {
        $item['order_number'] = $order['order_number'];
        $downloadable_items[] = $item;
    }
}

// Page SEO
$page_title = 'My Downloads - ' . get_setting('site_name', 'Digital Store');
$page_description = 'Download your purchased digital products.';

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

<section class="section" style="padding: 3rem 0;">
    <div class="container">
        <nav aria-label="breadcrumb" class="mb-4">
            <ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="<?php echo BASE_URL; ?>/">Home</a></li>
                <li class="breadcrumb-item"><a href="<?php echo BASE_URL; ?>/account.php?email=<?php echo urlencode($customer_email); ?>">My Account</a></li>
                <li class="breadcrumb-item active">Downloads</li>
            </ol>
        </nav>
        
        <h1 class="mb-4">My Downloads</h1>
        
        <?php if (empty($downloadable_items)): ?>
        <div class="text-center py-5">
            <i class="fas fa-download fa-4x text-muted mb-4"></i>
            <h3>No downloads available</h3>
            <p class="text-muted">You don't have any purchased products to download yet.</p>
            <a href="<?php echo BASE_URL; ?>/products.php" class="btn btn-primary btn-lg mt-3">
                <i class="fas fa-shopping-bag"></i> Browse Products
            </a>
        </div>
        <?php else: ?>
        <div class="row g-4">
            <?php foreach ($downloadable_items as $item): 
                $can_download = check_download_limit($item['id'], $item['download_limit'] ?? 5);
                $remaining_downloads = ($item['download_limit'] ?? 5) - ($item['download_count'] ?? 0);
                $is_expired = strtotime($item['token_expires']) < time();
            ?>
            <div class="col-md-6 col-lg-4">
                <div class="card border-0 shadow-sm h-100">
                    <div class="card-body">
                        <div class="d-flex align-items-center mb-3">
                            <div class="download-icon-sm me-3" style="width: 50px; height: 50px; background: var(--primary-color); color: white; border-radius: 10px; display: flex; align-items: center; justify-content: center; font-size: 1.5rem;">
                                <i class="fas fa-file-archive"></i>
                            </div>
                            <div>
                                <h5 class="mb-1"><?php echo sanitize($item['product_name']); ?></h5>
                                <p class="text-muted mb-0">Order #<?php echo $item['order_number']; ?></p>
                            </div>
                        </div>
                        
                        <div class="mb-3">
                            <div class="d-flex justify-content-between text-sm">
                                <span class="text-muted">Downloads:</span>
                                <span><?php echo $item['download_count'] ?? 0; ?> / <?php echo $item['download_limit'] ?? 5; ?></span>
                            </div>
                            <div class="d-flex justify-content-between text-sm">
                                <span class="text-muted">Expires:</span>
                                <span class="<?php echo $is_expired ? 'text-danger' : ''; ?>"><?php echo format_date($item['token_expires']); ?></span>
                            </div>
                        </div>
                        
                        <?php if ($can_download && !$is_expired): ?>
                        <a href="<?php echo BASE_URL; ?>/download.php?token=<?php echo $item['download_token']; ?>&item=<?php echo $item['id']; ?>" class="btn btn-primary w-100">
                            <i class="fas fa-download"></i> Download Now
                        </a>
                        <?php elseif ($is_expired): ?>
                        <button class="btn btn-secondary w-100" disabled>
                            <i class="fas fa-clock"></i> Link Expired
                        </button>
                        <?php else: ?>
                        <button class="btn btn-secondary w-100" disabled>
                            <i class="fas fa-ban"></i> Limit Reached
                        </button>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
        
        <div class="alert alert-info mt-4">
            <i class="fas fa-info-circle"></i>
            <strong>Note:</strong> Download links expire after 7 days. Each product has a download limit. If you need help, please <a href="<?php echo BASE_URL; ?>/contact.php">contact support</a>.
        </div>
        <?php endif; ?>
    </div>
</section>

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