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

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

$page_title = 'My Account';

// Get email from query or session
$email = isset($_GET['email']) ? sanitize_email($_GET['email']) : (isset($_SESSION['customer_email']) ? $_SESSION['customer_email'] : '');

$customer = null;
$order_count = 0;
$total_spent = 0;

if ($email && is_valid_email($email)) {
    $_SESSION['customer_email'] = $email;
    
    // Get customer info
    $stmt = $pdo->prepare("SELECT * FROM customers WHERE email = ?");
    $stmt->execute([$email]);
    $customer = $stmt->fetch();
    
    // Get order stats
    $stmt = $pdo->prepare("SELECT COUNT(*) FROM orders WHERE customer_email = ? AND payment_status = 'completed'");
    $stmt->execute([$email]);
    $order_count = $stmt->fetchColumn();
    
    $stmt = $pdo->prepare("SELECT COALESCE(SUM(total), 0) FROM orders WHERE customer_email = ? AND payment_status = 'completed'");
    $stmt->execute([$email]);
    $total_spent = $stmt->fetchColumn();
}

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

<div class="container py-5">
    <h1 class="mb-4">My Account</h1>
    
    <?php if (empty($email)): ?>
    <div class="row justify-content-center">
        <div class="col-md-6">
            <div class="card border-0 shadow-sm">
                <div class="card-body text-center">
                    <i class="fas fa-user-circle fa-3x text-primary mb-3"></i>
                    <h4>Access Your Account</h4>
                    <p class="text-muted">Enter your email address to view your account details and orders.</p>
                    <form method="GET">
                        <div class="input-group mb-3">
                            <input type="email" name="email" class="form-control" placeholder="your@email.com" required>
                            <button type="submit" class="btn btn-primary">Access Account</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    <?php else: ?>
    <div class="alert alert-info">
        Account: <strong><?php echo sanitize($email); ?></strong>
        <a href="account.php" class="float-end">Change email</a>
    </div>
    
    <div class="row">
        <!-- Stats -->
        <div class="col-md-4 mb-4">
            <div class="card border-0 shadow-sm h-100">
                <div class="card-body text-center">
                    <i class="fas fa-shopping-bag fa-2x text-primary mb-3"></i>
                    <h3><?php echo $order_count; ?></h3>
                    <p class="text-muted mb-0">Total Orders</p>
                </div>
            </div>
        </div>
        <div class="col-md-4 mb-4">
            <div class="card border-0 shadow-sm h-100">
                <div class="card-body text-center">
                    <i class="fas fa-dollar-sign fa-2x text-success mb-3"></i>
                    <h3><?php echo format_price($total_spent); ?></h3>
                    <p class="text-muted mb-0">Total Spent</p>
                </div>
            </div>
        </div>
        <div class="col-md-4 mb-4">
            <div class="card border-0 shadow-sm h-100">
                <div class="card-body text-center">
                    <i class="fas fa-download fa-2x text-info mb-3"></i>
                    <h3>Instant</h3>
                    <p class="text-muted mb-0">Downloads</p>
                </div>
            </div>
        </div>
    </div>
    
    <div class="row">
        <div class="col-md-6 mb-4">
            <div class="card border-0 shadow-sm h-100">
                <div class="card-header bg-white">
                    <h5 class="mb-0">Quick Links</h5>
                </div>
                <div class="card-body">
                    <div class="list-group">
                        <a href="orders.php?email=<?php echo urlencode($email); ?>" class="list-group-item list-group-item-action">
                            <i class="fas fa-list me-2"></i> View My Orders
                        </a>
                        <a href="products.php" class="list-group-item list-group-item-action">
                            <i class="fas fa-shopping-bag me-2"></i> Browse Products
                        </a>
                        <a href="contact.php" class="list-group-item list-group-item-action">
                            <i class="fas fa-envelope me-2"></i> Contact Support
                        </a>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="col-md-6 mb-4">
            <div class="card border-0 shadow-sm h-100">
                <div class="card-header bg-white">
                    <h5 class="mb-0">Account Information</h5>
                </div>
                <div class="card-body">
                    <table class="table table-borderless">
                        <tr>
                            <th>Email:</th>
                            <td><?php echo sanitize($email); ?></td>
                        </tr>
                        <?php if ($customer): ?>
                        <tr>
                            <th>Name:</th>
                            <td><?php echo sanitize($customer['first_name'] . ' ' . $customer['last_name']); ?></td>
                        </tr>
                        <tr>
                            <th>Member Since:</th>
                            <td><?php echo format_date($customer['created_at']); ?></td>
                        </tr>
                        <?php endif; ?>
                    </table>
                </div>
            </div>
        </div>
    </div>
    <?php endif; ?>
</div>

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