<?php

require_once __DIR__ . '/../app/config/config.php';
require_once __DIR__ . '/../app/config/database.php';
require_once __DIR__ . '/../app/helpers/functions.php';
require_once __DIR__ . '/../app/helpers/auth.php';

require_customer();

$pageTitle = 'My Account | LOVE-U Essentials';
$pageDescription = 'Manage your LOVE-U Essentials account, orders, wishlist, loyalty points, and referrals.';

$currentCustomer = customer();

$stmt = $pdo->prepare("
    SELECT c.*, cl.points_balance, cl.lifetime_points
    FROM customers c
    LEFT JOIN customer_loyalty cl ON c.id = cl.customer_id
    WHERE c.id = ?
    LIMIT 1
");
$stmt->execute([$currentCustomer['id']]);
$customerData = $stmt->fetch();

$stmt = $pdo->prepare("
    SELECT *
    FROM orders
    WHERE customer_id = ? OR customer_email = ?
    ORDER BY id DESC
");
$stmt->execute([$currentCustomer['id'], $currentCustomer['email']]);
$orders = $stmt->fetchAll();

$stmt = $pdo->prepare("
    SELECT wi.created_at, p.product_name, p.slug,
        (
            SELECT image_path
            FROM product_images
            WHERE product_id = p.id
            ORDER BY is_primary DESC, sort_order ASC, id ASC
            LIMIT 1
        ) AS primary_image
    FROM wishlist_items wi
    INNER JOIN products p ON wi.product_id = p.id
    WHERE wi.customer_id = ?
    ORDER BY wi.id DESC
");
$stmt->execute([$currentCustomer['id']]);
$wishlistItems = $stmt->fetchAll();

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

<section class="lue-account-page">
    <div class="lue-account-orb lue-account-orb-one"></div>
    <div class="lue-account-orb lue-account-orb-two"></div>

    <div class="container">
        <?php if (isset($_GET['registered'])): ?>
            <div class="lue-account-alert">Account created successfully. Welcome to LOVE-U Essentials.</div>
        <?php endif; ?>

        <div class="lue-account-hero">
            <div>
                <div class="eyebrow">My Account</div>
                <h1>Hello, <?= e($customerData['first_name']); ?></h1>
                <p>Manage your orders, wishlist, loyalty points, and referral code from one soft, simple account space.</p>
            </div>

            <a href="<?= e(BASE_URL); ?>/public/logout.php" class="lue-account-logout">Logout</a>
        </div>

        <div class="lue-account-stats">
            <div class="lue-account-stat">
                <span>Total Orders</span>
                <strong><?= e(count($orders)); ?></strong>
                <p>Your completed and active order history.</p>
            </div>

            <div class="lue-account-stat">
                <span>Loyalty Points</span>
                <strong><?= e($customerData['points_balance'] ?? 0); ?></strong>
                <p>Points earned through LOVE-U rewards.</p>
            </div>

            <div class="lue-account-stat">
                <span>Wishlist</span>
                <strong><?= e(count($wishlistItems)); ?></strong>
                <p>Your saved LOVE-U favourites.</p>
            </div>

            <div class="lue-account-stat lue-account-ref-code">
                <span>Referral Code</span>
                <strong><?= e($customerData['referral_code'] ?? '-'); ?></strong>
                <p>Share your code with friends.</p>
            </div>
        </div>

        <div class="lue-account-layout">
            <div class="lue-account-main">
                <?php if (!empty($currentCustomer['referral_code'])): ?>
                    <section class="lue-account-panel lue-referral-panel">
                        <div class="lue-panel-head">
                            <div>
                                <span>Invite & Earn</span>
                                <h2>Your Referral Link</h2>
                            </div>
                        </div>

                        <p>Share this link with a friend. They can use it when creating their LOVE-U Essentials account.</p>

                        <div class="lue-referral-copy">
                            <input
                                type="text"
                                id="referralLinkInput"
                                value="<?= e(BASE_URL . '/public/register.php?ref=' . $currentCustomer['referral_code']); ?>"
                                readonly
                            >

                            <button type="button" onclick="copyReferralLink()">Copy Link</button>
                        </div>

                        <small id="referralCopyMessage"></small>
                    </section>
                <?php endif; ?>

                <section class="lue-account-panel">
                    <div class="lue-panel-head">
                        <div>
                            <span>Recent Activity</span>
                            <h2>Order History</h2>
                        </div>

                        <a href="<?= e(BASE_URL); ?>/public/shop.php">Continue Shopping</a>
                    </div>

                    <?php if (!$orders): ?>
                        <div class="lue-account-empty">
                            <span>No orders yet</span>
                            <h3>Your order history is waiting.</h3>
                            <p>Start shopping LOVE-U Essentials and your orders will appear here.</p>
                            <a href="<?= e(BASE_URL); ?>/public/shop.php" class="btn">Shop Now</a>
                        </div>
                    <?php else: ?>
                        <div class="lue-order-list">
                            <?php foreach ($orders as $order): ?>
                                <div class="lue-order-card">
                                    <div>
                                        <span>Order</span>
                                        <strong><?= e($order['order_number']); ?></strong>
                                    </div>

                                    <div>
                                        <span>Total</span>
                                        <strong><?= e(format_price($order['total_amount'], $order['currency_code'])); ?></strong>
                                    </div>

                                    <div>
                                        <span>Payment</span>
                                        <strong><?= e(ucfirst($order['payment_status'])); ?></strong>
                                    </div>

                                    <div>
                                        <span>Status</span>
                                        <strong class="lue-status-pill"><?= e(ucfirst($order['order_status'])); ?></strong>
                                    </div>

                                    <div>
                                        <span>Date</span>
                                        <strong><?= e(date('d M Y', strtotime($order['created_at']))); ?></strong>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        </div>
                    <?php endif; ?>
                </section>

                <section class="lue-account-panel">
                    <div class="lue-panel-head">
                        <div>
                            <span>Saved Products</span>
                            <h2>Wishlist</h2>
                        </div>

                        <a href="<?= e(BASE_URL); ?>/public/shop.php">Browse Products</a>
                    </div>

                    <?php if (!$wishlistItems): ?>
                        <div class="lue-account-empty">
                            <span>No wishlist items</span>
                            <h3>Save your favourites for later.</h3>
                            <p>Tap wishlist on products you love and they’ll appear here.</p>
                        </div>
                    <?php else: ?>
                        <div class="lue-wishlist-grid">
                            <?php foreach ($wishlistItems as $item): ?>
                                <a href="<?= e(BASE_URL); ?>/public/product.php?slug=<?= e($item['slug']); ?>" class="lue-wishlist-card">
                                    <div class="lue-wishlist-image">
                                        <?php if (!empty($item['primary_image'])): ?>
                                            <img src="<?= e(BASE_URL . '/' . $item['primary_image']); ?>" alt="<?= e($item['product_name']); ?>">
                                        <?php else: ?>
                                            <div class="product-placeholder"><?= e($item['product_name']); ?></div>
                                        <?php endif; ?>
                                    </div>

                                    <div>
                                        <h3><?= e($item['product_name']); ?></h3>
                                        <p>Added <?= e(date('d M Y', strtotime($item['created_at']))); ?></p>
                                    </div>
                                </a>
                            <?php endforeach; ?>
                        </div>
                    <?php endif; ?>
                </section>
            </div>

            <aside class="lue-account-sidebar">
                <section class="lue-account-panel lue-account-details-panel">
                    <div class="lue-panel-head">
                        <div>
                            <span>Profile</span>
                            <h2>Account Details</h2>
                        </div>
                    </div>

                    <div class="lue-profile-avatar">
                        <?= e(strtoupper(substr($customerData['first_name'] ?? 'L', 0, 1))); ?>
                    </div>

                    <div class="lue-profile-name">
                        <h3><?= e(trim($customerData['first_name'] . ' ' . $customerData['last_name'])); ?></h3>
                        <p><?= e($customerData['email']); ?></p>
                    </div>

                    <div class="lue-profile-list">
                        <div>
                            <span>Phone</span>
                            <strong><?= e($customerData['phone'] ?? '-'); ?></strong>
                        </div>

                        <div>
                            <span>Status</span>
                            <strong><?= e(ucfirst($customerData['status'])); ?></strong>
                        </div>

                        <div>
                            <span>Lifetime Points</span>
                            <strong><?= e($customerData['lifetime_points'] ?? 0); ?></strong>
                        </div>
                    </div>

                    <a href="<?= e(BASE_URL); ?>/public/shop.php" class="btn lue-profile-shop-btn">Shop Essentials</a>
                </section>
            </aside>
        </div>
    </div>
</section>

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