from django.db import models
import uuid

class SlackSettings(models.Model):
    """Stores the Slack App Token so users don't have to edit .env files."""
    bot_token = models.CharField(max_length=255, blank=True, null=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return "Slack Settings"

    class Meta:
        verbose_name_plural = "Slack Settings"

class MCPToolRequest(models.Model):
    STATUS_CHOICES = [
        ('PENDING', 'Pending'),
        ('SENT', 'Sent'),
        ('ERROR', 'Error'),
    ]
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    tool_name = models.CharField(max_length=100)
    arguments = models.JSONField()
    status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='PENDING')
    slack_channel = models.CharField(max_length=50, blank=True)
    slack_ts = models.CharField(max_length=50, blank=True, help_text="Slack message timestamp")
    response_json = models.JSONField(null=True, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return f"{self.tool_name} - {self.status} ({self.created_at})"

class HumanResponse(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    request = models.ForeignKey(MCPToolRequest, related_name='responses', on_delete=models.CASCADE)
    text = models.TextField()
    user_name = models.CharField(max_length=100, blank=True)
    file_url = models.URLField(max_length=500, blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f"Response to {self.request.id} - {self.created_at}"
