from django.db import models


class Conversation(models.Model):
    """
    审核智能体会话主对象。

    会话与资料包一一绑定，标题默认使用解析出的产品名称，
    节点结果使用 JSON 挂载，便于页面按节点展示。
    """

    STATUS_PENDING = "pending"
    STATUS_PROCESSING = "processing"
    STATUS_COMPLETED = "completed"
    STATUS_REVIEW_REQUIRED = "review_required"
    STATUS_BLOCKED = "blocked"
    STATUS_FAILED = "failed"

    conversation_id = models.CharField(max_length=64, unique=True, db_index=True)
    title = models.CharField(max_length=255)
    product_name = models.CharField(max_length=255, blank=True, db_index=True)
    batch_id = models.CharField(max_length=64, blank=True, db_index=True)
    task_status = models.CharField(max_length=32, default=STATUS_PENDING, db_index=True)
    node_results = models.JSONField(default=list, blank=True)
    latest_summary = models.JSONField(default=dict, blank=True)
    created_at = models.DateTimeField(auto_now_add=True, db_index=True)
    updated_at = models.DateTimeField(auto_now=True)
    last_run_at = models.DateTimeField(null=True, blank=True)

    class Meta:
        ordering = ["-updated_at", "-created_at"]

    def __str__(self) -> str:
        return self.title
