"""
Locust Stress Test cho Canifa Chatbot
=====================================

Chạy:
    locust -f locustfile.py --host http://172.16.2.207:5000

Mở browser:
    http://localhost:8089

Các scenario:
- ChatUser: Gửi câu hỏi random → chatbot API
- QuickBurst: Gửi liên tục không wait (extreme load)
"""

import random

from locust import HttpUser, between, tag, task

# ═══ Test questions ═══
QUESTIONS = [
    "Toi muon mua ao di lam, khong biet chon mau gi?",
    "Shop co vay du tiec khong?",
    "Ngan sach 500k mua duoc gi?",
    "Co ao polo nam size XL khong?",
    "Gia ao khoac mua dong bao nhieu?",
    "Co chuong trinh khuyen mai gi khong?",
    "Giao hang mat bao lau?",
    "Chinh sach doi tra the nao?",
    "Toi muon mua quan jeans nu",
    "Size chart cua Canifa nhu the nao?",
    "Co ao so mi trang nam khong?",
    "Ao khoac nu mua dong co nhung mau nao?",
    "Muon mua do cho be gai 5 tuoi",
    "Co san pham nao dang sale khong?",
    "Toi can tu van mix do di date",
]


class ChatUser(HttpUser):
    """
    User bình thường: chat 1 câu, đợi 2-5 giây, chat tiếp.
    Mô phỏng real user behavior.
    """
    wait_time = between(2, 5)

    @tag("chat-dev")
    @task(3)
    def chat_dev(self):
        """Gọi API chat-dev (development endpoint)"""
        q = random.choice(QUESTIONS)
        self.client.post(
            "/api/agent/chat-dev",
            json={"user_query": q},
            name="/api/agent/chat-dev",
            timeout=60,
        )

    @tag("chat-prod")
    @task(1)
    def chat_prod(self):
        """Gọi API chat (production endpoint)"""
        q = random.choice(QUESTIONS)
        self.client.post(
            "/api/agent/chat",
            json={"user_query": q},
            name="/api/agent/chat",
            timeout=60,
        )

    @tag("products")
    @task(2)
    def search_products(self):
        """Gọi API search products"""
        keywords = ["ao so mi", "quan jeans", "vay", "ao khoac", "polo", "dam"]
        self.client.get(
            "/api/products/list",
            params={"search": random.choice(keywords), "limit": 10},
            name="/api/products/list",
            timeout=15,
        )


class QuickBurst(HttpUser):
    """
    Extreme load: bắn liên tục không đợi.
    Dùng để test breaking point.
    """
    wait_time = between(0.1, 0.5)

    @task
    def burst_chat(self):
        q = random.choice(QUESTIONS)
        self.client.post(
            "/api/agent/chat-dev",
            json={"user_query": q},
            name="/api/agent/chat-dev [burst]",
            timeout=60,
        )
