<?php
// product_view.php
require 'config/db.php';


$product_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$current_user_id = $_SESSION['user_id'] ?? 0;

try {
    $stmt = $pdo->prepare("
        SELECT p.*, u.username as seller_name 
        FROM products p 
        JOIN users u ON p.seller_id = u.id 
        WHERE p.id = ? LIMIT 1
    ");
    $stmt->execute([$product_id]);
    $product_data = $stmt->fetch();

    if (!$product_data) { 
        die("<div style='color: white; text-align: center; margin-top: 50px;'>Item not found.</div>"); 
    }
} catch (PDOException $e) { die("Error."); }

include 'includes/header.php';
?>

<div style="max-width: 900px; margin: 50px auto; padding: 20px; font-family: 'Inter', sans-serif;">
    <div style="background: #161b22; border: 1px solid #30363d; border-radius: 20px; display: grid; grid-template-columns: 1fr 1fr; overflow: hidden;">
        
        <div style="background: #0d1117; display: flex; align-items: center; justify-content: center; border-right: 1px solid #30363d;">
            <?php $img = $product_data['image']; ?>
            <img src="<?= (file_exists($img) && !empty($img)) ? $img : 'assets/img/no-image.png' ?>" style="width: 100%; height: 100%; object-fit: cover;">
        </div>

        <div style="padding: 40px;">
            <h1 style="color: white; font-family: 'Syncopate', sans-serif; font-size: 22px;"><?= htmlspecialchars($product_data['title']) ?></h1>
            <p style="color: #8b949e; font-size: 14px; margin: 20px 0;"><?= nl2br(htmlspecialchars($product_data['description'])) ?></p>
            
            <div style="margin: 25px 0; border-top: 1px solid #30363d; padding-top: 20px;">
                <span style="font-size: 32px; color: #00ff88; font-weight: 900;">$<?= number_format($product_data['price'], 2) ?></span>
                <p style="color: #8b949e; font-size: 11px; margin-top: 5px;">STOCK: <?= $product_data['stock'] ?> UNITS</p>
            </div>

            <?php if ($current_user_id == $product_data['seller_id']): ?>
                <a href="edit_item.php?id=<?= $product_id ?>" style="display:block; text-align:center; background:#30363d; color:white; padding:15px; border-radius:10px; text-decoration:none; font-weight:800;">MANAGE ITEM</a>
            <?php elseif ($product_data['stock'] > 0): ?>
                <form action="buy_process.php" method="POST">
                    <input type="hidden" name="product_id" value="<?= $product_id ?>">
                    <div style="margin-bottom: 15px;">
                        <label style="color: #8b949e; font-size: 10px; font-weight: 800; display: block; margin-bottom: 5px;">QUANTITY</label>
                        <input type="number" name="qty" value="1" min="1" max="<?= $product_data['stock'] ?>" 
                               style="width: 70px; background: #0d1117; border: 1px solid #30363d; color: white; padding: 8px; border-radius: 5px;">
                    </div>
                    <button type="submit" style="width: 100%; padding: 15px; background: #00ff88; color: black; border: none; border-radius: 10px; font-weight: 900; cursor: pointer;">PURCHASE NOW</button>
                </form>
            <?php else: ?>
                <button disabled style="width: 100%; padding: 15px; background: #222; color: #555; border-radius: 10px; cursor: not-allowed;">OUT OF STOCK</button>
            <?php endif; ?>
        </div>
    </div>
</div>