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


// 1. Security Check
if (!isset($_SESSION['user_id'])) { 
    header("Location: login.php"); 
    exit; 
}

$buyer_id = $_SESSION['user_id']; // Direct numeric ID from session
$seller_id = isset($_GET['seller_id']) ? (int)$_GET['seller_id'] : 0;

if ($buyer_id == $seller_id) { 
    die("<div style='color: white; text-align: center; margin-top: 50px;'>You cannot rate yourself. That would be too easy!</div>"); 
}

// 2. Fetch Seller Name for the UI
try {
    $stmt_s = $pdo->prepare("SELECT username FROM users WHERE id = ? LIMIT 1");
    $stmt_s->execute([$seller_id]);
    $seller = $stmt_s->fetch();

    if (!$seller) { 
        die("<div style='color: white; text-align: center; margin-top: 50px;'>Merchant not found in the Vault.</div>"); 
    }
} catch (PDOException $e) {
    die("Connection error.");
}

// 3. Process POST request
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $rating = (int)$_POST['rating'];
    $comment = trim($_POST['comment']);

    try {
        $ins = $pdo->prepare("INSERT INTO reviews (seller_id, buyer_id, rating, comment) VALUES (?, ?, ?, ?)");
        if ($ins->execute([$seller_id, $buyer_id, $rating, $comment])) {
            // Redirecting back to the seller's profile
            header("Location: profile.php?user_id=$seller_id&msg=rated");
            exit;
        }
    } catch (PDOException $e) {
        $error = "System Error: Could not submit review.";
    }
}

include 'includes/header.php'; 
?>

<div style="max-width: 500px; margin: 60px auto; background: var(--nav-bg); padding: 40px; border-radius: 20px; border: 1px solid var(--border); box-shadow: 0 15px 40px rgba(0,0,0,0.4); font-family: 'Inter', sans-serif;">
    
    <div style="text-align: center; margin-bottom: 30px;">
        <i class="fa-solid fa-star" style="color: var(--primary); font-size: 40px; margin-bottom: 15px;"></i>
        <h2 style="margin: 0; color: white; font-family: 'Syncopate', sans-serif; font-size: 20px; text-transform: uppercase;">Rate Merchant</h2>
        <p style="color: var(--text-gray); font-size: 13px; margin-top: 10px;">
            How was your transaction with <strong style="color: var(--primary);">@<?php echo htmlspecialchars($seller['username']); ?></strong>?
        </p>
    </div>

    <?php if(isset($error)): ?>
        <div style="background: rgba(255, 77, 77, 0.1); color: #ff4d4d; padding: 12px; border-radius: 8px; margin-bottom: 20px; font-size: 12px; text-align: center; border: 1px solid #ff4d4d;">
            <?php echo $error; ?>
        </div>
    <?php endif; ?>

    <form method="POST">
        <div style="margin-bottom: 25px;">
            <label style="display: block; margin-bottom: 10px; color: var(--text-gray); font-size: 11px; font-weight: 900; text-transform: uppercase; letter-spacing: 1px;">Reputation Score</label>
            <select name="rating" required style="width: 100%; padding: 15px; background: #0d1117; border: 1px solid var(--border); color: white; border-radius: 10px; font-weight: bold; cursor: pointer; outline: none;">
                <option value="5">⭐⭐⭐⭐⭐ - Flawless Execution</option>
                <option value="4">⭐⭐⭐⭐ - Great Experience</option>
                <option value="3">⭐⭐⭐ - Satisfactory</option>
                <option value="2">⭐⭐ - Subpar Service</option>
                <option value="1">⭐ - Defective / Scam Attempt</option>
            </select>
        </div>

        <div style="margin-bottom: 30px;">
            <label style="display: block; margin-bottom: 10px; color: var(--text-gray); font-size: 11px; font-weight: 900; text-transform: uppercase; letter-spacing: 1px;">Detailed Feedback</label>
            <textarea name="comment" rows="4" placeholder="Describe the delivery speed, item quality, or communication..." style="width: 100%; padding: 15px; background: #0d1117; border: 1px solid var(--border); color: white; border-radius: 10px; resize: none; font-size: 14px; line-height: 1.5; outline: none;" required></textarea>
        </div>

        <button type="submit" style="width: 100%; padding: 18px; background: var(--primary); border: none; border-radius: 12px; font-weight: 900; color: black; cursor: pointer; text-transform: uppercase; letter-spacing: 2px; transition: 0.3s; font-size: 14px;"
                onmouseover="this.style.opacity='0.8'" onmouseout="this.style.opacity='1'">
            SUBMIT TO VAULT
        </button>
        
        <a href="profile.php?user_id=<?php echo $seller_id; ?>" style="display: block; text-align: center; margin-top: 20px; color: var(--text-gray); text-decoration: none; font-size: 12px; font-weight: bold; opacity: 0.6;">
            Cancel & Return
        </a>
    </form>
</div>

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