<?php
/**
 * GET /api/reviews/vendor.php?vendor_id=123
 * Public. Aggregate rating across every product a vendor has ever sold -
 * useful on a business profile as an overall reputation signal, separate
 * from any single product's rating.
 */

require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../includes/response.php';

$vendorId = (int) ($_GET['vendor_id'] ?? 0);
if ($vendorId <= 0) {
    send_error('A valid vendor_id is required.', 422);
}

$pdo = Database::connect();
$stmt = $pdo->prepare('
    SELECT AVG(pr.rating) AS avg_rating, COUNT(*) AS review_count
    FROM product_reviews pr
    JOIN products p ON p.id = pr.product_id
    WHERE p.owner_id = ?
');
$stmt->execute([$vendorId]);
$row = $stmt->fetch();

send_json(true, 'OK', [
    'average_rating' => $row['avg_rating'] ? round((float) $row['avg_rating'], 1) : 0,
    'review_count' => (int) $row['review_count'],
]);
