<?php
/**
 * Agentic Square - Professional Merchant Reviews Management
 * Path: seller/reviews.php
 */
require_once __DIR__ . '/../config/config.php';

// 1. SESSION GUARD: Ensure user is a logged-in seller
if (!isset($_SESSION['user_id']) || ($_SESSION['user_role'] ?? '') !== 'seller') {
    header('Location: ../login.php');
    exit;
}

// 2. FETCH REVIEWS
// Using the logged-in user's ID to filter the reviews table directly
$seller_id_to_query = (int)$_SESSION['user_id']; 

// Pagination setup
$records_per_page = 10;
$current_page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($current_page - 1) * $records_per_page;

// Count total records for pagination
$countStmt = $pdo->prepare("SELECT COUNT(*) FROM reviews WHERE seller_id = ?");
$countStmt->execute([$seller_id_to_query]);
$total_records = $countStmt->fetchColumn();
$total_pages = (int)ceil($total_records / $records_per_page);

// Fetch paginated results
$revQuery = $pdo->prepare("SELECT * FROM reviews WHERE seller_id = ? ORDER BY created_at DESC LIMIT ? OFFSET ?");
$revQuery->bindValue(1, $seller_id_to_query, PDO::PARAM_INT);
$revQuery->bindValue(2, $records_per_page, PDO::PARAM_INT);
$revQuery->bindValue(3, $offset, PDO::PARAM_INT);
$revQuery->execute();
$reviews = $revQuery->fetchAll(PDO::FETCH_ASSOC);

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

<div class="min-h-screen bg-nftDark text-white antialiased flex flex-col lg:flex-row">
    <?php require_once __DIR__ . '/seller-sidebar.php'; ?>

    <div class="flex-1 min-w-0 flex flex-col z-10 lg:pl-64">
        <main class="flex-1 py-10 px-4 sm:px-6 lg:px-8 max-w-[1600px] w-full mx-auto space-y-8 mt-14 lg:mt-0">
            
            <div class="border-b border-slate-900/60 pb-6">
                <h1 class="text-3xl font-extrabold tracking-tight bg-clip-text text-transparent bg-gradient-to-r from-white via-white to-nftAccentPurple">Client Feedback Ledger</h1>
                <p class="text-sm text-nftTextGray mt-1">Review all verified client ratings and feedback statements for your listed assets.</p>
            </div>

            <div class="glass-card rounded-2xl border border-slate-800/80 shadow-2xl overflow-hidden bg-slate-950/20 backdrop-blur-md">
                <div class="overflow-x-auto">
                    <table class="w-full text-left border-collapse">
                        <thead>
                            <tr class="bg-slate-950/60 border-b border-slate-900 text-nftTextGray font-bold uppercase tracking-wider text-[10px]">
                                <th class="py-4 px-5 text-center">SL</th>
                                <th class="py-4 px-4">Date</th>
                                <th class="py-4 px-4">Item Name</th>
                                <th class="py-4 px-4">Client</th>
                                <th class="py-4 px-4 text-center">Rating</th>
                                <th class="py-4 px-5">Feedback Review</th>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-slate-900/40 text-xs font-medium">
                            <?php if (count($reviews) > 0): 
                                $sl = $offset + 1;
                                foreach ($reviews as $rev): ?>
                                <tr class="hover:bg-slate-900/40 transition-colors duration-150">
                                    <td class="py-4 px-5 text-center font-mono text-slate-500"><?php echo $sl++; ?></td>
                                    <td class="py-4 px-4 text-slate-300 whitespace-nowrap"><?php echo date('M d, Y', strtotime($rev['created_at'])); ?></td>
                                    <td class="py-4 px-4 text-white font-bold"><?php echo htmlspecialchars($rev['item_name']); ?></td>
                                    <td class="py-4 px-4 text-slate-300"><?php echo htmlspecialchars($rev['client_name']); ?></td>
                                    <td class="py-4 px-4 text-center text-nftAccentPurple">
                                        <?php for($i=0; $i<$rev['rating']; $i++) echo '<i class="fa-solid fa-star text-[10px]"></i>'; ?>
                                    </td>
                                    <td class="py-4 px-5 text-nftTextGray italic max-w-xs truncate">"<?php echo htmlspecialchars($rev['review_text']); ?>"</td>
                                </tr>
                            <?php endforeach; else: ?>
                                <tr><td colspan="6" class="py-16 text-center text-nftTextGray">No feedback records found.</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>

                <?php if ($total_pages > 1): ?>
                    <div class="px-6 py-4 border-t border-slate-900/60 bg-slate-950/40 flex items-center justify-between">
                        <div class="text-xs text-nftTextGray">Showing page <?php echo $current_page; ?> of <?php echo $total_pages; ?></div>
                        <div class="flex space-x-1.5">
                            <?php for ($p = 1; $p <= $total_pages; $p++): ?>
                                <a href="?page=<?php echo $p; ?>" class="px-3 py-1.5 rounded-lg border text-[11px] font-bold transition-all <?php echo $p == $current_page ? 'bg-nftAccentPurple border-transparent text-white' : 'bg-slate-900 border-slate-800 text-nftTextGray hover:border-slate-700'; ?>">
                                    <?php echo $p; ?>
                                </a>
                            <?php endfor; ?>
                        </div>
                    </div>
                <?php endif; ?>
            </div>
        </main>
    </div>
</div>
