import sys
import os
import random

# Добавляем путь к бэкенду, чтобы импортировать db
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

import db

def create_mock_reviews():
    print("Finding orders to add mock reviews...")
    
    # Берем последние 10 заказов (или любые доступные)
    orders = db.execute_query("SELECT id FROM orders ORDER BY created_at DESC LIMIT 15")
    
    if not orders:
        print("No orders found in database. Please create some orders first.")
        return

    review_templates = [
        {"rating": 5, "text": "Excellent quality! The surface is very smooth.", "lang": "en"},
        {"rating": 5, "text": "Отличное качество печати, очень доволен результатом!", "lang": "ru"},
        {"rating": 4, "text": "Good work, but took a bit longer than expected.", "lang": "en"},
        {"rating": 5, "text": "Превосходно! Деталь подошла идеально.", "lang": "ru"},
        {"rating": 3, "text": "Average quality, some support marks are visible.", "lang": "en"},
        {"rating": 4, "text": "Хорошее качество, буду заказывать еще.", "lang": "ru"},
        {"rating": 5, "text": "Fast shipping and great communication. Recommended!", "lang": "en"}
    ]

    count = 0
    for order in orders:
        # С вероятностью 70% добавляем отзыв
        if random.random() > 0.3:
            template = random.choice(review_templates)
            is_approved = random.choice([True, False]) # Разные статусы для теста админки
            
            db.execute_commit(
                "UPDATE orders SET review_text = %s, rating = %s, review_approved = %s WHERE id = %s",
                (template['text'], template['rating'], is_approved, order['id'])
            )
            print(f"  [DONE] Added review to Order #{order['id']} (Rating: {template['rating']}, Approved: {is_approved})")
            count += 1

    print(f"\nSuccessfully added {count} mock reviews.")

if __name__ == "__main__":
    create_mock_reviews()
