<?php
/**
 * Agentic Square - Seller Framework Asset Matrix Output Data Table
 * Path: seller/seller-item-table.php
 */

if (!isset($pdo) || !isset($user_id)) {
    require_once __DIR__ . '/../config/config.php';
    $user_id = (int)($_SESSION['user_id'] ?? 0);
}

// Dynamic extraction checking match authorizations on profile entries
$stmtSeller = $pdo->prepare("SELECT id FROM sellers WHERE user_id = ? LIMIT 1");
$stmtSeller->execute([$user_id]);
$currentSeller = $stmtSeller->fetch();

$myItems = [];

if ($currentSeller) {
    $seller_id = (int)$currentSeller['id'];
    
    // Authorization Match Core Logic Block
    // Grabs products *only* if both structural components perfectly check out
    $itemsQuery = $pdo->prepare("SELECT * FROM items WHERE user_id = ? AND seller_id = ? ORDER BY id DESC");
    $itemsQuery->execute([$user_id, $seller_id]);
    $myItems = $itemsQuery->fetchAll();
}
?>

<div class="glass-card p-6 rounded-2xl border border-slate-800/80">
    <div class="flex items-center justify-between mb-6">
        <div class="flex items-center space-x-2.5">
            <div class="w-2 h-2 rounded-full bg-nftAccentCyan animate-ping"></div>
            <h4 class="font-bold text-base text-white tracking-wide">Live Framework Architecture Repository Inventory</h4>
        </div>
    </div>

    <div class="overflow-x-auto">
        <?php if (empty($myItems)): ?>
            <div class="text-center py-10 text-nftTextGray text-xs font-medium space-y-2">
                <i class="fa-solid fa-folder-open text-3xl block text-slate-800 mb-2"></i>
                No catalog nodes uploaded to database repository arrays yet.
            </div>
        <?php else: ?>
            <table class="w-full text-left text-sm border-collapse">
                <thead>
                    <tr class="border-b border-slate-900 text-xs font-bold text-nftTextGray uppercase tracking-wider">
                        <th class="pb-3 pl-2 w-12">SL</th>
                        <th class="pb-3 w-20">Asset Image</th>
                        <th class="pb-3">Product Asset Name</th>
                        <th class="pb-3 w-32">License Price</th>
                        <th class="pb-3 w-32">Traffic Views</th>
                        <th class="pb-3 w-24">Status</th>
                        <th class="pb-3 text-right pr-2 w-28">Actions</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-900/50 text-xs text-slate-300 font-medium">
                    <?php 
                    $sl = 1;
	                    foreach ($myItems as $item): 
	                        // Safe image preview path fallback resolver logic pipeline
	                        $imagePath = !empty($item['feature_image']) ? '../' . $item['feature_image'] : '../assets/img/placeholder.png';
	                        $itemDetailsUrl = item_url($item);
	                    ?>
                        <tr class="hover:bg-slate-900/20 transition-colors group">
                            <td class="py-4 pl-2 font-mono font-bold text-slate-500"><?php echo $sl++; ?></td>
                            
                            <td class="py-2">
                                <div class="w-12 h-10 rounded-lg bg-slate-950 border border-slate-900 overflow-hidden relative shadow-md">
                                    <img src="<?php echo htmlspecialchars($imagePath); ?>" alt="Asset Visual Feature" class="w-full h-full object-cover">
                                </div>
                            </td>
                            
	                            <td class="py-4 font-bold text-white group-hover:text-nftAccentCyan transition-colors">
	                                <a href="<?php echo htmlspecialchars($itemDetailsUrl, ENT_QUOTES, 'UTF-8'); ?>" target="_blank" rel="noopener" class="truncate max-w-xs sm:max-w-md lg:max-w-lg block" title="<?php echo htmlspecialchars($item['item_name']); ?>">
	                                    <?php echo htmlspecialchars($item['item_name']); ?>
	                                </a>
	                            </td>
                            
                            <td class="py-4 font-extrabold font-mono text-emerald-400">
                                <?php echo htmlspecialchars(format_currency($item['amount']), ENT_QUOTES, 'UTF-8'); ?>
                            </td>

                            <td class="py-4 font-bold font-mono text-nftAccentCyan">
                                <?php echo number_format($item['views'] ?? 0); ?> Views
                            </td>

                            <td class="py-4">
                                <?php if (($item['status'] ?? 'active') === 'active'): ?>
                                    <span class="bg-emerald-500/10 border border-emerald-500/20 text-emerald-400 px-2 py-0.5 rounded-md text-[9px] font-bold uppercase">Active</span>
                                <?php else: ?>
                                    <span class="bg-rose-500/10 border border-rose-500/20 text-rose-400 px-2 py-0.5 rounded-md text-[9px] font-bold uppercase"><?php echo htmlspecialchars($item['status']); ?></span>
                                <?php endif; ?>
                            </td>
                            
                            <td class="py-4 text-right pr-2">
                                <a href="update-item.php?id=<?php echo $item['id']; ?>" class="inline-flex items-center justify-center bg-slate-900 hover:bg-slate-800 border border-slate-800 hover:border-slate-700 text-nftAccentCyan px-3 py-1.5 rounded-lg text-[11px] font-bold transition-all shadow-sm group/btn gap-1.5">
                                    <i class="fa-solid fa-pen-to-square text-[10px] text-slate-500 group-hover/btn:text-nftAccentCyan transition-colors"></i> 
                                    Update
                                </a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        <?php endif; ?>
    </div>
</div>
