<?php
csrf_check();

if (!is_customer()) {
    reply(false, 'Değerlendirme yapmak için giriş yapın.', ['redirect' => url('giris')]);
}

$product = db_one('SELECT id FROM products WHERE id = ? AND status = 1', [post_int('id')]);
$comment = mb_substr(post('comment', ''), 0, 2000);
$rating = min(5, max(1, post_int('rating', 5)));

if (!$product) {
    reply(false, 'Ürün bulunamadı.');
}
if (mb_strlen($comment) < 10) {
    reply(false, 'Yorumunuz en az 10 karakter olmalı.');
}
if (customer_reviewed($product['id'])) {
    reply(false, 'Bu ürünü zaten değerlendirdiniz.');
}

db_insert('reviews', [
    'product_id'  => $product['id'],
    'customer_id' => customer_id(),
    'name'        => current_customer()['name'],
    'rating'      => $rating,
    'comment'     => $comment,
]);

reply(true, 'Teşekkürler! Yorumunuz onaylandıktan sonra yayınlanacak.', ['reset' => true]);
